1

Here is my problem: I have an object "Strip" and I need to have a list or array of these strips "stripList" and after that I need to have a list from different stripList that I called it "listOfStripList". I know that I can save the in this way:

List<List<Strip>> listOfStripList=new List<List<Strip>>();

the reason that I want to have this objects in this way is because in each time I want to have access to the each stripList without using For Loop. For example I want to say listOfStripList[1] and this related to the first list of strips.

Is there any way to define these list by Array?

Jeff B
  • 8,572
  • 17
  • 61
  • 140

2 Answers2

0

List<T> and T[] both allow the use of an indexer (aka the [] operator). So you could just use your list like the following:

List<Strip> firstList = listOfStripList[0];

Although, if you must have it as an array, you could do something like this:

List<Strip>[] arrayOfListStrip = listOfStripList.ToArray();
Cameron
  • 2,574
  • 22
  • 37
  • Not exactly. `T[]` and `List` both allow the syntax `obj[index]`, yes, but for different reasons, and with no relation to the fact that they implement `IEnumerable`. – Jeppe Stig Nielsen Jan 16 '15 at 22:18
  • Ah.. That was just an assumption. I guess I should look stuff like that up. I'll edit. – Cameron Jan 16 '15 at 22:20
0

listOfStripList[0] would give you a List<Strip> object. Calling listOfStripList[0][0] should give you the first item in the first list in listOfStripList

Here's a fiddle: https://dotnetfiddle.net/XdDggB

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        List<List<Strip>> listOfStripLists = new List<List<Strip>>();

        for(int j = 65; j < 100; j++){

            List<Strip> stripList = new List<Strip>();

            for(int i = 0; i < 10; i++){
                stripList.Add(new Strip(){myval = ((char)j).ToString() + i.ToString()});
            }

            listOfStripLists.Add(stripList);
        }// end list of list

        Console.WriteLine(listOfStripLists[0][1].myval);
    }


    public class Strip
    {
        public string myval {get;set;}  
    }
}
ps2goat
  • 8,067
  • 1
  • 35
  • 68