Showing posts with label Many. Show all posts
Showing posts with label Many. Show all posts

Tuesday, May 8, 2012

ArrayList

An ArrayList is a resizable array. There is no need to tell the ArrayList how many items it can store.

Similar to an array, in that all objects in the ArrayList occupy an index (and remember, indexes begin at 0).

Additionally, items may be added and removed without providing an index.

Instantiating ArrayList

Syntax:
ArrayList<Type> identifier = new ArrayList<Type>();


example:
ArrayList<String> customerNames = new ArrayList<String>();


In the example above, the ArrayList can store a collection of Strings.

We can also add user defined types (Types/classes we have created) to ArrayList.

Let's say for instance we have a class type called Player. We can create an ArrayList to store a collection of Player objects:


example:
ArrayList<Player> playerList = new ArrayList<Player>();



Adding to ArrayList

Syntax:
identifier.add(object);


example:
playerList.add(new Player("baz",100));


In the example above, a Player is added to the ArrayList. The new object is added to the end of the list.

Notice that we can instantiate the Player object directly into the add method.

Alternatively, we could add an existing Player object to the ArrayList:

Player p = new Player("maz",200);
playerList.add(p);

Replacing an element in ArrayList

Syntax:
identifier.set(index,object);

example:
playerList.set(10, new Player("baz",100));


In the example above, a Player object at index 10 is replaced in the ArrayList.


Removing from ArrayList

Syntax:
identifier.remove(index);


example:
playerList.remove(3);


Check ArrayList for element

Syntax:
identifier.contains(object);

example:
playerList.contains(myObj);

In the example above, the list will return true or false depending on whether the object called obj is found in the list - note: you must override the equals method to use this effectively!.




Getting the number of items in ArrayList

Syntax:
identifier.size();


example:
playerList.size();



In the example below, the size function is used to set the continuation condition in a for loop:

for (int x = 0; x < playerList.size(); x ++) { }



Looping through ArrayList

ArrayList contains a method called get which enables you to return the object in the list at a specific index.

Syntax:
identifier.get(index);


example:
playerList.get(1);


In the above example, we get the Player object at index 1 (the second object in the list).

In the example below, we loop through an ArrayList, using the loop counter to get each object in turn:

for (int x = 0; x < playerList.size(); x ++) {
Player currentPlayer = playerList.get(x);
}

If there are 20 Player objects in the list, the loop will iterate 20 times. On each pass of the loop, the get method returns the Player object at the specified index. As we are using the loop counter to provide this value, we are able to get each object in the list, in turn.


Searching ArrayList for a particular object

In the following example, we search ArrayList for a Player object, by playerID (the particular playerID is passed to the function as a parameter). An equality comparison is performed within the loop using the equality operator ( = = ):

public Player searchForPlayer(int playerID) {
Player p = null;

for (int x = 0; x < playerList.size(); x ++) {
Player currentPlayer = playerList.get(x);
if (currentPlayer.getID == playerID) {
p = currentPlayer;
}
}
return p;
}

In the following example, we search ArrayList for a Player object, by playerName (the particular playerName is passed to the function as a parameter). An equality comparison is performed within the loops using the equals function:


public Player searchForPlayer(String playerName) {
Player p = null;

for (int x = 0; x < playerList.size(); x ++) {
Player currentPlayer = playerList.get(x);
if (currentPlayer.getName.equals(playerName) {
p = currentPlayer;
}
}
return p;
}

"ArrayList (Java 2 Platform SE v1.4.2)" ( http://bit.ly/Ja6HGS )