veronikaaa86

Array Manipulator Two

Feb 14th, 2018
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.05 KB | None | 0 0
  1. package Lists;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8. import java.util.List;
  9. import java.util.stream.Collectors;
  10.  
  11. public class P05_ArrayManipulator {
  12.     public static void main(String[] args) throws IOException {
  13.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  14.  
  15.         String[] numbersArr = reader.readLine().split("\\s+");
  16.  
  17.         List<Integer> numsList = new ArrayList<>();
  18.         for (String n : numbersArr) {
  19.             numsList.add(Integer.parseInt(n));
  20.         }
  21.  
  22.         String command = "";
  23.         String[] sequence;
  24.  
  25.         while (!"print".equals(command)) {
  26.             sequence = reader.readLine().split("\\s+");
  27.             command = sequence[0];
  28.             int element = 0;
  29.             int position = 0;
  30.             int index = 0;
  31.             int sumElements = 0;
  32.             if ("add".equals(command)) {
  33.                 index = Integer.parseInt(sequence[1]);
  34.                 element = Integer.parseInt(sequence[2]);
  35.                 numsList.add(index, element);
  36.             } else if ("addMany".equals(command)) {
  37.                 for (int i = sequence.length - 1; i >= 2; i--) {
  38.                     index = Integer.parseInt(sequence[1]);
  39.                     element = Integer.parseInt(sequence[i]);
  40.                     numsList.add(index, element);
  41.                 }
  42.             } else if ("contains".equals(command)) {
  43.                 element = Integer.parseInt(sequence[1]);
  44.                 boolean contains = false;
  45.                 for (int i = 0; i < numsList.size(); i++) {
  46.                     if (numsList.get(i) == element) {
  47.                         System.out.println(i);
  48.                         contains = true;
  49.                         break;
  50.                     }
  51.                 }
  52.                 if (!contains) {
  53.                     System.out.println(-1);
  54.                 }
  55.             } else if ("remove".equals(command)) {
  56.                 index = Integer.parseInt(sequence[1]);
  57.                 numsList.remove(index);
  58.             } else if ("shift".equals(command)) {
  59.                 position = Integer.parseInt(sequence[1]);
  60.                 for (int i = 0; i < position % numbersArr.length; i++) {
  61.                     int temp = numsList.get(0);
  62.  
  63.                     for (int j = 0; j < numsList.size() - 1; j++) {
  64.                         numsList.set(j, numsList.get(j + 1));
  65.                     }
  66.                     numsList.set(numsList.size() - 1, temp);
  67.                 }
  68.             } else if ("sumPairs".equals(command)) {
  69.                 for (int i = 0; i < numsList.size() - 1; i++) {
  70.                     int firstElement = numsList.get(i);
  71.                     int secondElement = numsList.get(i + 1);
  72.                     sumElements = firstElement + secondElement;
  73.                     numsList.set(i, sumElements);
  74.                     numsList.remove(i + 1);
  75.                 }
  76.             }
  77.         }
  78.         System.out.println(numsList);
  79.     }
  80. }
Add Comment
Please, Sign In to add comment