fork download
  1. import java.util.ArrayList;
  2.  
  3. class ArrayListTest
  4. {
  5. public static void main(String [] args)
  6. {
  7. //Standard Java arrays are of a fixed length. After arrays are created,
  8. //they cannot grow or shrink, which means that you must know in advanced
  9. //how many elements an array will hold.
  10.  
  11. //Array lists are vreated with an initial size. When this size is exceeded
  12. //the collection is automatically enlarged. When objects are removed,
  13. //the array may be shrunk.
  14.  
  15. ArrayList<String> names = new ArrayList<>();
  16.  
  17. names.add("James");//0
  18. names.add("Peter");//1
  19. names.add("John");//2
  20. names.add("Jake");//3
  21. names.add("Paul");//4
  22.  
  23. for (int i = 0 ; i < names.size(); i++)
  24. {
  25. System.out.println(names.get(i));
  26. }
  27. }
  28. }
Success #stdin #stdout 0.06s 32352KB
stdin
Standard input is empty
stdout
James
Peter
John
Jake
Paul