sᴜʀᴇsʜ ᴀᴛᴛᴀ sᴜʀᴇsʜ ᴀᴛᴛᴀ - 2 months ago 14
Java Question

Iterating two lists same time which have different size

I have two list X,Y.

Which are lists of Strings .

there is a possibility to have the two lists different sizes .

If both lists are of same size of 2 then i can procedd like

for (int i =0; i<anyList.size(); i++){
system.out.printLn(X(i) +" "+Y(i));
}

example result :

stringX1 stringY1
stringX2 stringY2


how can i handle the loop which have different sizes

example result should be look like this

example result :

stringX1 stringY1
stringX2 stringY2
stringX3
stringX4

Answer
for (int i =0;  i<max(X.size(),Y.size()); i++){
  if(i<X.size() && i<Y.size()) {
    print(X.get(i) + " " + Y.get(i));
  } else if(i<Y.size()) {
    print(Y.get(i));
  } else {
    print(X.get(i));
  }
}

Programming a max(int, int) and print(String) method shouldn't be to hard.