sᴜʀᴇsʜ ᴀᴛᴛᴀ sᴜʀᴇsʜ ᴀᴛᴛᴀ - 1 year ago 69
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

Answer Source
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()) {
  } else {

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download