2016-05-08 7 views
1

私はC#コースを進んでいますので、この質問は現実世界よりも学問的です。C#クラスの配列を含むクラスの配列内の文字列を検索します。

私は、クラスの1つまたは複数の補助(埋め込み)配列を含むクラスの配列内の文字列を検索したいと考えています。私は、親配列と補助配列の両方を検索できるようにしたい。私はNET Frameworkクラスライブラリ配列メソッドを試してきましたが、どこにもいません - 私のArray.IndexOfが-1を返します。私は下に私のコードを貼り付け、任意のアドバイスに感謝します。私はこれを行うためのより洗練された方法があることを知っているが、私はしばらくの間、配列にこだわる必要があります。前もって感謝します。

私は最終的にそれをやってきた私は初心者だから、それが最初)、..だから、私が内の文字列の二つのリストを追加したすべてを

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

などの各クラス :

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

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

、その後必要になりますあなたは、アレイ内のすべてのオブジェクト内のリストがあるでしょう、この段階でデバッグを行う場合

public string StudentName 
       { 
       get 
        { 
        return studentName; 
        } 

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

は、含まれています:プロパティのアクセサを「設定」使用することにより、これらのリストにすべてのあなたのpropertysを追加する必要がありますObjectのすべてのプロパティ。

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

3)今、私たちはループができます:

2)あなたのインデックスが、それはもっとして1つの番号が含まれていますので、配列でなければなりません。

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型私は、アレイ内の学生の指標である

int型のxであるStudentSubjectのインデックス、searchwordは、学生オブジェクトの内部にある場合と、 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"を返します。あなたのコードがちょうど落ちるからです。 私は.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

関連する問題