View RSS Feed

penguinCoder

ArrayList Cheat Sheet

Rating: 2 votes, 5.00 average.
by , 10-29-2012 at 11:17 AM (3469 Views)
Importing the Library
Before you can use an ArrayList, you must first import the necessary library.
Java Code:
import java.util.ArrayList
Creating a Generic ArrayList
Generic ArrayList is an ArrayList with a type; no other type is allowed to be inserted into the ArrayList.
Java Code:
ArrayList<type> listName = new ArrayList<type>();
Example of this would be:
Java Code:
ArrayList<String> myArrayList = new ArrayList<String>();

Creating Non-Generic ArrayList
Java Code:
ArrayList listName = new ArrayList();
An example of this would be:
Java Code:
ArrayList myArrayList = new ArrayList();

Note
You do not need to build the ArrayList to any mentioned size, as it will automatically expand. This however is not recomended, as it will take many additional resources to accomplish this. Each time the ArrayList needs to expand, it creates a new ArrayList, and copies all of the contents to the new ArrayList, using the System.arrayCopy() method. As you can tell, this would eat up alot of additional resources.


Creating ArrayList with a Size
Java Code:
ArrayList<type> listName = new ArrayList<type>(size);
ArrayList listName = new ArrayList(size);
An example of this would be:
Java Code:
ArrayList<String> myArrayList = new ArrayList<String>(50);
ArrayList myArrayList = new ArrayList(50);



An ArrayList has a host of different methods that are associated with it that allow the programmer to modify it. Here are a few I have used, and examples of each.


Adding an Item
Java Code:
listName.add(item);
An example of this would be:
Java Code:
myArrayList.add("penguin");

Removing an Item
You can specify either the item, or the index number to remove an item from an ArrayList.
Java Code:
listName.remove(index);
listName.remove(item);
An example of this would be:
Java Code:
myArrayList.remove(0);
myArrayList.remove("penguin");

Replacing an Item
You must specify the index and the new item that is going to replace the old item.
Java Code:
listName.set(index, item);
An example of this would be:
Java Code:
myArrayList.set(0, "tux");

Checking the Size
Java Code:
listName.size();
An example of this would be:
Java Code:
int size = myArrayList.size();

Searching ArrayList
This will only return the index of the first occurance of whatever it is you are searching for.
Java Code:
listName.indexOf(item);
An example of this would be:
Java Code:
int ix = myArrayList.indexOf("penguin");

Verifying Contents
This will return a boolean true or false depending upon if the ArrayList does, or does not, contain what you are looking for.
Java Code:
listName.contains(item);
An example of this would be:
Java Code:
if( myArrayList.contains("penguin") )

Checking if Empty
This will return a boolean true if the ArrayList is empty.
Java Code:
listName.isEmpty();
An example of this would be:
Java Code:
while( myArrayList.isEmpty() );

Copying a Whole ArrayList
After creating a new ArrayList, you can copy the contents of an existing ArrayList to the new one.
Java Code:
newListName.addAll(listName);
An example of this would be:
Java Code:
ArrayList<String> copyArrayList = new ArrayList<String>();
copyArrayList.addAll(myArrayList);

Clearing an ArrayList
You can clear the entire contents of an ArrayList.
Java Code:
listName.clear();
An example of this would be:
Java Code:
myArrayList.clear();

Sorting an ArrayList
Java Code:
Collections.sort(listName);
An example of this would be:
Java Code:
Collections.sort(myArrayList);

Outputing an ArrayList
Java Code:
for( <type> varName : listName )
  System.out.println(varName);

An example of outputting a Generic ArrayList would be:
Java Code:
for( String ix : myArrayList )
  System.out.println(ix);
An example of outputting a Non-Generic ArrayList would be:
Java Code:
for(Object ix : myArrayList )
  System.out.println(ix);

Conversion
There might be times when you will need to convert an ArrayList to an ordinary array, or convery an ordinary array to an ArrayList.

ArrayList to an Array
Java Code:
listName.toArray(arrayName);
An example of this would be:
Java Code:
String[] regArray = new String[myArrayList.size()];
myArrayList.toArray(regArray);

Array to an ArrayList
Java Code:
ArrayList listName = Array.asList(arrayName)
An example would be:
Java Code:
ArrayList<String> myArrayList = Arrays.asList(regArray);

Submit "ArrayList Cheat Sheet" to Facebook Submit "ArrayList Cheat Sheet" to Digg Submit "ArrayList Cheat Sheet" to del.icio.us Submit "ArrayList Cheat Sheet" to StumbleUpon Submit "ArrayList Cheat Sheet" to Google

Comments