Mix two arrays in one. Java

3

I'm new to the world of programming, I'm trying to mix two arrays into one, each with the dimension that we choose, and filled with randoms. But when it comes to mixing them, printing the first of the first array, first of the second array, second of the first array, second of the second array .. etc, gives me an exception when printing.

  

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5     at arrays.Ej_5.main (Ej_5.java:34).

Any help?

package arrays;
import java.util.*;

public class Ej_5 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner input = new Scanner (System.in);
        System.out.println("Introduce la dimension del primer array");
        int n=input.nextInt();
        System.out.println("Introduce la dimension del segundo array");
        int d=input.nextInt();

        int[] p_array= new int[n];
        int[] s_array= new int[d];

        for (int i=0;i<p_array.length;i++){
            p_array[i]= (int)(Math.random()*10);
            System.out.print(p_array[i] + " ");
        }

        System.out.println();

        for (int j=0;j<s_array.length;j++){
            s_array[j]=(int)(Math.random()*10);
            System.out.print(s_array[j] + " ");
        }

        System.out.println();

        int[] mezcla = new int [(n+d)-1];
        for (int k=0;k<mezcla.length;k++){
            System.out.print(p_array[k] + " ");
            System.out.print(s_array[k] + " ");
        }


    }

}
    
asked by Javi 30.11.2017 в 19:18
source

2 answers

3

Sure, because you're trying to read an array index that does not exist, look:

p_array = longitud = n
s_array = longitud = d
mezcla  = longitud = n + d - 1 

  int[] mezcla = new int [(n+d)-1];  
    for (int k=0;k<mezcla.length;k++){
        //aquí el error, va a llegar un momento que k > n y cae
        // también k > d y cae
        System.out.print(p_array[k] + " ");
        System.out.print(s_array[k] + " ");
    }

What you should do would be something like this:

int[] mezcla = new int [(n+d)-1];

//llena tu array mezcla con p_array (n) y s_array (d) 
int indice = 0;
int indiceParray = 0;
int indiceSarray= 0 ;
for (int x=0;x<mezcla.length;x++){
   //si es elemento par
   if (x%2 == 0 && indiceParray < p_array.length -1){
     mezcla[x] =  p_array[indiceParray];
     indiceParray++;
   } else if(indiceSarray < s_array.length -1){
     mezcla[x] =  s_array[indiceSarray];
     indiceSarray++;
   }
}


for (int x=0 ;x<mezcla.length;x++){
    System.out.print(mezcla[x] + " ");
}
    
answered by 30.11.2017 / 19:36
source
0

As mentioned, suppose that n introduces 5 and d = 10 when k reaches 5, the index in the array p_array [5] does not exist and will mark the error. To fix it you can do this

int mayor = n > d ? n : d; //verificamos que número es mayor
int[] mezcla = new int [mayor];
for (int k=0;k<mezcla.length;k++){
   if(n < k){
      System.out.print(p_array[k] + " ");
   }
   if(p < k){
      System.out.print(s_array[k] + " ");
   }
}
    
answered by 30.11.2017 в 19:50