Code center > Darwinbots3
An ArrayList inside an ArrayList
Botsareus:
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: ---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;
}
}
}
--- End code ---
--- Code: ---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();
}
}
--- End code ---
--- Code: ---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();
}
}
}
--- End code ---
oh, Copyright: Max Nastry :P
Numsgil:
--- Code: ---var list1 = new List<int>();
var list2 = int[50];
--- End code ---
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).
Botsareus:
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...
Numsgil:
--- Quote from: Botsareus on September 19, 2011, 03:05:40 PM ---The idea was to be able to re-size the array after it was created...
--- End quote ---
You can resize a List<>.
--- Quote ---We can pass <int> as well, to restrict it to only integers...
--- End quote ---
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).
Botsareus:
--- Quote ---the angle brackets <> denote the "generics".
--- End quote ---
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<>.
--- End quote ---
Can you give me an example? TY
Navigation
[0] Message Index
[#] Next page
Go to full version