2016-05-08 51 views
1

我正在通過C#課程工作,所以此問題比現實世界更具學術性。C#在包含類的數組的類的數組中搜索字符串

我想在包含一個或多個子類(嵌入式)數組的類的數組中搜索字符串。我希望能夠搜索父代和輔助數組。我一直在嘗試.NET框架類庫數組方法,但我無處可去 - 你會看到我的Array.IndexOf返回-1。我在下面粘貼了我的代碼,並會很感激任何建議。我知道有更復雜的方法可以做到這一點,但我需要暫時堅持使用陣列。提前致謝。

using System; 

namespace Nested_Arrays 
{ 
    public class Program 
    { 
     static void Main(string[] args) 
     { 
      Student[] StudentArray = new Student[3]; 
      StudentSubjects[] StudentSubjectsArray = new StudentSubjects[3]; 

      StudentArray[0] = new Student() { 
       StudentName = "Peter", StudentLocation = "Australia" 
      }; 
      StudentArray[0].StudentSubjectsArray[0] = new StudentSubjects() { 
       SubjectName = "Calculus", StudentsResult = "Pass" 
      }; 
      StudentArray[0].StudentSubjectsArray[1] = new StudentSubjects() { 
       SubjectName = "Algebra", StudentsResult = "Pass" 
      }; 
      StudentArray[0].StudentSubjectsArray[2] = new StudentSubjects() { 
       SubjectName = "Statistics", StudentsResult = "Pass" 
      }; 
      StudentArray[1] = new Student() { 
       StudentName = "Michelle", StudentLocation = "France" 
      }; 
      StudentArray[1].StudentSubjectsArray[0] = new StudentSubjects() { 
       SubjectName = "Engineering", StudentsResult = "Pass" 
      }; 
      StudentArray[1].StudentSubjectsArray[1] = new StudentSubjects() { 
       SubjectName = "Algebra", StudentsResult = "Pass" 
      }; 
      StudentArray[1].StudentSubjectsArray[2] = new StudentSubjects() { 
       SubjectName = "Aramaic", StudentsResult = "Pass" 
      }; 
      StudentArray[2] = new Student() { 
       StudentName = "Mitchell", StudentLocation = "Canada" 
      }; 
      StudentArray[2].StudentSubjectsArray[0] = new StudentSubjects() { 
       SubjectName = "Engineering", StudentsResult = "Pass" 
      }; 
      StudentArray[2].StudentSubjectsArray[1] = new StudentSubjects() { 
       SubjectName = "Greek", StudentsResult = "Pass" 
      }; 
      StudentArray[2].StudentSubjectsArray[2] = new StudentSubjects() { 
       SubjectName = "Aramaic", StudentsResult = "Pass" 
      }; 

      for (int i = 0; i < 3; i++) 
      { 
       Console.WriteLine($ "\n{i + 1,3} {StudentArray[i].StudentName,-10} {StudentArray[i].StudentLocation,-15}"); 
       for (int j = 0; j < 3; j++) { 
        Console.WriteLine($ "{j + 1,6} {StudentArray[i].StudentSubjectsArray[j].SubjectName,-15} {StudentArray[i].StudentSubjectsArray[j].StudentsResult,-10}"); 
       } 
      } 
      String searchString = "Mitchell"; 
      Console.WriteLine($ "\n We are searching for \"{searchString}\""); 
      int index = Array.IndexOf(StudentArray, searchString); 
      Console.WriteLine(" The first occurrence of \"{0}\" is at index {1}.", searchString, index); 
      searchString = "Aramaic"; 
      Console.WriteLine($ "\n We are searching for \"{searchString}\""); 
      index = Array.IndexOf(StudentSubjectsArray, searchString); 
      Console.WriteLine(" The first occurrence of \"{0}\" is at index {1}.", searchString, index); 
      Console.WriteLine($ "\n"); 
     } 

     public class Student 
     { 
      private string studentName; 
      public string StudentName { 
       get { 
        return studentName; 
       } 
       set { 
        studentName = value; 
       } 
      } 

      private string studentLocation; 
      public string StudentLocation { 
       get { 
        return studentLocation; 
       } 
       set { 
        studentLocation = value; 
       } 
      } 

