Author Topic: An ArrayList inside an ArrayList  (Read 3862 times)

Offline Botsareus

  • Society makes it all backwards - there is a good reason for that
  • Bot God
  • *****
  • Posts: 4483
    • View Profile
An ArrayList inside an ArrayList
« on: September 17, 2011, 02:03:28 PM »
I have decided to post this for all the newbees who are used to vb6 array structure. The code is in three parts (c#)

Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
   public class Brian_Class
    {
       public ArrayList brain_parts = new ArrayList();

       public void addContents(object content)
       {
           brain_parts.Add(content);
           
       }
       public ArrayList getContent()
       {
           return brain_parts;
       }
    }
}

Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    class Robo_Class
    {
        public Brian_Class my_brain = new Brian_Class();
    }
}

Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList robots = new ArrayList();
            Robo_Class tmp_robo = new Robo_Class();
            robots.Add(tmp_robo);
            Robo_Class tmp_robo2 = (Robo_Class)robots[0];
            tmp_robo2.my_brain.addContents("1");
            tmp_robo2.my_brain.addContents("2");
            tmp_robo2.my_brain.addContents("3");
            robots[0] = tmp_robo2;
            tmp_robo = new Robo_Class();
            robots.Add(tmp_robo);
            tmp_robo2 = (Robo_Class)robots[1];
            tmp_robo2.my_brain.addContents("4");
            tmp_robo2.my_brain.addContents("5");
            tmp_robo2.my_brain.addContents("6");
            robots[1] = tmp_robo2;
            tmp_robo = new Robo_Class();
            robots.Add(tmp_robo);
            tmp_robo2 = (Robo_Class)robots[2];
            tmp_robo2.my_brain.addContents("7");
            tmp_robo2.my_brain.addContents("8");
            tmp_robo2.my_brain.addContents("9");
            robots[2] = tmp_robo2;
            for (int x = 0; x < robots.Count; x++)
            {
                tmp_robo2 = (Robo_Class)robots[x];
                ArrayList tmp_brain = tmp_robo2.my_brain.getContent();
                for (int y = 0; y < tmp_brain.Count; y++)
                {
                    Console.WriteLine(tmp_brain[y].ToString() + " :" + x.ToString() + "-" + y.ToString());
                }
            }
            Console.ReadLine();
            tmp_robo2 = (Robo_Class)robots[1];
            tmp_robo2.my_brain.addContents("Beans");
            robots[1] = tmp_robo2;
            for (int x = 0; x < robots.Count; x++)
            {
                tmp_robo2 = (Robo_Class)robots[x];
                ArrayList tmp_brain = tmp_robo2.my_brain.getContent();
                for (int y = 0; y < tmp_brain.Count; y++)
                {
                    Console.WriteLine(tmp_brain[y].ToString() + " :" + x.ToString() + "-" + y.ToString());
                }
            }
            Console.ReadLine();
        }
    }
}

oh, Copyright: Max Nastry  :P
« Last Edit: September 19, 2011, 03:06:22 PM by Botsareus »

Offline Numsgil

  • Administrator
  • Bot God
  • *****
  • Posts: 7742
    • View Profile
Re: An ArrayList inside an ArrayList
« Reply #1 on: September 18, 2011, 02:02:56 PM »
Code: [Select]
var list1 = new List<int>();
var list2 = int[50];

Either above are preferable to an ArrayList.  The problem is that ArrayLists aren't type safe (they don't understand what sort of objects they hold).  Both the above implement IList<int>, which I'll often use in the existing DB3 code as a read only way to access a list (it's not really read only, though).

Offline Botsareus

  • Society makes it all backwards - there is a good reason for that
  • Bot God
  • *****
  • Posts: 4483
    • View Profile
Re: An ArrayList inside an ArrayList
« Reply #2 on: September 19, 2011, 03:05:40 PM »
The idea was to be able to re-size the array after it was created...

We can pass <int> as well, to restrict it to only integers...
« Last Edit: September 19, 2011, 03:07:13 PM by Botsareus »

Offline Numsgil

  • Administrator
  • Bot God
  • *****
  • Posts: 7742
    • View Profile
Re: An ArrayList inside an ArrayList
« Reply #3 on: September 19, 2011, 05:07:11 PM »
The idea was to be able to re-size the array after it was created...

You can resize a List<>.

Quote
We can pass <int> as well, to restrict it to only integers...

Ah, be careful with your vocabulary.  The angle brackets <> denote the "generics".  We (ie: the coding community) don't usually say that we "pass" the "type parameter" from inside the angle brackets.  If you use that language you're going to either confuse the person you're talking to or make yourself seem unknowledgeable.  I'm not sure if you understand generics or not from your post (if you don't read that article I linked).

Offline Botsareus

  • Society makes it all backwards - there is a good reason for that
  • Bot God
  • *****
  • Posts: 4483
    • View Profile
Re: An ArrayList inside an ArrayList
« Reply #4 on: September 28, 2011, 03:09:37 PM »
Quote
the angle brackets <> denote the "generics".

lol, I just finished the chapter on all that stuff, and I still don't really get it. Don't bother Numsgil, I will figure it out myself eventually.

Quote
You can resize a List<>.

Can you give me an example? TY

Offline Numsgil

  • Administrator
  • Bot God
  • *****
  • Posts: 7742
    • View Profile
Re: An ArrayList inside an ArrayList
« Reply #5 on: September 29, 2011, 12:40:56 PM »
Here, this should blow your mind a bit :P

Code: [Select]
var list = new List<int>() { 5, 4, 2, 1, 7, };

list.Add(6);
list.Add(7);
list.Add(8);
list.Add(9);

list.ForEach(x => System.Console.WriteLine(x));

Offline Botsareus

  • Society makes it all backwards - there is a good reason for that
  • Bot God
  • *****
  • Posts: 4483
    • View Profile
Re: An ArrayList inside an ArrayList
« Reply #6 on: October 03, 2011, 04:18:26 PM »
Oh Nice, not only did I learn a new way to create an array, but I learned a new Operator as well.  :P