Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Java Print ArrayList

This Java Print ArrayList example shows how to print values of Java ArrayList.
  1. import java.util.ArrayList;
  2.  
  3. //Java Print ArrayList, Java Display ArrayList
  4.  
  5. /*
  6.  * This Java Print ArrayList example shows how to print values of
  7.  * Java ArrayList.
  8.  */
  9.  
  10. public class JavaPrintArrayList {
  11.    
  12.     public static void main(String args){
  13.        
  14.         ArrayList<String> alistNumbers = new ArrayList<String>();
  15.         alistNumbers.add("1");
  16.         alistNumbers.add("2");
  17.         alistNumbers.add("3");
  18.        
  19.         //pint elements of ArrayList using println method
  20.         System.out.println(alistNumbers);
  21.        
  22.         //Or use for loop to display Java ArrayList elements
  23.        
  24.         for(int i=0; i< alistNumbers.size(); i++){
  25.             System.out.println(alistNumbers.get(i));
  26.         }
  27.        
  28.         /*
  29.          * However, this will not display proper output if the objects added
  30.          * to the ArrayList does not have toString method implemented.
  31.          *
  32.          * For example, object of Class X is added to the ArrayList and Class X
  33.          * does not have toString method overridden.
  34.          */
  35.     }
  36. }
  37.  
  38. /*
  39. Output of above given Java Print ArrayList example would be,
  40. [1, 2, 3]
  41. 1
  42. 2
  43. 3
  44. */
Your rating: None Average: 2 (2 votes)
Share this
Anonymous's picture

Esta opción es mas

Esta opción es mas elegante: for(String i: alistNumbers){ System.out.println(i); }

Post new comment

7 + 11 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.