ArrayLists

ArrayList is like a regular Java array on steroids. It overcomes some of the shortfalls of arrays such as having to predetermine its size. It adds some really useful methods to make its data easy to manage, and it uses an enhanced version of a for loop, which is clearer to use than a regular for loop.

Let's look at some code that uses ArrayList:

// Declare a new ArrayList called myList to hold int variables
ArrayList<int> myList;
 
// Initialize the myList ready for use
myList = new ArrayList<int>();

In the previous code, we declared and initialized a new ArrayList called myList. We can also do this in a single step as shown in the following code:

ArrayList<int> myList = new ArrayList<int>();

We have seen nothing especially interesting so far, so let's take a look at what we can actually do with ArrayList. Let's use String ArrayList this time:

// declare and initialize a new ArrayList
ArrayList<String> myList = new ArrayList<String>();

// Add a new String to myList in the next available location
myList.add("Donald Knuth");
// And another
myList.add("Rasmus Lerdorf");
// And another
myList.add("Richard Stallman");
// We can also choose 'where' to add an entry
myList.add(1, "James Gosling");

// Is there anything in our ArrayList?
if(myList.isEmpty()){
  // Nothing to see here
}else{
  // Do something with the data
}

// How many items in our ArrayList?
int numItems = myList.size();

// Now where did I put James Gosling?
int position = myList.indexOf("James Gosling");

In the previous code, we saw that we can use some really useful methods of the ArrayList class in our ArrayList object. We can add an item (myList.add), add it at a specific location (myList.add(x, value)), check whether ArrayList is empty (myList.isEmpty), see how big the array is (myList.size()), and get the current position of a given item (myList.indexOf).

Note

There are even more methods in the ArrayList class, and you can read about them at http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html. What we have seen so far is enough to complete this book.

With all this functionality, all we need now is a way to handle ArrayLists dynamically.

The enhanced for loop

This is what the condition of an enhanced for loop looks like:

for (String s : myList)

The previous example would iterate (step through) all of the items in myList one at a time. At each step, s would hold the current String.

So, this code would print on the console all our eminent programmers from the previous section's ArrayList code sample:

for (String s : myList){
  Log.i("Programmer: ","" + s);
}

We can also use the enhanced for loop with regular arrays too:

int [] anArray = new int [];
// We can initialize arrays quickly like this
anArray {0, 1, 2, 3, 4, 5}

for (int s : anArray){
	Log.i("Contents = ","" + s);
}

There's another incoming newsflash!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.219.166