简体   繁体   中英

Get index of the same value from two arraylist with two different sizes/length

I am new to Java and I want to get the index of same value of two arraylist however the two arraylist do NOT have the same size/length. This is the working code:

List<String> new_valueString = new ArrayList<>();
    List<String> new_fieldsString = new ArrayList<>();
    List<String> oldValueString = new ArrayList<>();
    List<String> oldFieldsString = new ArrayList<>();

  if (new_fieldsString.size() != 0 && new_valueString.size() != 0) {
        for (int l = 0; l < new_fieldsString.size(); l++) {
                if (new_fieldsString.get(l).equalsIgnoreCase(oldFieldsString.get(l))) {
                    field_index = l;
                    if (!new_valueString.get(field_index).contains(oldValueString.get(field_index))) {
                        ifUpdated = true;
                    } else {
                        ifUpdated = false;
                    }
                }
            }

    }

I want to get the index of the same value between new_fieldsString and oldFieldsString and use that index to compare the new_valueString and oldValueString.

Hello You need a two for sentences one for your first array and second to compare with the short arraylist and when you find your condition true not forget break the loop, some like this:

List<String> new_valueString = new ArrayList<>();
    List<String> new_fieldsString = new ArrayList<>();
    List<String> oldValueString = new ArrayList<>();
    List<String> oldFieldsString = new ArrayList<>();

  if (new_fieldsString.size() != 0 && new_valueString.size() != 0) {
        for (int l = 0; l < new_fieldsString.size(); l++) {
           for (int m = 0; m < oldFieldsString.size(); m++) {
                if (new_fieldsString.get(l).equalsIgnoreCase(oldFieldsString.get(m))) {
                    if (!new_valueString.get(l).contains(oldValueString.get(m))) {
                        ifUpdated = true;
                    } else {
                        ifUpdated = false;
                    }
                    break;
                }
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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