fork download
  1. package org.JavaIncloud.java;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import java.util.ListIterator;
  7.  
  8. import javax.swing.border.Border;
  9.  
  10. public class ListShowCase
  11. {
  12. public static void main(String...JavaInCloud)
  13. {
  14. /*create an ArrayList(a class implements List) and assign to a list as we can not create object for List interface*/
  15. List<String> birds = new ArrayList<String>();
  16.  
  17. //Store some birds name to the collection of bird object(Here String)
  18. birds.add("Penguins");
  19. birds.add("Crows");
  20. birds.add("Sparrows");
  21. birds.add("Dippers");
  22. birds.add("Manakins");
  23.  
  24. /*print the ArrayList, it'll print the bird name, as internally ArrayList overwrite toString()*/
  25. System.out.println("Bird Name>>"+birds);
  26. //output:Dippers:Bird Name>>[Penguins, Crows, Sparrows, Dippers, Manakins]
  27.  
  28. /*get a specific element using index over get(...) method*/
  29. System.out.println(birds.get(3));//output:Dippers
  30.  
  31. /*Get an instance of iterator from array list.Iterator is an interface implemented by ArrayList indirectly*/
  32. Iterator<String> iterator = birds.iterator();
  33.  
  34. /*Retrieve element using Iterator's next() method*/
  35. while (iterator.hasNext())
  36. {
  37. String bird = (String) iterator.next();
  38. System.out.print(bird+",");
  39. }
  40. //output:Dippers: Penguins,Crows,Sparrows,Dippers,Manakins,
  41.  
  42. /*similarly you can iterate it using for-each and ListIterator in reverse and forward direction
  43. *ListIterator listIterator = birds.listIterator();*/
  44. }
  45. }
  46.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:10: error: class ListShowCase is public, should be declared in a file named ListShowCase.java
public class ListShowCase 
       ^
1 error
stdout
Standard output is empty