Array and ArrayList

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi All

Can someone please tell me the difference between an Array and an ArrayList, and where I would be likely to use one over the other

Thanks alot for any help
 
An ArrayList is a more complex class that can be grown on demand. Normal CLR
arrays are immutable so you can't add or remove items once you've created
them.


--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/

Kevin said:
Hi All,

Can someone please tell me the difference between an Array and an
ArrayList, and where I would be likely to use one over the other.
 
An ArrayList type is roughly equivalent to the LinkedList learned about in
beginner CompSci classes - its length can be changed; whereas an Array type
is immutable and so cannot be resized.

Kevin said:
Hi All,

Can someone please tell me the difference between an Array and an
ArrayList, and where I would be likely to use one over the other.
 
Hi Kevin,
In addition to what the other said about the immutability of arrays and the
dynamic size of ArrayLists I would like to clarify some points:

1. Arrays are immutable. This doesn't meen that you cannot change the items
of the array, though. Unlike the strings, which are immutable also, arrays
items can be changed, but the size of the array cannot be changed.

2. As a result of this immutability looping over arrays for example are well
optimized.
example:
int[] intArr = new int[10];
ArrayList arrList = new ArrayList();
//code to initialize the array and the list

for(int i = 0; i < arrList.Count; i++)
{
......
}
on each cycle arrList.Count method will be called (because the size is not
guarantee to be const)
-------------
for(int i = 0; i < intArr.Length; i++)
{
......
}
intArr.Length will be called only once at the begining of the loop

for(int i = 0; i < arrList.Count; i++)
{
......
}

3. Arrays are strongly typed.

4. Adding value types to ArraysList will cause boxing and reading
respectively will call unboxing. Arrays ,though, keeps the value type in
unboxed state. And calling some of the operations may not need any
boxing/unboxing or even copy the value in the stack; The unboxing will be
done anyways with ArrayList
Example:
struct Test
{
int a;
public void Foo()
{
a++;
}
public override string ToString()
{
return "Test";
}

}

---------------
Test[] arr = new Test[1]{new Test()};

none of the following will cause the object in the array to be boxed/unboxed
or even copied in the stack
arr[0].Foo();
Console.WriteLine(arr[0].ToString());

5. Arrays can be used with P\Invoke

Maybe there is other advantages Arrays may have infornt of ArrayLists and
the other collection. My idea is to bring your attentions on them because
you might get the wrong impression that there is no need of Arrays and
ArrayLists are the better choice.

--
B\rgds
100

Kevin said:
Hi All,

Can someone please tell me the difference between an Array and an
ArrayList, and where I would be likely to use one over the other.
 
I never said it was implemented with a linked list, in fact I made no
reference to implementation details at all...

"An ArrayList type is roughly equivalent to the LinkedList learned about in
beginner CompSci classes - its length can be changed"

However, it is good to see people referencing rotor. It's a wonderful
learning tool.

Ben


Peter Rilling said:
Actually, an ArrayList is not implemented with a linked-list.

In internal storage for an ArrayList is simply an array of objects
(immutable). When an item is inserted, and there is no more room, the
object[] is copied into a new array.

Here is the source code for the ArrayList.
http://dotnet.di.unipi.it/Content/sscli/docs/doxygen/fx/bcl/arraylist_8cs-source.html

Ben Rush said:
An ArrayList type is roughly equivalent to the LinkedList learned about in
beginner CompSci classes - its length can be changed; whereas an Array type
is immutable and so cannot be resized.


ArrayList, and where I would be likely to use one over the other.
 
Back
Top