关于c#:如何访问List< int>

How would one access a List<int> as a List<T> object?

本问题已经有最佳答案,请猛点这里访问。

对于这个新问题,我深表歉意,但我很难理解如何访问一个通用列表中的int列表,然后将所有这些信息显示到一个列表视图(winform)中。这是我到目前为止所拥有的…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    }
    List<Student> students;
    public class Student
    {

        public string First { get; set; }

        public string Last { get; set; }

        public List<int> Scores { get; set; }


        public int ID { get; set; }


        public string[] ToListViewItem()
        {
            return new string[] {
                First,
                Last,
                ID.ToString(),
                Scores.ToString()
            };

        }

    }

    protected override void OnLoad(EventArgs e)

    {
        base.OnLoad(e);

        students = new List<Student>


        {

        new Student {First="Svetlana", Last="Omelchenko", ID=111, Scores= new List<int> {97, 92, 81, 60}},

        new Student {First="Claire", Last="O’Donnell", ID=112, Scores= new List<int> {75, 84, 91, 39}},

        new Student {First="Sven", Last="Mortensen", ID=113, Scores= new List<int> {88, 94, 65, 91}},

        new Student {First="Cesar", Last="Garcia", ID=114, Scores= new List<int> {97, 89, 85, 82}},

        new Student {First="Debra", Last="Garcia", ID=115, Scores= new List<int> {35, 72, 91, 70}},

        new Student {First="Fadi", Last="Fakhouri", ID=116, Scores= new List<int> {99, 86, 90, 94}},

        new Student {First="Hanying", Last="Feng", ID=117, Scores= new List<int> {93, 92, 80, 87}},

        new Student {First="Hugo", Last="Garcia", ID=118, Scores= new List<int> {92, 90, 83, 78}},

        new Student {First="Lance", Last="Tucker", ID=119, Scores= new List<int> {68, 79, 88, 92}},

        new Student {First="Terry", Last="Adams", ID=120, Scores= new List<int> {99, 82, 81, 79}},

        new Student {First="Eugene", Last="Zabokritski", ID=121, Scores= new List<int> {96, 85, 91, 60}},

        new Student {First="Michael", Last="Tucker", ID=122, Scores= new List<int> {94, 92, 91, 91} }


    };

        PopulateListView(students);


        GetAverageGrade();

    }

    public void GetAverageGrade()
    {
        foreach (Student s in students)
        {
            double avg = s.Scores.Average();


        }

    }

    public void PopulateListView(List<Student> list)
    {

        listView1.SuspendLayout();

        for (int i = 0; i < list.Count; i++)
        {

            // create a list view item
            var lvi = new ListViewItem(list[i].ToListViewItem());
            // assign class reference to lvi Tag for later use
            lvi.Tag = list[i];
            // add to list view
            listView1.Items.Add(lvi);

            //This adjust the width of 1st column to fit data.
            listView1.AutoResizeColumn(0, ColumnHeaderAutoResizeStyle.ColumnContent);
            listView1.ResumeLayout();
        }


    }
}

到目前为止,我已经能够用名字和学生ID填充listView的前三列,但最后一列中我要做的就是"systems.collections.generic…"等。

任何帮助都会非常感谢,谢谢。


您可能正在使用返回类名的Scores.ToString()方法。你可以试试:用string.Join(",", Scores)代替。这将包含用,分隔的所有值。

1
2
3
4
5
6
7
8
9
public string[] ToListViewItem()
{
    return new string[] {
        First,
        Last,
        ID.ToString(),
        string.Join(",", Scores)
    };
}


这就是Scores.ToString()的回报。

如果要显示有用的信息,需要编写消耗List的代码并返回有用的信息。

先看一下string.Join()