ArrayListDemo - Java Program

You might also like

Download as pdf
Download as pdf
You are on page 1of 2
1 package collectiondemo; 3 import java.util ArrayList; 4 import java.util.scanners 5 6 public class ArrayListDemo { 7 public static void main(string[] args) { ® ArrayList alist = new ArreyListo(); 9 Scanner ip = new Scannes (System. in) ; 10 FE while (true) 32 { 13 System. out. print ("=--Menu--~\n"+ 14 "1, Insert an element\n" + 15 "2, Retrieve an element\n" + 16 3. update an element\n” + a "4. Remove an element\n" + ae "5. Search for an element\n" + 1s "6. Sort all element\n" + 20 "7, Print all element\n" + 21 "8. Exit\n" + 22 “Enter your choice: "); 23 int choice = Integer.parseint (ip-nextLine ()); 24 switch (choice) 25 ‘ 26 case 1: 27 System.out.print ("Enter the element to insert: "! 28 alist.add(ip-nextLine ()); 29 break; 30 case 2: a Syetom-out print ("Entor the position of oloment te rotriover "7 32 int position = Integer.parsetnt (ip nextLine ()); 33 if (position < 0 || position >= alist.size()) 34 System. out printIn ("Invalid position."); 35 else 26 Syotemsout peiuetu(BLowent a pusition "+ pusition +t. * alist get (position) ); 37 break; 28 case 3: 39 system.out.print ("Enter the position of element to update: ") 40 position = Integer.parseznt (ip.nextLine ())s an if (position < 0 || position >= alist.size()) az system. out.printin ("Invalid position."); 43 else 44 ( 45 System.out.print ("Enter the updated value: “) 46 alist.set (position, ip.nextLine()); oo > 48 break; ag case 4: 50 System.out.print ("Enter the position of element to remove: "); SL position = Integer.parseInt (ip.nextLine())s 52 53 34 35 56 37 56 59 60 or 62 63 64 65 66 67 6e 6s 70 n 2 3 "4 76 ” if (position < 0 || position >= alist.size()) System.out printin ("Invalid position."); else alist .remove (position) ; break; case 5: Systen.out.print ("Enter the element to search: "); String element = ip.nexthine(); position = alist. indexor (element) 5 if (position == -1) System.out.printin ("Element not found."); else System.out.printin ("Element found at position: " + position); break; case 6: alist-sort (mull); break: case 7: System.out.printin ("array list elements: " + alist); break; case @:return; default: break;

You might also like