By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,566 Members | 1,762 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,566 IT Pros & Developers. It's quick & easy.

Array and ArrayList

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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" <an*******@discussions.microsoft.com> wrote in message
news:4D**********************************@microsof t.com...
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

Nov 15 '05 #2

P: n/a
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" <an*******@discussions.microsoft.com> wrote in message
news:4D**********************************@microsof t.com...
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

Nov 15 '05 #3

P: n/a
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/ss...cs-source.html

"Ben Rush" <kw*****@yahoo.com> wrote in message
news:eX**************@TK2MSFTNGP12.phx.gbl...
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" <an*******@discussions.microsoft.com> wrote in message
news:4D**********************************@microsof t.com...
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


Nov 15 '05 #4

P: n/a
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" <an*******@discussions.microsoft.com> wrote in message
news:4D**********************************@microsof t.com...
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

Nov 15 '05 #5

P: n/a
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" <pe***@nospam.rilling.net> wrote in message
news:eR**************@TK2MSFTNGP12.phx.gbl...
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/ss...cs-source.html
"Ben Rush" <kw*****@yahoo.com> wrote in message
news:eX**************@TK2MSFTNGP12.phx.gbl...
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" <an*******@discussions.microsoft.com> wrote in message
news:4D**********************************@microsof t.com...
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



Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.