简体   繁体   English

如何在arraylist中搜索元素?

[英]How to search an element in arraylist?

Example is I want to search code A25 and A29 then it will print it? 例如我想搜索代码A25和A29那么它会​​打印出来吗? I cant seems find the syntax for searching long words in arraylist. 我似乎找不到在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
   //

您最好创建一个类并将该属性(如branch,rollno..etc)添加到该类,并将该类的对象添加到arrayList并进行迭代和比较以查找您正在查找的特定属性。现在很难搜索如此大的字符串和虽然看起来很糟糕。

How about this? 这个怎么样?

String code = scan.nextLine();
for (String s : arrList) {
  if (s.startsWith(code)) {
    System.out.println(s);
    break; //optional, depending on your use case
  }
}

It should output exactly what you want. 它应该输出你想要的。

Try the below code 请尝试以下代码

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"); 
     List <String> searchResult = new ArrayList<String>(); 
               for (String content : arrlist) {
                   if(content.matches("a29")){
                       searchresult.add(content);
                   }
               }

try this 尝试这个

List <String> codes = new ArrayList<String>();//list of codes to search
codes.add("A25");
codes.add("A29");
List <String> result = new ArrayList<String>();//list of search results
for(String s:arrList)
    for(String code:codes)
        if (s.startsWith(code))
            result.add(s);
for(String s:result)
     System.out.println(s)

Instead of iterating through all the array list for every input search code, you can create one hashmap to store key, value pair as follows : 您可以创建一个散列映射来存储键值对,而不是遍历每个输入搜索代码的所有数组列表,如下所示:

HashMap<String,String> map = new HashMap<String,String>();
map.put("A25","CS 212          Data Structures         3");
map.put("A26","IT 312          Data Base Management System 2   3");
.....
.......

Now, it is really easy to get the element by code . 现在,通过code获取元素非常容易。 Use HashMap#get(key) method 使用HashMap#get(key)方法

String code = scan.nextLine();
System.out.println(code + " " + map.get(code));

This is your desired output. 这是您想要的输出。

But, I would say @Task's answer is the correct one. 但是,我会说@ Task的答案是正确的。

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

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