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