1

这是我的问题:我有一个对象“Strip”,我需要这些条带“stripList”的列表或数组,之后我需要有一个来自不同stripList的列表,我称之为“listOfStripList”。我知道我可以用这种方式保存:

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

我想以这种方式拥有这些对象的原因是因为每次我都想在不使用 For Loop 的情况下访问每个 stripList。例如,我想说 listOfStripList[1] 这与第一个条带列表有关。

有没有办法通过数组来定义这些列表?

4

2 回答 2

0

List<T>并且T[]两者都允许使用索引器(又名[]操作员)。所以你可以像下面这样使用你的列表:

List<Strip> firstList = listOfStripList[0];

虽然,如果你必须将它作为一个数组,你可以这样做:

List<Strip>[] arrayOfListStrip = listOfStripList.ToArray();
于 2015-01-16T22:12:29.067 回答
0

listOfStripList[0]会给你一个List<Strip> 对象。调用listOfStripList[0][0]应该给你第一个列表中的第一个项目listOfStripList

这是一个小提琴: 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;}  
    }
}
于 2015-01-16T22:20:18.217 回答