Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class ArrayManipulatorV2 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String line = scanner.nextLine();
- int[] numbers = Arrays.stream(line.split(" ")).mapToInt(Integer::parseInt).toArray();
- String input = "";
- while (!"end".equals(input = scanner.nextLine())) {
- String[] comands = input.split(" ");
- if ("exchange".equals(comands[0])) {
- int index = Integer.parseInt(comands[1]);
- exchangesPlaces(numbers, index);
- } else if ("max".equals(comands[0])) {
- maxIndexElement(numbers, comands[1]);
- } else if ("min".equals(comands[0])) {
- minIndexElement(numbers, comands[1]);
- } else if ("first".equals(comands[0])) {
- int index = Integer.parseInt(comands[1]);
- firstElement(numbers, index, comands[2]);
- } else if ("last".equals(comands[0])) {
- int index = Integer.parseInt(comands[1]);
- lastElement(numbers, index, comands[2]);
- }
- }
- System.out.println(Arrays.toString(numbers));
- }
- private static void printArray(int count, int[] reverse) {
- if (count == 0) {
- System.out.println("[]");
- } else {
- String otput = "[";
- for (int i = 0; i < count; i++) {
- if (i < count - 1) {
- otput += (reverse[i]) + ", ";
- } else {
- otput += (reverse[i]) + "]";
- }
- }
- System.out.println(otput);
- }
- }
- private static boolean invalidCoun(int[] numbers, int index) {
- if (index < 0 || index > numbers.length) {
- System.out.println("Invalid count");
- return true;
- }
- return false;
- }
- private static void lastElement(int[] numbers, int index, String comand) {
- int count = 0;
- int[] result = new int[numbers.length];
- if (invalidCoun(numbers, index)) return;
- if ("even".equals(comand)) {
- for (int i = 0; i < numbers.length; i++) {
- if (numbers[i] % 2 == 0 && index > count) {
- result[count++] = numbers[i];
- }
- }
- } else if ("odd".equals(comand)) {
- for (int i = 0; i < numbers.length; i++) {
- if (numbers[i] % 2 != 0 && index > count) {
- result[count++] = numbers[i];
- }
- }
- }
- int[] reverse = new int[result.length];
- for (int i = result.length-1; i >=0 ; i--) {
- reverse[i]=result[i];
- }
- printArray(count, reverse);
- }
- private static void firstElement(int[] numbers, int index, String comand) {
- int count = 0;
- int[] result = new int[numbers.length];
- if (invalidCoun(numbers, index)) return;
- if ("even".equals(comand)) {
- for (int i = 0; i < numbers.length; i++) {
- if (numbers[i] % 2 == 0 && index > count) {
- result[count++] = numbers[i];
- }
- }
- } else if ("odd".equals(comand)) {
- for (int i = 0; i < numbers.length; i++) {
- if (numbers[i] % 2 != 0 && index > count) {
- result[count++] = numbers[i];
- }
- }
- }
- printArray(count, result);
- }
- private static void minIndexElement(int[] numbers, String comand) {
- int min = Integer.MAX_VALUE;
- int index = -1;
- if ("even".equals(comand)) {
- for (int i = 0; i < numbers.length; i++) {
- if (numbers[i] % 2 == 0) {
- if (numbers[i] <= min) {
- min = numbers[i];
- index = i;
- }
- }
- }
- } else if ("odd".equals(comand)) {
- for (int i = 0; i < numbers.length; i++) {
- if (numbers[i] % 2 != 0) {
- if (numbers[i] <= min) {
- min = numbers[i];
- index = i;
- }
- }
- }
- }
- if (index == -1) {
- System.out.println("No matches");
- } else {
- System.out.println(index);
- }
- }
- private static void maxIndexElement(int[] numbers, String comand) {
- int max = Integer.MIN_VALUE;
- int index = -1;
- if ("even".equals(comand)) {
- for (int i = 0; i < numbers.length; i++) {
- if (numbers[i] % 2 == 0) {
- if (numbers[i] >= max) {
- max = numbers[i];
- index = i;
- }
- }
- }
- } else if ("odd".equals(comand)) {
- for (int i = 0; i < numbers.length; i++) {
- if (numbers[i] % 2 != 0) {
- if (numbers[i] >= max) {
- max = numbers[i];
- index = i;
- }
- }
- }
- }
- if (index == -1) {
- System.out.println("No matches");
- } else {
- System.out.println(index);
- }
- }
- private static void exchangesPlaces(int[] numbers, int index) {
- int count = 0;
- int[] result = new int[numbers.length];
- if (index < 0 || index >= numbers.length) {
- System.out.println("Invalid index");
- return;
- }
- for (int i = index + 1; i < numbers.length; i++) {
- result[count++] = numbers[i];
- }
- for (int i = 0; i <= index; i++) {
- result[count++] = numbers[i];
- }
- for (int i = 0; i < numbers.length; i++) {
- numbers[i] = result[i];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement