Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,847 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,604 people online right now. Registration is fast and FREE... Join Now!




ArrayList vs. Static Arrays

 
Reply to this topicStart new topic

> ArrayList vs. Static Arrays, Comparison & Explanation

Rating  5
Locke37
Group Icon



post 28 Jul, 2008 - 01:07 PM
Post #1


Pre-condition: if you're reading this and intend to test it, you MUST have JDK 5 or later, since the implementation of the ArrayList class was changed in JDK 5.


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.

java
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.

java
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.

java
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>...

java
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!

java
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.

java
numbers.add(10, 0);


This adds our number 10 as the FIRST number in our list. Everything that was at index 0 or greater is pushed back 1 index.

Basic add() syntax: add(OBJECT_TO_ADD, INDEX_TO_ADD_AT);

The second parameter is optional, if you don't use it, the object is put in the back of the list.

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. smile.gif

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.

java
// "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.

java
// "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.

java
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
[1, 2, 3, 4, 5]


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. smile.gif


Checking if we have data

It's very simple...The ArrayList class has a few methods that make this incredibly easy.

java
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.

java
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. smile.gif Any feedback would be GREATLY appreciated!

"At DIC we be card dealing, array-smashing code ninjas! decap.gif" -~- Martyr2

Buh-Bye! biggrin.gif

This post has been edited by Locke37: 30 Jul, 2008 - 05:46 PM
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

commanderderjukes
*



post 30 Jul, 2008 - 01:10 AM
Post #2
QUOTE(Locke37 @ 28 Jul, 2008 - 02:07 PM) *

Pre-condition: if you're reading this and intend to test it, you MUST have JDK 5 or later, since the implementation of the ArrayList class was changed in JDK 5.


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.

CODE
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.

CODE
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.

CODE
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>...

CODE
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!

CODE
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.

CODE
numbers.add(10, 0);


This adds our number 10 as the FIRST number in our list. Everything that was at index 0 or greater is pushed back 1 index.

Basic add() syntax: add(OBJECT_TO_ADD, INDEX_TO_ADD_AT);

The second parameter is optional, if you don't use it, the object is put in the back of the list.

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. smile.gif

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.

CODE
// "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.

CODE
// "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.

CODE
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
[1, 2, 3, 4, 5]


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. smile.gif


Checking if we have data

It's very simple...

CODE
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. smile.gif Any feedback would be GREATLY appreciated!

"At DIC we be card dealing, array-smashing code ninjas! decap.gif" -~- Martyr2

Buh-Bye! biggrin.gif



post was helpful thanks man. didnt know the arraylist class had an internal mechanism that can search for elements
within it .nice icon_up.gif icon_up.gif icon_up.gif icon_up.gif icon_up.gif
Go to the top of the page
+Quote Post

Locke37
Group Icon



post 30 Jul, 2008 - 02:42 PM
Post #3
No problem! biggrin.gif

Glad I could help!
Go to the top of the page
+Quote Post

chili5
****



post 1 Nov, 2008 - 05:00 AM
Post #4
Wow, that is very useful. Thanks dude. biggrin.gif That'll help with my class problems/contest problems. smile.gif

Thanks a bunch. biggrin.gif
Go to the top of the page
+Quote Post

Locke37
Group Icon



post 2 Nov, 2008 - 07:23 PM
Post #5
To repeat myself. smile.gif

QUOTE(Locke37 @ 30 Jul, 2008 - 03:42 PM) *

No problem! biggrin.gif

Glad I could help!
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 12/3/08 06:09PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month