0

If it was up to me, I would loop a Java for loop with multiple lists this way:

for (String s1, s2, s3 : arrayOfString1, arrayOfString2, arrayOfString3)
  {
    System.out.println("arrayOfString1" + s1);
    System.out.println("arrayOfString2" + s2);
    System.out.println("arrayOfString3" + s3);
  }

Sadly, it isn’t up to me, how would I go about looping a for statement with multiple lists?

4
  • Do it separately in one thread, or having three threads? Commented Mar 13, 2015 at 11:24
  • are lists of the same size? Commented Mar 13, 2015 at 11:31
  • @xcoder I need to do it simultaneously because all 3 results will later be passed within the same "for loop" ,also note that each s(number) is a list of strings so the loop will put each string in a new line BUT each string of the 3 lists in the same line. Commented Mar 13, 2015 at 11:34
  • then I'd use the approach from Turtle Commented Mar 13, 2015 at 12:09

2 Answers 2

5

I guess you made the lengths of all the arrays the same. The solution using index is:

for (int i=0;i<arrayOfString1.length();++i) {
    System.out.println("arrayOfString1" + arrayOfString1.get(i));
    System.out.println("arrayOfString2" + arrayOfString2.get(i));
    System.out.println("arrayOfString3" + arrayOfString3.get(i));
}

If you want to use iterator, do as below:(I only print two arrays for example.)

for(Iterator<int> i1=arr1.iterator(),i2=arr2.iterator();i1.hasNext();){
    int val1= i1.next();
    int val2= i2.next();
}
Sign up to request clarification or add additional context in comments.

4 Comments

I went with the first option but encountered this: Cannot invoke length() on the array type String[], let me quickly convert it and will let you know
After converting String[] to String the error "The method get(int) is undefined for the type String" is returned, also i is an int, get(i) will return an int, I want to return the strings in the array not an integer or their lengths
If it is a array, use arr.length and arr[i]
thanks for your time, I didn't know an array would be treated differently for this specific loop given that they contained strings, but now i know.
2

try this for lists with different sizes:

int len = arrayOfString1.size();
len = Math.max(len, arrayOfString2.size());
len = Math.max(len, arrayOfString3.size());
for (int i = 0; i < len; i++) {
    if (i < arrayOfString1.size()) System.out.println("arrayOfString1" + arrayOfString1.get(i));
    if (i < arrayOfString2.size()) System.out.println("arrayOfString2" + arrayOfString2.get(i));
    if (i < arrayOfString3.size()) System.out.println("arrayOfString3" + arrayOfString3.get(i));
}

and if you used Arrays, not lists:

int len = arrayOfString1.length;
len = Math.max(len, arrayOfString2.length);
len = Math.max(len, arrayOfString3.length);
for (int i = 0; i < len; i++) {
    if (i < arrayOfString1.length) System.out.println("arrayOfString1" + arrayOfString1[i]);
    if (i < arrayOfString2.length) System.out.println("arrayOfString2" + arrayOfString2[i]);
    if (i < arrayOfString3.length) System.out.println("arrayOfString3" + arrayOfString3[i]);
}

1 Comment

Thank you, your second method worked!! I'm using an Array. Thank you to all who responded.

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.