Reputation: 23
I have create a class, islands, for which i have created 7 instances in the main class. Each instance has an in assigned, which represents how many containers need to be picked up from each.
class Program
{
public static int[,] TEUlayer1 = new int[4, 4] { { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 } };
public static int[,] TEUlayer2 = new int[4, 4] { { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 0 } };
public static Island is1 = new Island(2);
public static Island is2 = new Island(3);
public static Island is3 = new Island(1);
public static Island is4 = new Island(4);
public static Island is5 = new Island(2);
public static Island is6 = new Island(2);
public static Island is7 = new Island(1);
static void Main(string[] args)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
Console.Write(TEUlayer1[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
Console.Write(TEUlayer2[i, j] + " ");
}
Console.WriteLine();
}
}
}
class Island
{
public int S8s4collection { get; set; }
public Island(int s8s)
{
s8s = S8s4collection;
}
}
it is probably worth ignoring the main method and TEUlayer1/2 parts. these represent a stack of containers on a small ship, but this isn't relevant to the question.
My question is can I create a loop, or some other function, that goes through each instance of the island class (is1
, is2
...) and appends the value associated with it to a list or array etc?
thanks!
Upvotes: 1
Views: 72
Reputation: 134
To be able to iterate over your islands you need a collection of Islands.
This works:
public class Program
{
public static int[,] TeuLayer1 = new int[4, 4] { { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 } };
public static int[,] TeuLayer2 = new int[4, 4] { { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 0 } };
public static List<Island> Islands = new List<Island> { new Island(2), new Island(3), new Island(1), new Island(4), new Island(2), new Island(2), new Island(1) };
static void Main(string[] args)
{
Program.PrintLayer(Program.TeuLayer1);
Program.PrintLayer(Program.TeuLayer2);
Program.PrintIsland(Program.Islands);
Console.ReadKey();
}
public class Island
{
public int S8s4collection { get; private set; }
public Island(int s8s)
{
S8s4collection = s8s;
}
}
private static void PrintIsland(IEnumerable<Island> islands)
{
var index = 0;
foreach (var island in islands)
{
Console.WriteLine("Island {0} has a 'S8s4collection' of: {1}", index, island.S8s4collection);
index++;
}
Console.WriteLine();
}
private static void PrintLayer(int[,] layer)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
Console.Write(layer[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
}
}
Although having everything as static isn't ideal. Oh and your Island constructor was wrong.
Output:
Upvotes: 2
Reputation: 962
besides the problems with the islands class gilliduck mentioned, it seems. like you just want an array of islands.
public static Island[] is = new Island[7];
public static Island is[1] = new Island(2);
...
then if you wamt to loop through
for(int i=0;i<is.Length;i++) {
is[i].DoSomething () ;
}
Upvotes: 1