Petra_Abrasheva_185

ArrayList_String

Mar 2nd, 2021
644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.ArrayList;
  3.  
  4. public class ArrayList_185 {
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner Input = new Scanner(System.in);
  8.         ArrayList<String> MyList = new ArrayList<>();//създаване на арейлист
  9.  
  10.         //System.out.println("Please choose :: <add> <rem> <prn> <end> :");
  11.         String choice = "";//Input.nextLine();
  12.  
  13.         while (!choice.contains("end")) {//край
  14.  
  15.             if (choice.equals("add")) {//добавяне
  16.                 String name = Input.next();
  17.                 MyList.add(name);
  18.             }
  19.  
  20.             if ((choice.equals(("rem")))) {//премахване
  21.                 System.out.println("Input number:");
  22.                 int number = Input.nextInt();
  23.                 if (number > 0 && number <= MyList.size()) {
  24.                     MyList.remove(number - 1);
  25.                 }
  26.             else{
  27.                     System.out.println("No such number:");
  28.                 }
  29.  
  30.                 if (choice.equals("prn")) {
  31.                     System.out.println(MyList);
  32.                 }
  33.  
  34.             }
  35.             System.out.println("Please choose :: <add> <rem> <prn> <end> :");
  36.             choice = Input.nextLine();
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment