Subscribe to Dogstopper's Code Mania        RSS Feed
***** 1 Votes

Java ArrayLists

Icon 2 Comments
Introduction
Arrays are one of the fundamental datatypes in Java (along with most other programming languages), but they do have some disadvantages that sometimes confuse beginners. Some questions that I commonly hear:


How do I add elements at a certain index without overwriting anything?
How do I remove a whole block at a certain index without leaving a null block?

Both of these answers, with a standard array means that there will need to be a second array to copy to, and is generally a huge pain in the butt, but fortunately, there is an Object provided in the standard Java library that allows easy manipulation of data in an array-like format. It is simply called ArrayList, and that is what I plan to show you how to use in this lesson.

Advantages:
  • ArrayLists support generics, and at all times, I suggest that you use them.
  • ArrayLists automatically change their size to accomidate an add or remove.
  • ArrayLists allow you to add a single element, or a collection of elements to any point in the ArrayList.
  • BUT, THEY ARE NOT THREAD SAFE...That's their primary disadvantage.

The ArrayList Methods
Well, there are quite a few, so if you look at this, you will see the key ones that will help you: http://java.sun.com/...#method_summary

As you can see, there are methods to add, remove, get, clear, and find elements in the ArrayList. There is also a unique method called toArray() that returns an array of the elements in the ArrayList in proper order.

Finally, let me make a note of extreme importance: ArrayLists can only hold objects, so in order to hold a fundamental data type like int or double, you must use their Container classes: (Integer, Double, Character, ...), a lesson for another day.

Now time for an example:
import java.util.ArrayList;
import java.util.Scanner;

class ArrayListTest {
	public static void main(String[] args) {
		// Make an ArrayList that holds Integers (As seen by the generics)
		ArrayList<Integer> array = new ArrayList<Integer>();
   
		// Add integers to the array until a non-integer is entered
		Scanner scan = new Scanner(System.in);
		while(true) {
			try {
				int tempNum = scan.nextInt();
				
				 // If it is not an integer, an exception was thrown
				 // So add the number to the array
				 array.add(new Integer(tempNum));
			 } catch (InputMismatchException ime) {
				break; // Break out of the loop
		 }
 
		 // Print out each element of the arraylist
		 for (Integer i : array) 
			 System.out.println(i);
	 }
}



That's about it! Anyway, remember, if you don't use a foreach loop that in order to access an index don't use brackets, [], use the .get(int index) method instead.

Thanks for reading!
~Stephen Schwahn

2 Comments On This Entry

Page 1 of 1

trvr 

23 February 2010 - 07:57 AM
Hey great tutorial, but do you think you could write one on Try-Catch loops and the different ways to catch data input errors? Thanks!
0

Dogstopper 

23 February 2010 - 03:49 PM
Maybe...although I think we have a tutorial on those here somewhere. If i can fit it in with the games, I will.
0
Page 1 of 1

December 2018

S M T W T F S
      1
2345678
9101112131415
16 171819202122
23242526272829
3031     

Recent Entries

Search My Blog

Recent Comments

1 user(s) viewing

1 Guests
0 member(s)
0 anonymous member(s)