0

I have been trying to no avail to find a solution to this thing i am trying to do. What i am trying to do is to check if the String superConcept is equal to the first element of a line within the kb, and if so, it is checked if any of the remaining Arraylists in the list contain the same first element.

public static List<ArrayList<String>> kb = new ArrayList<>();

for(ArrayList<String> line : kb){
    if(superConcept.equals(line.get(0))){
        //to check if there are any other lines within the kb that contain the same first element (line.get(0))
    }
}
3
  • 1
    What is the problem you are having? Commented Apr 17, 2020 at 12:27
  • So you want to figure out which arraylists contain superConcept as their first element? Commented Apr 17, 2020 at 12:27
  • yes, within that loop Commented Apr 17, 2020 at 12:31

1 Answer 1

2

Do it like this:

List<ArrayList<String>> targetLists = new ArrayList<>();

for (ArrayList<String> list : kb)
    if (list.size() > 0 && list.get(0).equals(superConcept))
        targetLists.add(list);
Sign up to request clarification or add additional context in comments.

2 Comments

how would i go about clearing everything in targetList after i am done with the iteration, so that i would have targetLists fresh when the next iteration starts?
What iteration? Just create a new one everytime you call this piece of code, or, if you want to make it a global variable, call targetLists.clear() before the loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.