简体   繁体   English

搜索数组列表中的元素

[英]Search element in arraylist

How can I search element in arraylist and display it? 如何在arraylist中搜索元素并显示它? Example is the user wants to search the code A25 Then it will print the whole content on that arraylist that he search only and the output is A25 CS 212 Data Structures 3. 示例是用户想要搜索代码A25,然后它将全部内容打印在他仅搜索的该数组列表上,输出为A25 CS 212数据结构3。

 Subject CS212 = new Subject("A25","\t\tCS 212","\t\tData Structures\t\t\t\t",units);
    Subject IT312 = new Subject("A26","\t\tIT 312","\t\tData Base Management System 2\t\t",units);
    Subject IT313 = new Subject("A27","\t\tIT 312","\t\tData Base Management System 2\t\t",units);
    Subject CS313 = new Subject("A29","\t\tCS 313","\t\tDigital Designt\t\t\t\t",units);
    Subject Disc  = new Subject("A30","\t\tIT 212","\t\tDiscrete Structurest\t\t",units);
    Subject A31 = new Subject("A31","\t\tIT 212","\t\tDiscrete Structurest\t\t",units);
    Subject Engl3 = new Subject("984","\t\tEngl 3","\t\tSpeech and oral Communicationt\t\t",units);
    Subject Theo3 = new Subject("582","\t\tTheo 3","\t\tChrist and Sacramentst\t\t",units);
    Subject Stat = new Subject("470","\t\tStata1","\t\tProbablility and Statisticst\t\t",units);
    Subject Dota = new Subject("999","\t\tDota 2","\t\tDota Guide\t\t\t\t",units);

    ArrayList<Subject> arrList = new ArrayList<Subject>();

    arrList.add(CS212);
     arrList.add(IT312);
     arrList.add(IT313);
     arrList.add(CS313);
     arrList.add(Disc);
     arrList.add(A31);
     arrList.add(Engl3);
     arrList.add(Theo3);
     arrList.add(Stat);
     arrList.add(Dota);
//User input that he wants to search
for(int i = 0; i < 3; i++,num++)
    {   

        System.out.print("\t\t"+num +". ");
        codeNo[i] = scan.next();
        String output = Character.toUpperCase(codeNo[i].charAt(0)) + codeNo[i].substring(1);
        codeNo[i] = output;
    }

        // This is what I tried but it doesn't work Idk why
        for (Subject s : arrList) {

            for(int i =0; i < codeNo.length; i++)

                  if (s.equals(codeNo[i])) {

                    System.out.println("\t\t\t"+s);



                  } 

            }

public Subject(String codeNo, String subjectID, String title , int unit)
{
 //Constructor . .
}
//Desired output
Code to search
A25
A26
A27
output
A25     CS 212      Data Structures         3
A26     IT 312      Data Base Management System 2   3
A27     IT 312      Data Base Management System 2   3

You are trying to search an arraylist of subjects, you need to write a small function to compare the code string to the corresponding string of the class. 您正在尝试搜索主题的数组列表,您需要编写一个小函数以将代码字符串与该类的相应字符串进行比较。 You can do this by adding this to your subject class. 您可以通过将其添加到您的主题课程中来实现。

Example : 范例:

@Override
public boolean equals(String code) {
    return code.equals(this.<compare to member>);
}

and change the compare to member that needs to match the code that you match. 然后将比较对象更改为需要与您匹配的代码匹配的成员。

EDIT : Easier way to do is to just change your existing code to : 编辑:更简单的方法是将现有代码更改为:

if (s.code.equals(codeNo[i])) //assuming your code class member is a public string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM