What is a good use for ArrayList?

lack of type == dangerous?

Page 1 of 1

10 Replies - 2311 Views - Last Post: 07 January 2009 - 06:05 PM Rate Topic: -----

#1 badjava   User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

What is a good use for ArrayList?

Post icon  Posted 06 January 2009 - 06:48 PM

I'm reading up on creating array or arraylist in C# and wondering what do people think the best use of an arraylist would be as opposed to an array or list.

I'm reading about it's lack of type but it doesn't make sense to me yet as the primary example given shows that mixing two different types in the same arraylist would throw an exception error.

So now I'm curious what the primary use would be as opposed to more traditional(?) arrays or even a List, which looks like it does need a type declared if I'm understanding this correctly.

(oooh i might have just found my answer, re-reading one of the paragraphs "An ArrayList is a dynamically resizable array" AH HA, that is probably the answer yes?)

This post has been edited by badjava: 06 January 2009 - 06:50 PM


Is This A Good Question/Topic? 0
  • +

Replies To: What is a good use for ArrayList?

#2 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: What is a good use for ArrayList?

Posted 06 January 2009 - 07:02 PM

an ArrayList was one of the only ways to do a "dynamic array" in .Net 1.1. With .Net 2.0, generic lists were added and SHOULD be used instead of ArrayLists.

But yes, the main purpose of ArrayLists(and Generic Lists(List<>)) are that they are dynamic. And to some, they are easier to deal with than arrays.
Was This Post Helpful? 0
  • +
  • -

#3 badjava   User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: What is a good use for ArrayList?

Posted 06 January 2009 - 07:12 PM

View Posteclipsed4utoo, on 6 Jan, 2009 - 06:02 PM, said:

an ArrayList was one of the only ways to do a "dynamic array" in .Net 1.1. With .Net 2.0, generic lists were added and SHOULD be used instead of ArrayLists.

But yes, the main purpose of ArrayLists(and Generic Lists(List<>)) are that they are dynamic. And to some, they are easier to deal with than arrays.


Ah thank you. My reading material indicated a preference of List<T> over ArrayList but didn't mention the changes in .net versions. From now on I think I will understand what they mean by 'unless backwards compatibility is desired' though :P

You and my reading material both referred to lists as generic but these have to be type specific (as opposed to ArrayList) when created correct?

I think I am confusing generic with type or lack of.
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: What is a good use for ArrayList?

Posted 06 January 2009 - 07:24 PM

View Postbadjava, on 6 Jan, 2009 - 09:12 PM, said:

View Posteclipsed4utoo, on 6 Jan, 2009 - 06:02 PM, said:

an ArrayList was one of the only ways to do a "dynamic array" in .Net 1.1. With .Net 2.0, generic lists were added and SHOULD be used instead of ArrayLists.

But yes, the main purpose of ArrayLists(and Generic Lists(List<>)) are that they are dynamic. And to some, they are easier to deal with than arrays.


Ah thank you. My reading material indicated a preference of List<T> over ArrayList but didn't mention the changes in .net versions. From now on I think I will understand what they mean by 'unless backwards compatibility is desired' though :P

You and my reading material both referred to lists as generic but these have to be type specific (as opposed to ArrayList) when created correct?

I think I am confusing generic with type or lack of.


it's "generic" because you can make it any type that you like at design time. It's not generic in the way that it can be any type at runtime. If you want it to be a string list, you make it a string list and you will have a type-safe string list.

It was kinda confusing to me also...."generic lists that are type specific? How is that generic?
Was This Post Helpful? 1
  • +
  • -

#5 badjava   User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: What is a good use for ArrayList?

Posted 07 January 2009 - 01:29 AM

View Posteclipsed4utoo, on 6 Jan, 2009 - 06:24 PM, said:

It was kinda confusing to me also...."generic lists that are type specific? How is that generic?


Yeah me too obv. but your explanation of the difference between design time or run time cleared that up for me. Thanks!
Was This Post Helpful? 0
  • +
  • -

#6 badjava   User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: What is a good use for ArrayList?

Posted 07 January 2009 - 02:02 AM

View Posteclipsed4utoo, on 6 Jan, 2009 - 06:24 PM, said:

it's "generic" because you can make it any type that you like at design time. It's not generic in the way that it can be any type at runtime. If you want it to be a string list, you make it a string list and you will have a type-safe string list.

To update to the latest standard I changed my tutorial code from this:
ArrayList arrlist = new ArrayList();
			for (int i = 0; i < 10; i++) 
			{
				Console.WriteLine (i);
				arrlist.Add (i);
			}

to this:
List<int> arrlist = new List<int>();
			for (int i = 0; i < 10; i++) 
			{
				Console.WriteLine (i);
				arrlist.Add (i);
			}

It actually completed the build with no errors! So I was pleasantly surprised. Then I ran my code and it worked great until the .bin file I created and saved the data to was read back from disk, to demonstrate deserialization, and attempted to bring the List<int> back in to an ArrayList :rolleyes: since I forgot to change that part of the code also. It looks like it will be painless to convert to List<T> as long as I convert all of them. :P

P.S. Look at this call to st_in to bring the binary datafile back into a List. I'm pretty new at this so maybe it's just me but that looks repetitive and a bit ugly:
List<int> arrlist2 = (List<int>)binfmt2.Deserialize(st_in);

This post has been edited by badjava: 07 January 2009 - 02:10 AM

Was This Post Helpful? 0
  • +
  • -

#7 badjava   User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: What is a good use for ArrayList?

Posted 07 January 2009 - 04:18 PM

View Posteclipsed4utoo, on 6 Jan, 2009 - 06:24 PM, said:

it's "generic" because you can make it any type that you like at design time. It's not generic in the way that it can be any type at runtime. If you want it to be a string list, you make it a string list and you will have a type-safe string list.

I'm going crazy trying to find this code to paste in and see what you think but I'll have to just describe it until I can track it down again so forgive me if I don't get it exact but hopefully you will get the idea of what I'm describing.

In the tutorial they create an ArrayList in a class and the list has two different array's/objects inside of it. One of the arrays has a type of string and one has a type of int. Later in the program arguments are passed to the arraylist which include an int and string value targeting the two typed arrays specifically.

I don't see any other way they could have done this with a dynamic array other than with the truly generic ArrayList and it's troubling me. I want code I write today to be up to the latest standard and not using loopholes from older revisions that could cause problems later.

OK I found the code, first is the class where the int and string objects are defined and then I'll paste in the code from Main that uses an arraylist to create objects using the class which contain both int and string information.
		[Serializable]
		class myClass
		{
			private int _myint = 0;
            private string _mystring = "xxx";

			public myClass( int anint, string astring )
			{
				_myint = anint;
				_mystring = astring;
			}

			public int MyInt 
			{
				get 
				{ 
					return _myint; 
				}
				set 
				{
					_myint = value; 
				}
			}

			public string MyString 
			{
				get 
				{ 
					return _mystring; 
				}
				set 
				{
					_mystring = value; 
				}
			}

		}
//now code from main
[STAThread]
		static void Main(string[] args)
		{
			//
			// TODO: Add code to start application here
			//
			myClass ob1 = new myClass(100, "One Hundred");
			myClass ob2 = new myClass(20, "Twenty");

			ArrayList arrlist = new ArrayList();
			arrlist.Add( ob1 );
			arrlist.Add( ob2 );

So this is kind of complicated to me where the ArrayList should probably be updated to a newer standard but is a truly generic dynamic array the only way you could get away with this particular piece of code?

This post has been edited by badjava: 07 January 2009 - 05:41 PM

Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: What is a good use for ArrayList?

Posted 07 January 2009 - 05:03 PM

yeah, the code will be needed.
Was This Post Helpful? 0
  • +
  • -

#9 badjava   User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: What is a good use for ArrayList?

Posted 07 January 2009 - 05:42 PM

View Posteclipsed4utoo, on 7 Jan, 2009 - 04:03 PM, said:

yeah, the code will be needed.


Argh, my edited post with the code went away apparently, lemme go find it again...


OK, I went back in my browser history and resaved the post again, it's a couple of messages back above this one.
Was This Post Helpful? 0
  • +
  • -

#10 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: What is a good use for ArrayList?

Posted 07 January 2009 - 05:58 PM

View Postbadjava, on 7 Jan, 2009 - 06:18 PM, said:

OK I found the code, first is the class where the int and string objects are defined and then I'll paste in the code from Main that uses an arraylist to create objects using the class which contain both int and string information.
		[Serializable]
		class myClass
		{
			private int _myint = 0;
            private string _mystring = "xxx";

			public myClass( int anint, string astring )
			{
				_myint = anint;
				_mystring = astring;
			}

			public int MyInt 
			{
				get 
				{ 
					return _myint; 
				}
				set 
				{
					_myint = value; 
				}
			}

			public string MyString 
			{
				get 
				{ 
					return _mystring; 
				}
				set 
				{
					_mystring = value; 
				}
			}

		}
//now code from main
[STAThread]
		static void Main(string[] args)
		{
			//
			// TODO: Add code to start application here
			//
			myClass ob1 = new myClass(100, "One Hundred");
			myClass ob2 = new myClass(20, "Twenty");

			ArrayList arrlist = new ArrayList();
			arrlist.Add( ob1 );
			arrlist.Add( ob2 );

So this is kind of complicated to me where the ArrayList should probably be updated to a newer standard but is a truly generic dynamic array the only way you could get away with this particular piece of code?


you would leave the class the same. The Main method would look like this...

[STAThread]
		static void Main(string[] args)
		{
			//
			// TODO: Add code to start application here
			//
			myClass ob1 = new myClass(100, "One Hundred");
			myClass ob2 = new myClass(20, "Twenty");

			List<myClass> list = new List<myClass>();
			list.Add( ob1 );
			list.Add( ob2 );
			   }



This is because the type of object is "myClass". It's not a "combination" type of string and int.

That is how you create your own custom class.
Was This Post Helpful? 1
  • +
  • -

#11 badjava   User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: What is a good use for ArrayList?

Posted 07 January 2009 - 06:05 PM

View Posteclipsed4utoo, on 7 Jan, 2009 - 04:58 PM, said:

This is because the type of object is "myClass". It's not a "combination" type of string and int.

That is how you create your own custom class.

You know, the whole time I was posting my note and waiting to see what you (or someone else) thought about it I kept thinking what if it is something really really simple and I'm just not getting the paradigm shift.

I am still not making an easy jump to classes == new custom type.

Thank you for the help :rolleyes:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1