簡體   English   中英

C#在包含類數組的類數組中搜索字符串

[英]C# Search for string in an array of classes that include arrays of classes

我正在學習 C# 課程,所以這個問題比現實世界更具學術性。

我想在包含一個或多個附屬(嵌入式)類數組的類數組中搜索字符串。 我希望能夠搜索父數組和子數組。 我一直在嘗試 NET Framework 類庫數組方法,但一無所獲——你會看到我的 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() {}
        }
    }
}

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

我想我終於做到了,這花了我很多時間,因為我是新手..所以,

1)首先,我在每個類中添加了兩個字符串列表,例如:

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

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

然后您需要使用屬性的“Set”訪問器將所有屬性添加到這些列表中:

 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 的索引,如果searchword 在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]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM