• (2 Pages)
  • +
  • 1
  • 2

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

#16 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

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

Posted 24 March 2010 - 03:30 PM

About the performance notion...(from a while ago)

Typically, a regular array will be quicker, although, if you're using minute data sets, then it won't really matter, as the program will still run very quickly.

The reason for this is that when you allocate a regular array, it looks for one contiguous block of memory.

I'm not sure how the ArrayList is implemented, but if it's anything like a Linked List, then the memory won't be in one giant block, it will just be one node with a pointer to another node in memory (and so forth), thus slowing the execution time. The memory would be split up in a Linked List versus being all together. (this is the Linked List. I'm not sure how the ArrayList works exactly. Probably somewhat similarly)

If you're doing a more complex project that requires speed to function (such as a game), you'd want to use regular arrays wherever possible. (again, depending on the size of the project. If you're LEGITIMATELY making a game, use regular arrays. If you're just making Pong as a beginner, it doesn't really matter)
Was This Post Helpful? 0
  • +
  • -

#17 calebjonasson  Icon User is offline

  • $bert = new DragonUnicorn(); $bert->rawr();
  • member icon

Reputation: 209
  • View blog
  • Posts: 989
  • Joined: 28-February 09

Posted 04 April 2010 - 11:53 AM

There is a problem with your add method.
You stated that list.add(number, index) however it is the other way around.

numbers.add(10, 0);


should be:

numbers.add(0, 10);


This will allow things to be pushed back properly.
Was This Post Helpful? 1
  • +
  • -

#18 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

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

Posted 08 April 2010 - 10:20 PM

WOW. MAH BAD.

Can't believe that went unnoticed all this time.

Editing it right now.
Was This Post Helpful? 0
  • +
  • -

#19 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2002
  • View blog
  • Posts: 4,162
  • Joined: 11-December 07

Posted 05 August 2010 - 04:44 PM

A subtly important difference is when it comes to generics. This is a real example which is causing me major headaches right now. If I have a class declared as:

public class MyClass


I can create either an array or linked list:

MyClass[] asArray = new MyClass[10];
ArrayList<MyClass> asList = new ArrayList<MyClass>();


However, if I refactor MyClass to be generic:

public class MyClass<T>


The ArrayList is no problem at all:

ArrayList<MyClass<String>> asList = new ArrayList<MyClass<String>>();


but the array is nasty:

MyClass<String>[] asArray = new MyClass<String>[10]; // Compiler error
MyClass<String>[] asArray = new MyClass[10]; // Compiler warning
MyClass[] asArray = new MyClass[10]; // Compiler warning and lose the advantages of generics.


The more code I write and the more of my old code I have to maintain, the more I come to appreciate the collections framework. For me, arrays are pretty much reserved for when I need a performance boost, and only if the Collection alternative is demonstrably causing the bottleneck.

This post has been edited by cfoley: 06 August 2010 - 03:33 AM

Was This Post Helpful? 0
  • +
  • -

#20 audriusa  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 28
  • Joined: 22-September 10

Posted 23 September 2010 - 06:37 AM

Maybe for the total completeness a couple of positive features of the static arrays:

1. They are faster, ArrayLists are implemented on the top of them.
2. They can store primitive types like int or boolean. ArrayList needs to wrap these into objects (Integer, Boolean) first, this further slows down the program. The impact of course depends a lot on how many values do you have.
3. Static arrays have they size immediately after they are constructed. ArrayList is always constructed with zero size. This in some cases requires more complex initialization.
4. Static arrays can be easier intialized ( int [] a = {1,8,7,0}, do this with ArrayList without at least three lines of code).
Was This Post Helpful? 0
  • +
  • -

#21 Posiedon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 20-January 12

Posted 22 February 2012 - 10:56 AM

very nice tutorial...ty....i am a beginner to programming and i didnt even know that something like Arraylists exists.Now it looks much better than static arrays.
Was This Post Helpful? 0
  • +
  • -

#22 Posiedon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 20-January 12

Posted 22 February 2012 - 11:24 AM

I tried to implement a simple program to grasp the concept better but my program isnt running.I fail to understand what is the problem.Please have a look:

import java.util.ArrayList;

public class Arraylistsdemo 
{

	public static void main(String[] args) 
	{
		int i,z;
       ArrayList<Integer> numbers = new ArrayList<Integer> ();
       numbers.add(10);
       numbers.add(1,20);
       for (i=0;i<numbers.size();i++);
       {
    
		z= numbers.get(i);
    	System.out.println(z);
    	
       }
	}

}


Error:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at Arraylistsdemo.main(Arraylistsdemo.java:15)

Thanks in advance.
Was This Post Helpful? 0
  • +
  • -

#23 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

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

Posted 27 February 2012 - 07:29 AM

You simply have a semicolon after your for loop declaration.

for (i = 0; i < numbers.size(); i++) // no semicolon should be here


This is also one reason that if I know I only need the loop variable (in this case i) inside the loop, I declare it in the loop itself. Then, the compiler would have told you that i is out of scope and it would have given you a clue as to where the error is.

This is how I would code your for loop, if it helps:

for (int i = 0; i < numbers.size(); i++)
{
	int z = numbers.get(i);
	System.out.println(z);
}


:)

This post has been edited by Locke: 27 February 2012 - 07:30 AM

Was This Post Helpful? 0
  • +
  • -

#24 Posiedon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 20-January 12

Posted 27 February 2012 - 07:48 AM

thank you so much bro.And regarding declaring the local variable inside the loop thanks for that advice too.I'll definitely do it that way in my next program.
Was This Post Helpful? 0
  • +
  • -

#25 rosepetalpowder  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 28-February 12

Posted 02 March 2012 - 09:02 PM

nice tutorial. made me understand about arraylist and static arrays. but i need some example to practise to understand it better.

very nicely explained. i am beginner in java and this was helpful. i liked the funny comment in the end abt ninjas..lols.
Was This Post Helpful? 0
  • +
  • -

#26 cbkh2bankai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 07-April 12

Posted 25 April 2012 - 10:47 AM

Very helpful I was just looking into the array deal. Didn't know it had this much and you explained it perfectly. :]
Was This Post Helpful? 0
  • +
  • -

#27 7umper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 29-April 12

Posted 29 April 2012 - 09:00 PM

Thanks man
Was This Post Helpful? 0
  • +
  • -

#28 Alexey_Shevelyov-  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-April 12

Posted 24 September 2012 - 10:52 AM

This was very helpfull and understandable!
Keep it up sir!
Was This Post Helpful? 0
  • +
  • -

#29 Solixious  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 10
  • Joined: 26-July 13

Posted 26 July 2013 - 10:26 PM

It is a nice tutorial mate. I was wondering if I could substitute a multi-dimension arrays using ArrayList. If yes, would you be kind enough to tell me how it can be done?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2