ArrayLists Exercises

  < Previous  Next >
  1. Discuss with someone else why the following code will or will not work (assuming Circle.java and CheckingAccount.java are in the same directory):
    import java.util.ArrayList;
    
    public class InheritancePolymorphismArrayList{
        public static void main( String[] args ){
            ArrayList< Object > objArrayList = new ArrayList< Object >();
            Integer myInt = 42;
            Circle cir = new Circle( 5.0 );
            CheckingAccount checking = new CheckingAccount();
            String message = "Can you see me?";
    
            objArrayList.add( myInt );
            objArrayList.add( cir );
            objArrayList.add( checking );
            objArrayList.add( message );
    
            for( int i = 0; i < objArrayList.size(); ++i){
                System.out.println( objArrayList.get(i).toString() );
            }
        }
    }
  2. What if an ArrayList< Object > just had Strings in it? Why would or would not the following code work:
    ArrayList< Object > stringArrayList = new ArrayList< Object >();
    stringArrayList.add( "Who?" );
    
    for( int i = 0; i < stringArrayList.size(); ++i){
        System.out.println( stringArrayList.get(i).length() );
    }
  3. Compare and contrast an array and an ArrayList object with someone else.
  4. Discuss with someone else the difference between what ArrayList.size() returns and the largest index
  5. Discuss with someone else the difference between what ArrayList.size() returns and the number of elements allocated to an ArrayList
  6. Write code to do the following tasks:
    public class ArrayListPractice{
    
        public static void main( String[] args ){
    
            // A. Declare an ArrayList object
    
            // B. Add some objects to your ArrayList
    
            // C. Display the number of elements in your ArrayList (using an ArrayList method)
    
            // D. Retrieve some objects from your ArrayList
    
            // E. Display the number of elements in your ArrayList (using an ArrayList method)
    
        }
    }
    
  7. What will be the contents of favFruits after the following code executes:
    ArrayList<String> favFruits = new ArrayList<String>();
    
    int index = 1;
    
    favFruits.add( "Spondias Mombin" );
    favFruits.add( "mango" );
    favFruits.add( "peach" );
    favFruits.add( "banana" );
    favFruits.add( "prunes" );
    
    System.out.println( "favFruits has " + favFruits.size( ) + " elements");
    
    System.out.println( "index " + index + ": " + favFruits.get( index ));
    
    favFruits.remove( index );
    
    System.out.println( "index " + index + ": " + favFruits.get( index ));
    
    System.out.println( "favFruits has " + favFruits.size( ) + " elements");
    
    favFruits.set( index, "tarap" );
    favFruits.set( index, "mangostein" );
    
    System.out.println( "index " + index + ": " + favFruits.get( index ));
    
    System.out.println( "favFruits has " + favFruits.size( ) + " elements");
    
    System.out.println( "index " + index + ": " + favFruits.get( favFruits.size() ));