| 4 | |
| 5 | public class ArrayListProg { |
| 6 | public static void main(String[] args) throws IOException { |
| 7 | System.out.println("Program for implementing ArrayList: "); |
| 8 | ArrayList<Character> obj = new ArrayList<Character>(); |
| 9 | |
| 10 | Scanner sc = new Scanner(System.in); |
| 11 | char ans; |
| 12 | do { |
| 13 | System.out.println("Main Menu"); |
| 14 | System.out.println("1. Create \n2. Display \n3. Insert \n4. Delete \n5. Modify"); |
| 15 | System.out.println("Enter your choice: "); |
| 16 | int choice = sc.nextInt(); |
| 17 | switch (choice) { |
| 18 | case 1: |
| 19 | System.out.println("Inserting some elements in the array..."); |
| 20 | System.out.println("How many characters do you want to add to the list? "); |
| 21 | int n = sc.nextInt(); |
| 22 | for (int i = 0; i < n; i++) { |
| 23 | System.out.println("Enter character: "); |
| 24 | char ch = sc.next().charAt(0); |
| 25 | if (Character.isDigit(ch)) |
| 26 | throw new IOException("Invalid data type!"); |
| 27 | else |
| 28 | obj.add(ch); |
| 29 | } |
| 30 | break; |
| 31 | case 2: |
| 32 | System.out.println("The size of the array is: " + obj.size()); |
| 33 | System.out.println("The array elements are: " + obj); |
| 34 | break; |
| 35 | case 3: |
| 36 | System.out.println("Inserting some elements in the array in between: "); |
| 37 | System.out.println("Enter the index at which you want to insert new character: "); |
| 38 | int i = sc.nextInt(); |
| 39 | System.out.println("Enter character: "); |
| 40 | char ch = sc.next().charAt(0); |
| 41 | if (Character.isDigit(ch)) |
| 42 | throw new IOException("Invalid data type!"); |
| 43 | else |
| 44 | obj.add(i, ch); |
| 45 | break; |
| 46 | case 4: |
| 47 | System.out.println("Enter the index of the number that you want to delete: "); |
| 48 | int index = sc.nextInt(); |
| 49 | System.out.println("Removing " + index + "th element from the array!"); |
| 50 | obj.remove(index); |
| 51 | break; |
| 52 | case 5: |
| 53 | System.out.println("Updating the elements from the array: "); |
| 54 | System.out.println("Enter the index at which you want to modify the character: "); |
| 55 | i = sc.nextInt(); |
| 56 | System.out.println("Enter character: "); |
| 57 | ch = sc.next().charAt(0); |
| 58 | if (Character.isDigit(ch)) |
| 59 | throw new IOException("Invalid data type!"); |
| 60 | else |
| 61 | obj.set(i, ch); |
| 62 | break; |
| 63 | } |