2

I can make individual strings from a string [] array like this for example :

//first, make stringArray1 the same size as arrayList1
stringArray1 = new String[arrayList1.size()];

//stringArray1 will contain all the values in arrayList1
stringArray1 = arraylist1.toArray(stringArray1);

//for each value in stringArray1 make it into an individual string,
//called string1
            for(String string1: stringArray1 )

            {
                System.out.println("string1 is " + string1);

             }

Can you tell me how I would put another string conversion from string [] array into the same for loop? Both arrayList1 and arrayList2 are exact same size. I thought I would be able to use && but no joy. I'm getting 'Expression expected'. Or do I need to have two different for loops? This is what I have:

//first, make stringArray1 the same size as arrayList1
stringArray1 = new String[arrayList1.size()];

//second, make stringArray2 the same size as arrayList2
stringArray2 = new String[arrayList2.size()];

//stringArray1 will contain all the values in arrayList1
stringArray1 = arraylist1.toArray(stringArray1);

//stringArray2 will contain all the values in arrayList2
stringArray2 = arraylist2.toArray(stringArray2);

//for each value in stringArray1 make it into an individual string,
//called string1. Do likewise for string2
            for(String string1: stringArray1 && String string2: stringArray2)

            {
                System.out.println("string1 is " + string1);
                System.out.println("string2 is " + string2);

             }
4
  • That will not work. Either two for each loops or collect the arrays into one. Commented Sep 5, 2017 at 8:26
  • Also from the point that both arrays can have different sizes. Then the for each loop can't handle that. Commented Sep 5, 2017 at 8:27
  • @MuratK.Thanks for comment, both arrays will be the exact same size. Commented Sep 5, 2017 at 8:31
  • This question has nothing to do with android . I think you should use java tag rather android tag Commented Sep 5, 2017 at 10:09

2 Answers 2

6

You cannot iterate over two arrays at the same time using an enhanced for-loop (foreach loop). That is because such a foreach loop internally uses an Iterator instance to iterate over the elements.

You have several options:

  • Use a simple for loop:

    for (int i = 0; i < arr1.length; i++) {
        System.out.println(arr1[i]);
        System.out.println(arr2[i]);
    }
    

    Of course, you have to guarantee that arr1.length is not greater than arr2.length, otherwise an ArrayIndexOutOfBoundsException is emitted.

    To silently stop if one of the arrays is exhausted, you could just calculate the lowest of the two:

    for (int i = 0; i < Math.min(arr1.length, arr2.length); i++) {
        System.out.println(arr1[i]);
        System.out.println(arr2[i]);
    }
    
    Alternatively, you can use iterators:
    
    ```java
    Iterator<T> it1 = arr1.iterator();
    Iterator<U> it2 = arr2.iterator();
    while (it1.hasNext() && it2.hasNext()) {
        // Do something with it1.next()
        // Do something with it2.next()
    }
    
  • You can also change the code producing the two arrays, and make sure it uses a single List with an encapsulating object containing both an element from the first array and the corresponding one from the second array.

Sign up to request clarification or add additional context in comments.

Comments

2

If they have the same size you can do something like:

for ( i=0; i<arrayList1.size();i++){
   System.out.println("string1 is " + arrayList1[i]);
   System.out.println("strin2 is " + arrayList2[i]);
}

But to use arrayList1.size() is not the best way I think

3 Comments

just to be safe you should check if either arrayList1.size() or arrayList2.size() is greater than the other to avoid exceptions
Great,will try when home. Maybe a try-catch is best then, in case of exception ?
either this or you just check if(arrayList1.size()==arrayList1.size()){ ...}

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.