ArrayList(s) and Static Array(s). They have a lot in common. They both store data, they can both be traversed in loops...However, the ArrayList is MUCH more appealing in my opinion, since it can be dynamically re-sized, that is, re-sized during execution of a program, which is very nice. On the flip side, a static array has size limitations to how much space you declared them to have.
Let's get to the code, shall we?
Static Arrays
The static array. A nice little structure that holds data in a neat fashion. We can traverse through the data in a loop. It seems like a perfect thing...WRONG.
public class Main
{
public static void main(String[] args)
{
int[] numbers = new int[5];
}
}
This array can hold up to 5 integers. It essentially already does hold them, since it initializes them to zero. You CANNOT hold more than 5 in this data structure, or you will get an ArrayIndexOutOfBoundsException, unless you "re-size" it...which is kind of pointless unless you back up your data, because when you re-size the old array, you lose ALL of your data.
numbers = new int[10];
Whenever you use the keyword new in creating/"re-sizing" a static array...you lose all of the previous data. In other words, all 10 of the new numbers will be set to zero.
ArrayList
The way to declare an ArrayList follows.
ArrayList<CLASS> name = new ArrayList<CLASS>();
If you leave out the <CLASS> part of it, it can hold any object. It's the same as doing this...ArrayList<Object>...
import java.util.ArrayList; // necessary to access the ArrayList class
public class Main
{
public static void main(String[] args)
{
ArrayList<Integer> integers = new ArrayList<Integer>();
}
}
This integers ArrayList will only hold data of type Integer.
Methods of the ArrayList class
Adding Data
Well, we have an ArrayList, let's add something to it!
ArrayList<Integer> numbers = new ArrayList<Integer>(); numbers.add(10)
This adds the value 10 to our list. There is another add() method, which is as follows.
numbers.add(0, 10);
This adds our number 10 as the FIRST number in our list. Remember, the indexes start at zero, not one. Everything that was at index 0 or greater is pushed back 1 index. The first parameter specifies the index to add the item at.
Accessing Data
Well, now that we use a class for storing data, we have to use methods to get to that data. Let's use our Integer list for an example...In older times (AKA pre-JDK 5), you had to "unwrap" the Object of type Integer, because you can't use an Integer object in calculations. If you had an ArrayList to store some Object form of a simple data type (int, double, boolean, etc...), you have to call other methods, and convert from object to simple data type to get the actual value of that simple data type. Now, it's as simple as using the get() method.
We use the method get(INDEX) to access a particular slot in the list. This accesses the object at the specified index in the list.
// "numbers" is predefined int num = numbers.get(0); // first item
Traversing ArrayLists
We can use a for loop to access/output all of our array elements.
// "numbers" is predefined for (int x = 0; x < numbers.size(); x++) System.out.println(numbers.get(x));
The previous code will access ALL of the elements, since the condition in our loop to stop is x < numbers.size(). The size() method just returns how many objects are in our list.
There is an alternate way to print out the list...the following.
System.out.println(numbers);
That, if we say we have 5 integers in our list (1, 2, 3, 4, 5), will print this out...
Quote
As you can see, when we normally say println(some_object); it will normally print out a memory address. The ArrayList functions differently. It's not a jerk.
Checking if we have data
It's very simple...The ArrayList class has a few methods that make this incredibly easy.
boolean empty = numbers.isEmpty();
Well, I hope you can you can see what the isEmpty() method is going to tell us...
The next method makes searching for repeats and such very easy. As a matter of fact, this is what I used numerous times in my snippet here...NoRepeatRandom. (actually, upon further examination, I didn't use it. I created my own method to check if the data is in there)
int num = 10;
if (numbers.contains(num))
System.out.println("10 is in the list!");
Well, that's easy, if the list contains whatever you put in the contains() method...a boolean value is returned...true in this case, since we added the number 10 to the list before.
Well, I hope this tutorial helped! I hope I didn't sound like I was rambling. Gimme a break, it's my first tutorial.
"At DIC we be card dealing, array-smashing code ninjas!
Buh-Bye!







MultiQuote









|