ArrayList vs. Static Arrays Comparison & Explanation
#16
Posted 24 March 2010 - 03:30 PM
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)
#17
Posted 04 April 2010 - 11:53 AM
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.
#18
Posted 08 April 2010 - 10:20 PM
Can't believe that went unnoticed all this time.
Editing it right now.
#19
Posted 05 August 2010 - 04:44 PM
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
#20
Posted 23 September 2010 - 06:37 AM
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).
#21
Posted 22 February 2012 - 10:56 AM
#22
Posted 22 February 2012 - 11:24 AM
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.
#23
Posted 27 February 2012 - 07:29 AM
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
#24
Posted 27 February 2012 - 07:48 AM
#25
Posted 02 March 2012 - 09:02 PM
very nicely explained. i am beginner in java and this was helpful. i liked the funny comment in the end abt ninjas..lols.
#26
Posted 25 April 2012 - 10:47 AM
#28
Posted 24 September 2012 - 10:52 AM
Keep it up sir!
|
|







MultiQuote








|