I would like to merge the two arrays and create a new array with the numbers sorted in descending order, such as the following, but I get an error ArrayIndexOutOfBoundsException.
I don't know why this happens.I look forward to hearing from you.
public static int[]merge(int[]) arr1, int[] arr2){
int[]arr3 = new int[arr1.length+arr2.length];
int dim = 0;
for(inti=0;i<arr1.length;i++){
for (int j = 0; j <arr2.length; j++) {
if(arr1[i]<arr2[j]){
arr3[dim] = arr1[i];
dim++;
}
else if(arr1[i]==arr2[j]){
arr3[dim] = arr1[i];
dim++;
arr3[dim] = arr2[j];
dim++;
}
else{
arr3[dim] = arr2[j];
dim++;
}
}
}
return arr3;
}
If the exception ArrayIndexOutOfBoundsException
occurs, it is recommended that you follow the part that specifies the index of the array.
If you're using an IDE with debugging capabilities, you might want to pass a small array and run it step by step to see how the variables change, but here you just try to get the debug output in.
public static int[]merge(int[]) arr1, int[] arr2){
System.out.println("arr1.length="+arr1.length);
System.out.println("arr2.length="+arr2.length);
int[]arr3 = new int[arr1.length+arr2.length];
System.out.println("arr3.length="+arr3.length);
int dim = 0;
for(inti=0;i<arr1.length;i++){
for (int j = 0; j <arr2.length; j++) {
System.out.println("i="+i+",j="+j);
if(arr1[i]<arr2[j]){
System.out.println("[1]dim="+dim);
arr3[dim] = arr1[i];
dim++;
}
else if(arr1[i]==arr2[j]){
System.out.println("[2]dim="+dim);
arr3[dim] = arr1[i];
dim++;
System.out.println("[3]dim="+dim);
arr3[dim] = arr2[j];
dim++;
}
else{
System.out.println("[4]dim="+dim);
arr3[dim] = arr2[j];
dim++;
}
}
}
return arr3;
}
For testing purposes, arr1
passed an array of length 2 {2,4}
, and arr2
passed an array of length 3 {3,2,5}
, and this output was obtained.
arr1.length=2
arr2.length = 3
arr3.length = 5
i = 0, j = 0
[1]dim = 0
i = 0, j = 1
[2]dim = 1
[3]dim = 2
i = 0, j = 2
[1]dim = 3
i = 1, j = 0
[4]dim = 4
i = 1, j = 1
[4]dim = 5
java.lang.ArrayIndexOutOfBoundsException:5
at arraymerge.ArrayMerge.merge (ArrayMerge.java:52)
at arraymerge.ArrayMerge.main (ArrayMerge.java:19)
arr1
has a length of 2, so i
changes from 0 to 1.Also, arr2
length is 3, so j
should change from 0 to 2, but arr3
value used as index of arr3
exceeded the limit of 4 dim
before the value of i
, j
reached the last (1,2).
In your code,
for(inti=0;i<arr1.length;i++){
for (int j = 0; j <arr2.length; j++) {
//...
}
}
There is a double loop in the , so in this example, 2x3 = 6 attempts to perform the in-loop operation.(At this point, arr3
is greater than length 5!)
In addition, the processing in the loop is
if(arr1[i]<arr2[j]){
//...
dim++;
}
else if(arr1[i]==arr2[j]){
//...
dim++;
//...
dim++;
}
else{
//...
dim++;
}
Therefore, run dim++
at least once and at most twice, regardless of which branch in the condition.Therefore, the value of dim
must reach at least 6 during the 2x3=6 double for
.
arr3
has only 2+3=5 length, so when arr3[dim]
runs arr3[dim]
, the ArrayIndexOutOfBoundsException exception occurs when the dim
value exceeds 4.
You cannot create a "new array of numbers sorted in descending order" with just one scan of the array from the front to the result array.The merge process used in the so-called "merge sort" eventually results in a "new array of numbers sorted in descending order" by repeating the following simple merge process:
public static int[]merge(int[]) arr1, int[] arr2){
int[]arr3 = new int[arr1.length+arr2.length];
int idx = 0;
for(inti=0,j=0;i<arr1.length||j<arr2.length;){
if((i<arr1.length&j>arr2.length&arr1[i]<arr2[j])
|| (i<arr1.length&j==arr2.length) {
arr3 [idx] = arr1[i];
++idx;
++i;
} else{
arr3 [idx] = arr2[j];
++idx;
++j;
}
}
return arr3;
}
If you call this merge process only once for I don't know if your purpose of this (Merge sort requires twice as much space as the original array, so it is not normally used to sort the array.) {2,4}
and {3,2,5}
, it is not completely {2,3,2,4,5}
, but it is switched to {2,3}
and {2,2}
and {2,2/code>.
merge
method is to learn the principle of merge sorting or simply want to sort an array, but in the latter case, it's better to combine the contents of the array and then use Java's standard sort method.
© 2024 OneMinuteCode. All rights reserved.