Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class ListDemo {
- public static void main(String[] args) {
- System.out.println("ArrayList Demonstration: ");
- ArrayList<String> al = new ArrayList<>();
- al.add("Apple");
- al.add("Banana");
- al.add("Cherry");
- System.out.println(al);
- al.size();
- System.out.println("Using for loop");
- for(String s: al) {
- System.out.print(s+" ");
- }
- System.out.println("\n"+"Using index");
- System.out.println(al.get(0));
- System.out.println("Using for-each loop");
- for(int i=0;i<al.size();i++) {
- System.out.print(al.get(i)+" ");
- }
- al.remove(0); //Remove using index
- System.out.println("\n"+al);
- al.remove("Cherry"); //Remove using the value
- System.out.println(al);
- al.add("Cherry"); //adding again
- System.out.println(al);
- System.out.println(al.indexOf("Cherry")); //Returns the first occurring index of element
- al.clear(); //Removes all the elements
- System.out.println(al);
- }
- }
Add Comment
Please, Sign In to add comment