简体   繁体   中英

Compare arrays java

I have four arrays as follow.

  1. Main String Array - contains the words of a main string

  2. User String Array - contains the words of the user string

  3. Missing words Array - contains the missing words of the user string

  4. Value Array - contains the value of each word in the user string array

I have another empty array to assign new values. What I am doing is as below.

  1. Compare each element in the main string with the corresponding element in the user string.
  2. If the two elements are matching assign the corresponding element of value array to the new value array.
  3. Else check whether the element is matching with the missing words array.
  4. If it matches assign "0" to the corresponding element in the new value array.

What I have done up to now is given below.

    for (int i = 0; i < mainStringArray.length; i++) {

        if (userStringArray[i].equals(mainWordsArray[i])){
           newvalueArray[i] = valueArray[i];
        }
        else {
            if (mainStringArray[i].equals(missingWordsArray[i])){
                newValueArray[i] = 0;
            }
        }

        System.out.println(mainStringArray[i] + " " + newValueArray[i]);
    }


  }

But I get an ArrayIndexOutOfBoundsException because the arrays are not same size. Please tell me a way to avoid this.

In a short way what I want is , I have a main string and a user string. Each word in the user string has a corresponding value and it is stored in the value array. I want to add those values to the words in the main string which can be found in the user string and 0 to the missing words.

its a very simple solution: since you know that userStringArray is always gonna be lesser than or equal to the mainStringArray , you do the for loop over the userStringArray . you then need to take note of the length of the userStringArray using userStringArray.length and use that length value to get the part of the mainStringArray which has no corresponding elements in the userStringArray and proceed to do whatever you want to with these extra characters.

you may choose to use ArrayLists instead of arrays, but you will have to change your loop to account for removing values from an empty ArrayList by checking the isEmpty() condition. the rest of the logic can be the same.

EDIT:

this question is very ambiguous. as such, i shall roughly show you how to do the same thing as you wrote except with ArrayLists. the only tangible difference to the code is the checking of userStringArray.isEmpty().

    import java.util.ArrayList;
    ArrayList<Double> newValueArray = new ArrayList();
    ArrayList<Double> userStringArray = new ArrayList();
    ArrayList<Double> mainStringArray = new ArrayList();

    for (int i = 0; i < mainStringArray.size(); i++) {
        if(!userStringArray.isEmpty){
           if (userStringArray.get(i).equals(mainWordsArray.get(i))){
              newvalueArray.remove(i);
              newvalueArray.add(i, valueArray.get(i));                
           } else {
              if (mainStringArray.get(i).equals(missingWordsArray.get(i))){
                 newValueArray.remove(i);
                 newValueArray.add(i, 0);
               }
           }

           System.out.println(mainStringArray[i] + " " + newValueArray.get(i));
        }


     }

According to this case all the four arrays must be of the same size. So when you declare the arrays, simply make all their size as same. That will do.

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