-5

I have tried this

public static void main(String[] args) throws ArrayIndexOutOfBoundsException
{
int arr1[]= {1,2,3,4,5};
int arr2[]= {6,7};
for(int i=0;i<arr1.length;i++)
   {
     System.out.println(arr1[i]);
     System.out.println(arr2[i]);
   }
}

but this does not work.

  • It would be helpful if you shared the error it throws. Looking at your code, as `arr1` is longer than `arr2`, the loop will keep going past the length of `arr2`, where there are no elements, resulting in the failure. – cluskii Apr 11 '18 at 12:16
  • 1
    It would also be helpful if we knew what is the expected output – Th0rndike Apr 11 '18 at 12:17
  • 1
    I would down-vote if i could... – CannedMoose Apr 11 '18 at 12:18
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – EJoshuaS - Stand with Ukraine Apr 11 '18 at 12:51

5 Answers5

4

You have to find the max length and then care about what you can output to not hit the boundaries.

public static void main(String[] args) throws ArrayIndexOutOfBoundsException
{
int arr1[]= {1,2,3,4,5};
int arr2[]= {6,7};
int max = arr1.length;
if (max < arr2.length){
  max = arr2.length;
}
for(int i=0;i<max;i++)
   {
     if (i < arr1.length){
       System.out.println(arr1[i]);
     }
     if (i < arr2.length){
       System.out.println(arr2[i]);
     }
   }
}
libik
  • 22,239
  • 9
  • 44
  • 87
4

Iterate the loop to the maximum length of two arrays, and Print only those array element whose index is valid.

public static void main(String[] args) throws ArrayIndexOutOfBoundsException
    {
    int arr1[]= {1,2,3,4,5};
    int arr2[]= {6,7};
    for(int i=0;i<Math.max(arr1.length, arr2.length);i++)
       {
         if(i<arr1.length)
             System.out.println(arr1[i]);
         if(i<arr2.length)
             System.out.println(arr2[i]);
       }
    }
Ishpreet
  • 5,230
  • 2
  • 19
  • 35
0

Try this, this will work I think

int arr1[]= {1,2,3,4,5};
int arr2[]= {6,7};
int a1 = arr1.length();
int a2 = arr2.length();
int n = a1 > a2 ? a1 : a2;
for(int i = 0; i < n; i++) {
    if(a1 > i) 
        System.out.println(arr1[i]);
    if(a2 > i) 
        System.out.println(arr2[i]);
}

Best of luck

Akshay Batra
  • 137
  • 7
0

Or, for printing them one after the other:

  int arr1[]= {1,2,3,4,5};
  int arr2[]= {6,7};

  for (int i=0; i < arr1.length + arr2.length; ++i)
  {
     if (i < arr1.length)
         System.out.println (arr1 [i]);
     else 
         System.out.println (arr2 [i - arr1.length]);
  }
user unknown
  • 35,537
  • 11
  • 75
  • 121
0

You can use below code. You can still meet a condition where both arrays of same length.This will work only if arrays are of different lengths.

public static void main(String[] args) throws ArrayIndexOutOfBoundsException
{
    int arr1[]= {1,2,3,4,5};
    int arr2[]= {6,7};
    int arr1Length = arr1.length;
    int arr2Length = arr2.length;
    if(arr1.length>arr2.length)
    {
        for(int i=0;i<arr1.length;i++)
           {
             System.out.println(arr1[i]);
                if(i<arr2.length)
                {
                System.out.println(arr2[i]);
                }
                else continue;
       }
    else{
            for(int i=0;i<arr2.length;i++)
               {
                if(i<arr1.length)
                {
                 System.out.println(arr1[i]);
                }
                else continue;
                 System.out.println(arr2[i]);
               }
    }

}
vamsi
  • 329
  • 2
  • 10