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?

7
  • 1
    What language is this? Commented Feb 27, 2013 at 21:26
  • From the code snippet I would guess C#.... Commented Feb 27, 2013 at 21:32
  • I know that in MATLAB when we have a matrix like A(6,6) we can use A(1,:) and it means a column of that matrix. I am looking for some thing like this. Commented Feb 27, 2013 at 21:48
  • 1
    stackoverflow.com/questions/6705583/indexers-in-list-vs-array Commented Mar 7, 2013 at 20:51
  • 2
    something like Strip[][] listOfStripList Commented Jan 16, 2015 at 22:09

2 Answers 2

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;}  
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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();

2 Comments

Not exactly. T[] and List<T> both allow the syntax obj[index], yes, but for different reasons, and with no relation to the fact that they implement IEnumerable<T>.
Ah.. That was just an assumption. I guess I should look stuff like that up. I'll edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.