      private StudentSubjects[] studentSubjectsArray = new StudentSubjects[3]; 
      public StudentSubjects[] StudentSubjectsArray { 
       get { 
        return studentSubjectsArray; 
       } 
       set { 
        studentSubjectsArray = value; 
       } 
      } 

      //Constructor 
      public Student() {} 
     } 

     public class StudentSubjects { 
      private string subjectName; 
      public string SubjectName { 
       get { 
        return subjectName; 
       } 
       set { 
        subjectName = value; 
       } 
      } 

      private string studentsResult; 
      public string StudentsResult { 
       get { 
        return studentsResult; 
       } 
       set { 
        studentsResult = value; 
       } 
      } 

      //Constructor 
      public StudentSubjects() {} 
     } 
    } 
} 

回答

0

我認爲,我終於做到了,我花了一個詭計,因爲我是一個新手.. 所以,

1)首先關閉所有我添加字符串的兩個列表每個類 像:

 public class StudentSubjects 
      { 
       public List<String> studentsubjectlist = new List<string>(); 

public class StudentSubjects 
    { 
    public List<String> studentsubjectlist = new List<string>(); 

,那麼你將需要將所有propertys添加到這些列表,通過使用「設置」屬性的訪問:

public string StudentName 
       { 
       get 
        { 
        return studentName; 
        } 

       set 
       { 
        studentName = value; 
        studentslist.Add(value); 
       } 
      } 

如果在這個階段,你會看到有在數組中的每個對象名單做調試,包含對象的所有屬性。

2)你的指數已是一個數組,因爲它將包含一個以上的號碼:

int[] index = new int[StudentArray.Length]; 

3)現在,我們可以循環。

int i; 
    int j; 
    int x; 

    for (i = 0; i < StudentArray.Length; i++) 
    { 

     for (j = 0; j < StudentArray[i].studentslist.Count; j++) 

     { 

      if (StudentArray[i].studentslist[j] == searchString) 
      { 
       index[0] = i; 
       index[1] = -1; 
       index[2] = j; 
       break; 
      } 
     } 

     for (x = 0, j = 0; j < StudentArray[i].StudentSubjectsArray[x].studentsubjectlist.Count; j++) 
     { 

      if (StudentArray[i].StudentSubjectsArray[x].studentsubjectlist[j] == searchString) 

      { 
       index[0] = i; 
       index[1] = x; 
       index[2] = j; 
       break; 
      } 

    else if (j == StudentArray[i].StudentSubjectsArray [x].studentsubjectlist.Count)   
      { 
       x++; 
       j = 0; 
      } 
     } 
    } 

4)注意的是:

INT I是在該陣列的學生的索引,

INT x是StudentSubject的索引,如果搜索內容是Student對象的內部和不在StudentSubject中,x將返回-1。

int j是List內屬性的索引。

5)你還需要將你的Console.Writeline更改爲:

Console.WriteLine("\n We are searching for \"{searchString}\""); 
    Console.WriteLine(" The first occurrence of \"{0}\" is at index {1}.{2}. {3}.", searchString, index[0], index[1], index[2]); 
+0

感謝羅迪。 StudentArray [i] .GetType()。ToString()返回「TestingForumResponses。程序+學生「,所以你的ifs不會觸發,你的代碼只能通過。 我不太清楚如何使用.GetType - 有什麼建議嗎? – Joe

+0

我已經完全改變了解決方案,現在它應該可以工作。 –

1

Array.IndexOf搜索指定的對象,並返回其第一次出現的指數在一維數組。

在你的情況下,你需要搜索對象屬性並找到匹配對象的索引。我建議使用使用Linq來搜索匹配屬性值的對象,然後搜索對象的索引(如下所示)。

var item = StudentArray.FirstOrDefault(x=> x.StudentName.Equals(searchString, StringComparison.CurrentCultureIgnoreCase) 
             || x.StudentSubjectsArray.Any(s=>s.SubjectName.Equals(searchString, StringComparison.CurrentCultureIgnoreCase))); 

if(item!= null) 
    index = Array.IndexOf(StudentArray, item); 

檢查該工作demo