Java 在ArrayList中搜索并计数

Java 在ArrayList中搜索并计数,java,arraylist,Java,Arraylist,在这段代码中,我想在ArrayList中搜索,但我的代码返回了不正确的结果,我无法解决此问题 ReceivedItemStructure结构: public class ReceivedItemStructure { public String mLastID; public String mUserID; public String mSmsBody; public String mMobileNumber; public String mDate;

在这段代码中,我想在
ArrayList
中搜索,但我的代码返回了不正确的结果,我无法解决此问题

ReceivedItemStructure
结构:

public class ReceivedItemStructure {
    public String mLastID;
    public String mUserID;
    public String mSmsBody;
    public String mMobileNumber;
    public String mDate;
    public String mSenderName;
    public String mSmsNumber;
    public String mContactName;

public String getmLastID() {
        return mLastID;
    }
}
我的代码:

int countSMS = 0;

String smsReceivedSender = "";
String r = new JsonService(config_username, config_password, 0, 20, G.F_RECEIVE_SMS).request();

JSONArray data_array = new JSONArray(r);

for (int i = 0; i < data_array.length(); i++) {
    JSONObject json_obj = data_array.getJSONObject(i);
    String mId = json_obj.getString("id_recived_sms");

    for (ReceivedItemStructure rf:items){

        if( ! mId.equals(rf.getmLastID()) ) {
            countSMS++;
        }
    }

}

如果
mId=2000
rf.getmLastID()=1000
,则
count
必须是
++
在列表中循环,并执行
包含
启动操作

ArrayList<String> resList = new ArrayList<String>();
String searchString = "man";

for (String curVal : list){
  if (curVal.contains(searchString)){
    resList.add(curVal);
  }
}
ArrayList resList=new ArrayList();
String searchString=“man”;
用于(字符串曲线:列表){
if(曲线包含(搜索字符串)){
resList.add(曲线);
}
}

您可以将其包装在一个方法中。包含检查其是否在列表中。您也可以选择StartWith。

好的,为了澄清问题,请尝试如下方式调试代码:

int countSMS = 0;

String TAG = "Debugger";

String smsReceivedSender = "";
String r = new JsonService(config_username, config_password, 0, 20, G.F_RECEIVE_SMS).request();

JSONArray data_array = new JSONArray(r);
Log.i(TAG, "items size is " + items.size());

for (int i = 0; i < data_array.length(); i++) {
    JSONObject json_obj = data_array.getJSONObject(i);
    String mId = json_obj.getString("id_recived_sms");
    Log.i(TAG, "Trying to compare " + mId);
    for (ReceivedItemStructure rf:items){
        Log.i(TAG, "iteration step of  " + rf.getmLastID);
        if( ! mId.equals(rf.getmLastID()) ) {

            countSMS++;
            Log.i(TAG, "they are equal, count is " + countSMS);
        }
    }

}
int countSMS=0;
String TAG=“调试器”;
字符串smsReceivedSender=“”;
字符串r=newJSONService(配置用户名、配置密码、0、20、G.F_接收短信);
JSONArray数据_数组=新的JSONArray(r);
Log.i(标记“items size is”+items.size());
对于(int i=0;i
您想精确计算多少?你的
项目列表中有什么?你的结果是什么?@NIPHIN
如果
语句不加
countSMS
@A.S.请准确查看我的代码<代码>项目
接收数据结构
我看不到
项目
在哪里被填充,我想你确实在一个空列表上迭代,只是试着记录你的foreach
contains
中的每个迭代在类结构中不像我的类
接收数据结构
那样工作,所以这是正确的行为,你到底想数多少?请告诉我。例如,我在
mId
中有
30354260
,但它不在
receivedItemsStructure
中,那必须是我的日志
find
,但它找不到,如果
不能正确工作,那么为什么不计算匹配项并从大小中减去呢?
int countSMS = 0;

String TAG = "Debugger";

String smsReceivedSender = "";
String r = new JsonService(config_username, config_password, 0, 20, G.F_RECEIVE_SMS).request();

JSONArray data_array = new JSONArray(r);
Log.i(TAG, "items size is " + items.size());

for (int i = 0; i < data_array.length(); i++) {
    JSONObject json_obj = data_array.getJSONObject(i);
    String mId = json_obj.getString("id_recived_sms");
    Log.i(TAG, "Trying to compare " + mId);
    for (ReceivedItemStructure rf:items){
        Log.i(TAG, "iteration step of  " + rf.getmLastID);
        if( ! mId.equals(rf.getmLastID()) ) {

            countSMS++;
            Log.i(TAG, "they are equal, count is " + countSMS);
        }
    }

}