如何从数组列表中的对象中提取特定数据 [英] How to pull specific data from an object in an arraylist

查看:88
本文介绍了如何从数组列表中的对象中提取特定数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,不确定标题的措辞是否正确,但说我有三个对象,每个对象有两个数据字符串(假设一个 stateName 和一个 cityName)存储在一个数组列表中.是否可以将状态名称作为字符串变量输入(称为 searchStateName),然后使用方法搜索列表中的对象并将 searchStateName 与数组列表中每个对象内的 stateName 进行比较,然后找到匹配项是否返回与匹配 stateName 位于同一对象内的 cityName?

Ok, not sure if the title worded that correctly but say I have three objects with two strings of data each(Lets say a stateName and a cityName) stored into an arraylist. Is it possible to enter in a state name in as a string variable(call it searchStateName), then have a method search through the objects in the list and compare searchStateName to the stateName inside each object in the arraylist, and when a match is found have it return the cityName that is inside the same object as the matching stateName?

我目前正在尝试做类似的事情,并想自己解决这个问题,但过去一个小时我一直在努力解决这个问题,但完全迷失了方向.有人能指出我正确的方向吗?

I am trying to do something similar currently and would like to figure it out myself, but I have been working on this for the past hour and am completely lost. Could anyone point me in the right direction?

推荐答案

这就是方法.假设您有一个名为 SC 的类,其中有两个实例变量以及它们的 getter 和 setter.您需要在 main 中初始化它们的 3 个对象:

This is the approach. Say you have a class called SC and you have two instance variables in it and getters and setters for them. You need to initialize 3 objects of them inside your main:

SC a = new SC("Illinois ","Chicago ");
SC b = new SC("Michigan ","Detroit");
SC c = new SC("New York"," New York");

然后创建对象SCArrayList并将SC的那3个对象添加到该ArrayList中.

And then create an ArrayList of object SC and add those 3 objects of SC to that ArrayList.

ArrayList<SC> list = new ArrayList<SC>();
list.add(a);
list.add(b);
list.add(c);

现在调用搜索州名的方法,如果找到则返回城市名:

Now call the method that search for a state name and returns the city name if it finds it:

this.searchStateName("Illinois");

这个方法的实际实现与for-each循环:

The actual implementation of this method with for-each loop:

public String searchStateName(String stateName){
 String result = "No city found.";
      for (SC sc : list )
         if (sc.getStateName().equalsIgnoreCase(stateName)){
          result = sc.getCityName();
          break;
        }
    }
 return result;
}

如果您对 for-each 循环不满意,那么您可以使用下面的常规 for 循环.我是故意评论的.但它会正常工作并为您提供正确的城市名称.

If you are not comfortable with for-each loop then you can use the regular for loop below. I commented out on purpose. But it will work correctly and give you the correct city name.

//public String searchStateName(String stateName){
// String result = null;
//      for (int i = 0; i < list.size(); i++){
//         if (list.get(i).getStateName() .equalsIgnoreCase(stateName)){
//          result = list.get(i).getCityName();
//          break;
//        }
//    }
// return result;
//}

就是这样.如果您需要进一步帮助,请告诉我.

And thats it. Let me know if you need help further.

这篇关于如何从数组列表中的对象中提取特定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