Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search an element in arraylist?

Example is I want to search code A25 and A29 then it will print it? I cant seems find the syntax for searching long words in arraylist.

    ArrayList<String> arrList = new ArrayList<String>();
    arrList.add("A25            CS 212          Data Structures         3");
    arrList.add("A26            IT 312          Data Base Management System 2   3");
    arrList.add("A27            IT 312          Data Base Management System 2   3");
    arrList.add("A28            IT 212          Data Base Management System 2   3");
    arrList.add("A29            CS 313          Digital Design          3");
    arrList.add("A30            IT 212          Discrete Structures     3");
    arrList.add("A25            IT 212          Discrete Structures     3");
    arrList.add("984            Engl 3          Speech and oral Communication   3");
    arrList.add("582            Theo 3          Christ and Sacraments       3");
    arrList.add("470            Stata1          Probablility and Statistics 3");
    arrList.add("999            Dota 2          Dota Guide          3");
    System.out.println("Enter code to search");
    int code = scan.next();
    //Desired output
   Enter code to search: 
   A29
   A30

   A29          CS 313          Digital Design          3
   A30          IT 212          Discrete Structures     3
   //
like image 205
Angela Vinagrera Avatar asked May 06 '26 20:05

Angela Vinagrera


1 Answers

You better create a class and add attributes like branch,rollno..etc to that class and add object of that class to arrayList and iterate and compare to find specific attribute you are looking for.Right now it's hard to search such a big String and looks bad practice though.

like image 189
akash Avatar answered May 09 '26 10:05

akash