• (2 Pages)
  • +
  • 1
  • 2

ArrayList vs. Static Arrays Comparison & Explanation Rate Topic: ***** 3 Votes

#1 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Posted 28 July 2008 - 02:07 PM

*
POPULAR

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.

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

[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. :) The reason it prints out all of our data nice and neatly is because it overrides the toString() method, which System.out.println() calls to print objects. But be careful, if you're storing custom objects in the ArrayList, you'll still get a print out, but it will be the memory addresses of your objects, unless you override the toString() method in your custom class.


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) :rolleyes:

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. :) Any feedback would be GREATLY appreciated!

"At DIC we be card dealing, array-smashing code ninjas! :snap:" -~- Martyr2

Buh-Bye! :D

Is This A Good Question/Topic? 21
  • +

Replies To: ArrayList vs. Static Arrays

#2 commanderderjukes  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 28-January 08

Posted 30 July 2008 - 02:10 AM

post was helpful thanks man. didnt know the arraylist class had an internal mechanism that can search for elements
within it .nice :^: :^: :^: :^: :^:
Was This Post Helpful? 0
  • +
  • -

#3 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Posted 30 July 2008 - 03:42 PM

No problem! :D

Glad I could help!
Was This Post Helpful? 0
  • +
  • -

#4 chili5  Icon User is offline

  • D.I.C Lover

Reputation: 20
  • View blog
  • Posts: 1,144
  • Joined: 28-December 07

Posted 01 November 2008 - 06:00 AM

Wow, that is very useful. Thanks dude. :D That'll help with my class problems/contest problems. :)

Thanks a bunch. :D
Was This Post Helpful? 0
  • +
  • -

#5 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Posted 02 November 2008 - 08:23 PM

To repeat myself. :)

View PostLocke37, on 30 Jul, 2008 - 03:42 PM, said:

No problem! :D

Glad I could help!

Was This Post Helpful? 0
  • +
  • -

#6 Ajavalearner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-February 09

Posted 06 February 2009 - 04:12 AM

Hey dude , the information u provide were really good , no doubt abt it ,
But for me i am totally a beginner and it was bit hard in initial phase to learn what exactly arraylist
and want some more realtime examples which is using the whole of arraylist
But really thanks for it , it is really good. and keep it up
Was This Post Helpful? 0
  • +
  • -

#7 Mark200  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 04-December 08

Posted 27 February 2009 - 09:52 AM

Hey really good tutorial thanks :D

I now understand arraylists because of this! My lecturer kept saying we should use arraylists for an assignment we have instead of normal arrays and I could never get my head around them until now. They seem so easy now. thank you!


Although I think there is one tiny mistake:

for (int x = 0; x < numbers.size(); x++)
		System.out.println(numbers.get(x));


I think that should "numbers.size() - 1"

This post has been edited by Mark200: 27 February 2009 - 09:55 AM

Was This Post Helpful? 0
  • +
  • -

#8 muhstang1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-March 09

Posted 04 March 2009 - 07:33 PM

View PostMark200, on 27 Feb, 2009 - 08:52 AM, said:

Hey really good tutorial thanks :D

I now understand arraylists because of this! My lecturer kept saying we should use arraylists for an assignment we have instead of normal arrays and I could never get my head around them until now. They seem so easy now. thank you!


Although I think there is one tiny mistake:

for (int x = 0; x < numbers.size(); x++)
		System.out.println(numbers.get(x));


I think that should "numbers.size() - 1"

What would be the purpose of (-1). If there are five objects and x < 5 (numbers.size), then objects 0,1,2,3,4 get traversed which is the point of a loop. I suppose if you did not want to include the last entry in the loop...but then you have other looping possibilities. So the short answer would be "no", not (-1). But as for the initial post, it was just what I was looking for. Cheers!
Was This Post Helpful? 0
  • +
  • -

#9 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Posted 11 March 2009 - 12:49 PM

@Mark200: No, that is not an error. Keep in mind, indexes start at zero, so if I wanted to go to 0, 1, 2, 3, 4, I would start at zero, and end before x hits the size of the array, which is 5, since that is the number of elements in the list. So I want to keep the loop going as long as x is less than 5. When it hits 5, it exits the loop.

It goes from x = 0, x = 1, x = 2, x = 3, x = 4, x = 5 << does not execute when x = 5. It accesses every index.

If I did the numbers.size() - 1, I would have to change the middle condition to x <= numbers.size() - 1 instead of just using <. Because then that would be saying go until x is less than or equal to the size minus 1, which is 4.

Hope this clears it up for you. :)

This post has been edited by Locke: 11 March 2009 - 12:52 PM

Was This Post Helpful? 1
  • +
  • -

#10 ankur586  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-June 09

Posted 02 June 2009 - 09:53 PM

Nice tutorial.
Was This Post Helpful? 0
  • +
  • -

#11 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Posted 11 June 2009 - 10:16 PM

GREAT tutorial man! I'm new to all things programming, so I didn't even know what an array list was before reading this. In my classes so far in college we had only used Static Arrays. Haha, that's the last time I use one of those!

This post has been edited by eZACKe: 11 June 2009 - 10:16 PM

Was This Post Helpful? 0
  • +
  • -

#12 joezim007  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 110
  • Joined: 13-September 08

Posted 13 June 2009 - 08:39 AM

If I hadn't already known all this before I read the tutorial I would say it's great. ;) I actually came in here thinking it would be a performance comparison. Oh well. I'll just have to do that on my own. :)
Was This Post Helpful? 0
  • +
  • -

#13 Atspulgs  Icon User is offline

  • D.I.C Regular

Reputation: 68
  • View blog
  • Posts: 380
  • Joined: 29-July 09

Posted 28 August 2009 - 08:11 AM

The array list seems very similar to Vector. What would be the main difference between them?
Was This Post Helpful? 0
  • +
  • -

#14 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 481
  • Joined: 05-February 10

Posted 23 February 2010 - 05:27 AM

Thanks Locke you helped me understand: transversing arraylists, and check if one empty.
Was This Post Helpful? 0
  • +
  • -

#15 XRaizeX  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 20
  • Joined: 20-March 10

Posted 23 March 2010 - 09:59 PM

Thanks, Great tutorial. :clap:
Ive used them before, but i didnt kno they were called Static arrays.
I just wrote a program implementing arraylists. I gotta say, array lists ARE a lot better than static arrays.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2