Darwinbots Forum
Code center => Darwinbots3 => Topic started by: Botsareus 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#)
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;
}
}
}
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();
}
}
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
-
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).
-
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...
-
The idea was to be able to re-size the array after it was created...
You can resize a List<>.
We can pass <int> as well, to restrict it to only integers...
Ah, be careful with your vocabulary. The angle brackets <> denote the "generics" (http://msdn.microsoft.com/en-us/library/ms379564%28v=vs.80%29.aspx). 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).
-
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.
You can resize a List<>.
Can you give me an example? TY
-
Here, this should blow your mind a bit :P
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));
-
Oh Nice, not only did I learn a new way to create an array, but I learned a new Operator as well. :P