比较两种类型的Arraylist和return id

时间:2017-03-16 04:44:29

标签: java android arraylist collections

我有两个Arraylist。 ArrayList workerlist和Arraylist workernamelist

public class Workers {
    private String worker_id;
    private String worker_name;


    public String getWorker_id() {
        return worker_id;
    }

    public void setWorker_id(String worker_id) {
        this.worker_id = worker_id;
    }

    public String getWorker_name() {
        return worker_name;
    }

    public void setWorker_name(String worker_name) {
        this.worker_name = worker_name;
    }


}

Arraylist workernamelist列表包含字符串中的所有工作者名称。 我想通过ArrayList workerlist搜索它包含worklist中的wokrername,如果它与worker名称匹配,那么它将返回相应的worker ID。

5 个答案:

答案 0 :(得分:1)

我认为我们只需要遍历整个worker列表,并且对于每个worker,将它的名称与给定的worker_name进行比较,如果它们相等,则返回worker的id。如果此类worker不存在或worker_name为null,则可以返回null。

public String findWorkerId(ArrayList<Workers> workerList, String workerName) {
    if (workerList == null || workerName == null) {
        return null;
    }
    for (Workers worker : workerList) {
        if (workerName.equals(worker.getWorker_name())) {
            return worker.getWorker_id();
        }
    }
    return null;
}

答案 1 :(得分:0)

public Workers searchWorkers(ArrayList workers,String worker_name){
 int len = workers.size();
 for(int i = 0; i<len; i++){
  your logic


    enter code here

 }
}

答案 2 :(得分:0)

你需要一个for循环来查看数组列表,然后如果有任何匹配显示匹配或你想做什么。

for(int i =0: i< workernamelist.size(); i++){
   if(workernamelist.worker_ID = worker_ID){
      system.out.println(workerlist.worker_ID);
   }
}

答案 3 :(得分:0)

试试这个,     public String searchs(ArrayList list_workers,String worker_name){      for(int i = 0; i&lt; list_workers.size(); i ++){         如果(list_workers.get(ⅰ).getWorker_name.equals(WORKER_NAME)         {             String worker_id = list_workers.get(i).getWorker_id();             Log.d( “TAG”, “worker_id:” + worker_id);             return worker_id;         }       }       return null;     }

答案 4 :(得分:0)

您可以这样做:

        ArrayList<String> workernamelist=new ArrayList<>();
        ArrayList<Workers> workersArrayList=new ArrayList<>();
        for(int i=0;i<workernamelist.size();i++)
        {
            for(int j=0;j<workersArrayList.size();j++) {
                if (workernamelist.get(i).equalsIgnoreCase(workersArrayList.get(j).getWorker_name()))
                {
                    String id=workersArrayList.get(j).getWorker_id();
                }
            }
        }