- 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() );
}
}
}
- 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() );
}
- Compare and contrast an array and an ArrayList object with someone else.
- Discuss with someone else the difference between what ArrayList.size() returns and the largest index
- Discuss with someone else the difference between what ArrayList.size() returns and the number of elements allocated to an ArrayList
- Write code to do the following tasks:
public class ArrayListPractice{
public static void main( String[] args ){
}
}
- 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() ));