tsveten_kavrakov

2.Change List

Jun 19th, 2019
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner sc=new Scanner(System.in);
  7.  
  8.         int[] intInputArr=Arrays.stream(sc.nextLine().split(" ")).mapToInt(e->Integer.parseInt(e)).toArray();
  9.         List<Integer>intList=new ArrayList<Integer>();
  10.  
  11.         for (int i:intInputArr){
  12.             intList.add(i);
  13.         }
  14.  
  15.         String strInput="";
  16.         while(!(strInput=sc.nextLine().toLowerCase()).equals("end")){
  17.             String[] strInputArr=strInput.split(" ");
  18.             switch (strInputArr[0]){
  19.                 case "add":
  20.                     int number=Integer.parseInt(strInputArr[1]);
  21.                     intList.add(number);
  22.                     break;
  23.                 case "insert":
  24.                     int indexToInsert=Integer.parseInt(strInputArr[2]);
  25.                     if (indexToInsert>=0 && indexToInsert<intList.size()){
  26.                         int numberToInsert=Integer.parseInt(strInputArr[1]);
  27.                         intList.add(indexToInsert,numberToInsert);
  28.                     }else{
  29.                         System.out.println("Invalid index");
  30.                     }
  31.                     break;
  32.                 case "remove":
  33.                     int indexToRemove = Integer.parseInt(strInputArr[1]);
  34.                     if (indexToRemove>=0 && indexToRemove<intList.size()){
  35.                          intList.remove(indexToRemove);
  36.                     }
  37.                     else{
  38.                         System.out.println("Invalid index");
  39.                     }
  40.                      break;
  41.                 case "shift":
  42.                     String direction=strInputArr[1];
  43.                     int count=Integer.parseInt(strInputArr[2]);
  44.                     if (direction.equals("left")){
  45.                         ShiftLeft(intList,count);
  46.                     }else{
  47.                         ShiftRight(intList,count);
  48.                     }
  49.                     break;
  50.             }
  51.         }
  52.  
  53.         for (int i=0;i<intList.size();i++){
  54.             System.out.printf("%d ",intList.get(i));
  55.         }
  56.  
  57.     }
  58.  
  59.     static void ShiftLeft(List<Integer> l,int count){
  60.         for (int shiftStep=0;shiftStep<count;shiftStep++){
  61.             int temp=l.get(0);
  62.             for (int i=0;i<l.size()-1;i++){
  63.                 l.set(i,l.get(i+1));
  64.             }
  65.             l.set(l.size()-1,temp);
  66.         }
  67.     }
  68.  
  69.     static void  ShiftRight(List<Integer> l,int count){
  70.  
  71.         for (int shiftStep=0;shiftStep<count;shiftStep++){
  72.             int temp=l.get(l.size()-1);
  73.             for (int i=l.size()-1;i>0;i--){
  74.                 l.set(i,l.get(i-1));
  75.             }
  76.             l.set(0,temp);
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment