I was asked to do this exercise in java... "Read two different arrays that contain integers and create a third array. Where the elements are entered sequentially in the third array. Exemplifying or representing the two arrays."
I have made the following code that solves the exercise.
public static void main(String[] args) {
Scanner entrada = new Scanner (System.in);
int [] impares = new int [10];
int [] pares = new int [10];
int [] fusion = new int [impares.length + pares.length];
for (int a = 0; a < impares.length; a++) {
System.out.println("Ingrese un numero en la posición ["+a+"] del Arreglo Impares.");
impares[a]=entrada.nextInt();
}
for (int b = 0; b < pares.length; b++) {
System.out.println("Ingrese un numero en la posición ["+b+"] del Arreglo Pares.");
pares[b]=entrada.nextInt();
}
int j=0;
for (int i = 0; i < 10; i++) {
fusion[j]= impares[i];
j++;
fusion[j]= pares[i];
j++;
}
System.out.println("\n \n Al juntar los dos arreglos el resultado es: ");
for (int i = 0; i < fusion.length; i++) {
System.out.println(fusion[i]+"");
}
}
The help I need is to correct the following for loop, when the value of the arrays is unequal. For example, suppose that the "odd" array is initialized with length 8 and the "even" array is initialized with length 9.
int j=0;
for (int i = 0; i < 10; i++) {
fusion[j]= impares[i];
j++;
fusion[j]= pares[i];
j++;
}
I have already tried putting the length of one of the two arrays, but nevertheless I always get an error, as I also tried to add the two lengths of the two arrays in that for, but I still can't get the execution to finish correctly.
I thank in advance those who help me with their answers, I'm learning software development and java is one of the subjects we are looking at.
Because the 2 will have different values, one way to solve it would be the following.
To avoid having to enter the value that your final array will have , you
fusion
can use without problemsfusion.length