2014-12-02 68 views
-4

我有一个人为一个人添加一些数据的人。从第二类ListOfP中,我可以在每个添加的新人后显示数据。现在我需要一些帮助,将每个新人都列入清单,并能够显示清单中的每个人及其所有详细信息。任何帮助,将不胜感激。如何制作一个包含列表的类的列表?

class Person 
{ 
    public List<string> PhoneNbrList = new List<string>(); 
    public List<string> GetListOfListOfP() { return PhoneNbrList; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 

    public void AddNewPerson() 
    { 
     PhoneNbrList = new List<string>(); 
     int i = 1; 
     string str = ""; 
     bool stop = false; 

     do 
     { 
      Console.Write("PhoneNbr [" + i + "]: "); 
      str = Console.ReadLine(); 
      if (str == "n") 
       stop = true; 
      else 
       PhoneNbrList.Add(str); 
      i++; 
     } while (!stop); 
     Console.Write("First name: "); 
     FirstName = Console.ReadLine(); 
     Console.Write("Last name: "); 
     LastName = Console.ReadLine(); 

    } 

    public void ShowPersonData() 
    { 
     for (int i = 0; i < PhoneNbrList.Count; i++) 
      Console.WriteLine("PhoneNbr[" + (i + 1) + "]: " + PhoneNbrList[i]); 

     Console.WriteLine("First name: " + FirstName); 
     Console.WriteLine("Last name: " + LastName); 
    } 
} 


This is the second class 

class ListOfP 
    { 
     static List<Person> ListP = new List<Person>(); 

     static void Main(string[] args) 
     { 
      Person lp = new Person(); 
      int counter = 1; 

      lp.AddNewPerson(); 
      lp.ShowPersonData(); 
      ListP.Add(lp); 


      lp.AddNewPerson(); 
      lp.ShowPersonData(); 
      ListP.Add(lp); 

      Console.ReadLine(); 
     } 
} 
+3

_“现在我需要一些帮助”_你已经开始了,你已经卡住了?你遇到异常或发生了什么? – 2014-12-02 13:53:49

+1

“列表”的任何问题? – crashmstr 2014-12-02 13:53:53

+1

“从第二级ListOfP中,我可以在每个添加的新人后显示数据”。第二课在哪里?你卡在哪里? – 2014-12-02 13:55:08

回答

2

让你的人上课;

class Person 
{ 
    public List<string> PhoneNbrList = new List<string>(); 
    public List<string> GetListOfListOfP() { return PhoneNbrList; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
} 

然后使用泛型为您需要的人名单;

List<Person> myList = new List<Person>; 
Person x = new Person(); 
myList.Add(x); 

,那么你可以通过这个列表循环 - 或做任何你想做的事情

foreach (Person p in myList) 
{ 
//Do something 
} 
+0

我确实尝试过这样的foreach,并为fisrt和姓氏工作正常,但不知道如何与phonenbr列表相同。这是一个列表,所以有点不同,我不是那个专业版:) – Godriel 2014-12-02 14:15:34

+0

循环通过电话号码列表,你需要说foreach(PhoneNbrList中的字符串s){//做点什么} - 在循环中你可以引用每个电话号码与s在Console.Writeline(s); – 2014-12-02 14:19:33

+0

就是这样。 Ty,我没有意识到列表中需要循环的列表。 – Godriel 2014-12-02 14:26:28

0

至于我也明白了,你需要的是这样的:

UPDATE:

class Person 
{ 
    public Person() 
    { 
     PhoneNbrList = new List<string>(); 
    } 
    public List<string> PhoneNbrList; 
    public List<string> GetListOfListOfP() { return PhoneNbrList; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
} 

List<Person> objPerson = new List<Person>(); 

Person obj1 = new Person(); 
//Initialise obj1 here 
objPerson.Add(obj1); 

Person obj2 = new Person(); 
//Initialise obj2 here 
objPerson.Add(obj2); 

......// Add all the objects to list in same manner 


// Query through list 

foreach(var item in objPerson) 
{ 
    // Access each of the items of list here 
    foreach(var phoneNumbers in item.PhoneNbrList) 
    { 
     **//Access your each phone number here** 
    } 
} 
+0

我确实尝试了这样的foreach,并为fisrt和姓氏工作正常,但不知道如何做phonenbr列表相同。这是一个列表,所以有点不同,我不是那个亲:) – Godriel 2014-12-02 14:17:51

+0

@Godriel:我已经更新了答案,并自己进行了测试。工作正常;请尝试。如果有效,请接受答案。对其他问题的回复。 – Saket 2014-12-03 06:38:25

相关问题