Advertisement
wingman007

Java_ArrayList

Nov 23rd, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. package arraylist2;
  8. import java.util.ArrayList;
  9. import java.util.Iterator;
  10.  
  11. /**
  12.  *
  13.  * @author fmi
  14.  */
  15. public class ArrayList2 {
  16.  
  17.     /**
  18.      * @param args the command line arguments
  19.      */
  20.     public static void main(String[] args) {
  21.         // TODO code application logic here
  22.         ArrayList listTest = new ArrayList();
  23.         // ArrayList<String> listTest = new ArrayList<String>();
  24.         // You can only add objects, however
  25.         listTest.add( "first item" );
  26.     listTest.add( "second item" );
  27.     listTest.add( "third item" );
  28.     listTest.add( 7 );
  29.        
  30.         // To go through each item in your ArrayList you can set up something called an Iterator.
  31.         Iterator it = listTest.iterator( );
  32.  
  33.         while ( it.hasNext( ) ) {
  34.             System.out.println( it.next( ) );
  35.         }
  36.        
  37. // You can also remove items from an ArrayList.
  38.         listTest.remove(2);
  39.  
  40.         // Or you can use the value on the list:
  41.         listTest.remove( "second item" );
  42.  
  43.         // to print the whole list
  44.         System.out.println("Whole list = " + listTest);
  45.  
  46.         // System.out.println("Position 1 = " + listTest(1));        
  47.     }
  48.    
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement