Search code examples
javaarraysstring-comparison

I'm trying to iterate through two arrays in Java, while also checking to see if the values are equal


I am trying to iterate through many arrays, two at a time. They contain upwards of ten-thousand entries each, including the source. In which I am trying to assign each word to either a noun, verb, adjective, or adverb.

I can't seem to figure a way to compare two arrays without writing an if else statement thousands of times.

I searched on Google and SO for similar issues. I couldn't find anything to move me forward.

package wcs;

import dictionaryReader.dicReader;
import sourceReader.sourceReader;

public class Assigner {
    private static String source[], snArray[], svArray[], sadvArray[], sadjArray[];
    private static String nArray[], vArray[], advArray[], adjArray[];
    private static boolean finished = false;

public static void sourceAssign() {
    sourceReader srcRead = new sourceReader();
    //dicReader dic = new dicReader();
    String[] nArray = dicReader.getnArray(), vArray = dicReader.getvArray(), advArray = dicReader.getAdvArray(),
            adjArray = dicReader.getAdjArray();

    String source[] = srcRead.getSource();

    // Noun Store
    for (int i = 0; i < source.length; i++) {
        if (source[i] == dicReader.getnArray()[i]) {
            source[i] = dicReader.getnArray()[i];               
        }else{

        }
    }
    // Verb Store

    // Adverb Store

    // Adjective Store
}
}

Solution

  • Basically this is a simpler way to get a list of items that are in both Lists

    // construct a list of item for first list
            List<String> firstList = new ArrayList<>(Arrays.asList(new String[0])); // add items
            //this function will only keep items in `firstList` if the value is in both lists
            firstList.retainAll(Arrays.asList(new String[0]));
    
            // iterate to do your work
            for(String val:firstList) {
    
            }