473,320 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Array and ArrayList

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
5 7841
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: davehunt | last post by:
Hi folks, New C# programmer here. I am reading some CSV data from a file into an ArrayList. I want to get the data from the ArrayList into a 2-dimensional array. I see a few references to...
13
by: Hrvoje Voda | last post by:
How to put a specified dataset table into an array list ? Hrcko
9
by: Steve | last post by:
Hello, I created a structure ABC and an array of type ABC Public Structure ABC Dim str1 As String Dim int1 As Integer End Structure Public ABC1 As New ABC, ABC2 As New ABC
4
by: Peter | last post by:
I run into this situation all the time and I'm wondering what is the most efficient way to handle this issue: I'll be pulling data out of a data source and want to load the data into an array so...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
18
by: Sam | last post by:
Hi All I'm planing to write an application which allows users dynamically add their points (say you can add upto 30,000) and then draw xy graph. Should I use an array for my coordinate point...
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
5
by: Paulers | last post by:
Hello all, I have a string array with duplicate elements. I need to create a new string array containing only the unique elements. Is there an easy way to do this? I have tried looping through...
12
by: Maxwell2006 | last post by:
Hi, I declared an array like this: string scriptArgs = new string; Can I resize the array later in the code? Thank you, Max
9
by: Brian Tkatch | last post by:
I'm looking for a simple way to unique an array of strings. I came up with this. Does it make sense? Am i missing anything? (Testing seems to show it to work.) Public Function Unique(ByVal...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.