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] + " ");
}
}
}