[Solved] for each nested for each


You shouldn’t use nested for-each loops here.

Try an indexed for on the listMain and store the result of each iteration in a kind of tuple. This tuple could be stored in a Map. As your friend mentioned, you could use a HashMap for this.
Something like this should do the trick:

    ArrayList<Main>listMain = mainDAO.getlAllMain(Connection con);
    Map<Integer, Pair<A, B>> map = new Hashmap<>();

    for (int i = 0; i < listMain.size(); i++) {
       ArrayList<A> listA = aDAO.getallbyMainId(Connection con, int main);
       ArrayList<B> listB = bDAO.getallbyMainId(Connection con, int main);
       A aValue = listA.get(i);
       B bValue = listB.get(i);
       map.put(i, new Pair(aValue, bValue));
    }

I’m using the apache-commons class Pair as a tuple here. See below to get more information about tuples in java. You could for example use a custom class for this purpose.

It is also possible to change the type of the pair content and the map key to be a String depending on the information u want to use later.

I hope i understood your problem correctly and that this answer is helping you.

See also

A Java collection of value pairs? (tuples?)

solved for each nested for each