JAVA ARRAYLIST and JAVA ARRAY

Java ArrayListJava Array
 Importing 
 import java.util.ArrayList;

 (at the very top)
 nothing to import
 Creating 
 balloons = new ArrayList<Balloon>();

 someList = new ArrayList<SomeType>();

 creates an empty set of items
 balloons = new Balloon[ 20 ];

 someList = new SomeType[ 30 ];

 need to know the size in advance
 Indexing 
 0..size-1 

           __0_____1_____2_____3_____4__
 balloons: |_(*)_|_(*)_|_(*)_|_(*)_|_(*)_|
 0..size-1

           __0_____1_____2_____3_____4__
 balloons: |_(*)_|_(*)_|_(*)_|_(*)_|_(*)_|
 Size/Length 
 balloons.size()
 balloons.length
 Extracting Element 
 Balloon b = balloons.get( index ); 
 Balloon b = balloons[ index ]; 
 Updating Element 
 balloons.set( index, someBalloon ); 

 // differs from .add(...) -- replaces balloon
 // at given index, does not add new cell     
 balloons[ index ] = someBalloon; 
 Adding Elements 
 balloons.add( someBalloon );

 // balloon added to the end, size increased by 1
 not possible, initial size is fixed;
 Removing Elements 
 Balloon b = balloons.remove( index );

 // balloon at given index removed, size decreased by 1
 not possible, initial size is fixed;

ENHANCED FOR-LOOP (FANCY FOR-LOOP)

The enhanced for-loop is a more convenient form of expressing repetition.

It is applicable when we traverse an array/ArrayList:

The following are equivalent:

Regular LoopEnhanced Loop
double[] numbersList = { 1.2, 3.4, 5.6 };    
for ( int i = 0; i < numbersList.length; i = i + 1 )
{
    double curValue = numbersList[i];

    // do something with curValue
}

// this says: for each valid index:  
//                get the item at that index
//                do something with the item
//
// too much focus on index, all we want is the item
double[] numbersList = { 1.2, 3.4, 5.6 };
for ( double curValue : numbersList )
{
    // do something with value
}



// this says: for each item:
//                do something with the item
//
// this is much clearer, focus is on the item

// to get enhanced for-loop:
// * remove all index related components
// * move up the line that gets curValue
  
double[] numbersList = { 1.2, 3.4, 5.6 };    
for ( int i = 0; i < numbersList.length; i = i + 1 )
{
    ^-^-^-^-------- : ---------^-^-^
    double curValue = numbersList[i];

    // do something with curValue
}




double[] numbersList = { 1.2, 3.4, 5.6 };
for ( double curValue : numbersList )
{
    // do something with curValue
}




String[] wordsList = { "how", "are", "you" };    
for ( int i = 0; i < wordsList.length; i = i + 1 )
{
    String curWord = wordsList[i];

    // do something with curWord
}
String[] wordsList = { "how", "are", "you" };    
for ( String curWord : wordsList )
{
    // do something with curWord
}



ArrayList<String> wordsList = new ArrayList<String>();

// add some words to the list  

for ( int i = 0; i < wordsList.size(); i = i + 1 )
{
    String curWord = wordsList.get(i);

    // do something with curWord
}
ArrayList<String> wordsList = new ArrayList<String>();

// add some words to the list  

for ( String curWord : wordsList )
{
    // do something with curWord
}