简体   繁体   中英

Search element in arraylist

How can I search element in arraylist and display it? 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.

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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