无法在JAVA中识别新数组中的数字排序数组



当它开始按时间顺序对数组进行排序时,我遇到了一个问题,首先,我构建了一个2扫描仪数组,它将合并到最后一行,但结果一团糟,最后一行字符行闪烁了0

这是我的源代码:

import java.util.*;

public class Main
{
public static void main(String[] args) 
{
Scanner x = new Scanner (System.in);
System.out.print("Enter the number of elements of the first array: ");
int first = x.nextInt();
int ffinal[] = new int[first];
for(int i=0;i<ffinal.length;i++)
{
System.out.print("");
ffinal[i]=x.nextInt();
}
System.out.print("Enter the number of elements of the second array: ");
int second = x.nextInt();
int sfinal[] = new int[second];
for(int i=0;i<sfinal.length;i++)
{
System.out.print("");
sfinal[i]=x.nextInt();
}

int n = ffinal.length;
int m = sfinal.length;
int res[] = new int [n+m];
int i = 0, j=0, k=0;
while(i< n && j<m )
{
if(ffinal[i] <= sfinal[j])
{
res[k]=ffinal[i];
i+=1;
k+=1;
}
else
{
res[k]=sfinal[j];
j+=1;
k+=1;
}
}

System.out.print("New array:");
for (i=0; i<n+m;i++)
{
System.out.print(" "+res[i]); 
}

}
}

输出:

Enter the number of elements of the first array: 3
2 3 10
Enter the number of elements of the second array: 2
10 16
New array: 2 3 10 0 0

预期输出:

Enter the number of elements of the first array: 3
2 3 10
Enter the number of elements of the second array: 2
10 16
New array: 2 3 10 10 16

问题在于while条件while(i< n && j<m )
假设第一个数组的所有元素都小于第二个数组,将这些元素添加到新数组后,i将等于n,因此while(i< n && j<m )中的条件i<n变为false并退出循环
第二个数组的元素不会添加到新数组中。

假设你的a1[]={1,2}和a2[]={3,4,5}
n是2,m是3。
根据您的代码,12将添加到新的数组a3[]中
现在i将变为2,而j仍然是0

在该while(i< n && j<m )中,作为2<2i<nfalse。因此while循环将停止
那么数组a2的元素呢?{3,4,5}未添加到新数组a3中。

你可以按照@UnholySheep的建议去做。如果您想继续您的代码,
我已经更正了您的代码。

int i = 0, j=0, k=0;
while(i< n || j<m )
{   
if(i<n && i<m)
{
if(ffinal[i] <= sfinal[j])
{
res[k]=ffinal[i];
i+=1;
k+=1;
}
else
{
res[k]=sfinal[j];
j+=1;
k+=1;
}
}
else if(i<n)
{
res[k]=ffinal[i];
i+=1;
k+=1;
}
else
{
res[k]=sfinal[j];
j+=1;
k+=1;
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner x = new Scanner (System.in);
System.out.print("Enter the number of elements of the first array: ");
int first = x.nextInt();
int a[] = new int[first];

for(int i=0;i<a.length;i++)
{
System.out.print("");
a[i]=x.nextInt();
}

System.out.print("Enter the number of elements of the second array: ");
int second = x.nextInt();
int b[] = new int[second];

for(int i=0;i<b.length;i++)
{
System.out.print("");
b[i]=x.nextInt();
}

int j = 0, k = 0;
int c [] = new int[a.length+b.length];

for (int i = 0; i < c.length; i++) 
{
if (j < a.length && k < b.length) 
{
if (b[k] < a[j])
{
c[i] = b[k];
k++;
}
else 
{
c[i] = a[j];
j++;
}       
}
else if (k < b.length) 
{
c[i] = b[k];
k++;
}
else 
{
c[i] = a[j];
j++;
}
}
System.out.print("New array: ");
for(int i = 0; i < c.length; i++)
{
System.out.print(c[i]+" ");
}
}
}

谢谢你,我回答了!

查看此处的代码跟踪

n:2, m:3
i:0,j:0,k:0 | first array 2 3 | second array 10 16 | New array: 0 0 0 0 0
i:1,j:0,k:1 | first array 2 3 | second array 10 16 | New array: 2 0 0 0 0
i:2,j:0,k:2 | first array 2 3 | second array 10 16 | New array: 2 3 0 0 0

在循环结束时,i的值为2

在while循环条件while(i< n && j<m )i<n->CCD_ 20将给出CCD_ 21,条件失败并退出循环。

但正如您所看到的,第二个数组中仍然保留着一些元素,所以您需要添加两个额外的循环。

import java.util.*;

public class Main
{
public static void main(String[] args) 
{
Scanner x = new Scanner (System.in);
System.out.print("Enter the number of elements of the first array: ");
int first = x.nextInt();
int ffinal[] = new int[first];
for(int i=0;i<ffinal.length;i++)
{
System.out.print("");
ffinal[i]=x.nextInt();
}
System.out.print("Enter the number of elements of the second array: ");
int second = x.nextInt();
int sfinal[] = new int[second];
for(int i=0;i<sfinal.length;i++)
{
System.out.print("");
sfinal[i]=x.nextInt();
}

int n = ffinal.length;
int m = sfinal.length;
int res[] = new int [n+m];
System.out.println();
int i = 0, j = 0, k = 0;
while(i < n && j < m )
{

if(ffinal[i] <= sfinal[j])
{
res[k]=ffinal[i];
i+=1;
k+=1;
}
else
{
res[k]=sfinal[j];
j+=1;
k+=1;
}
}

// if there are any remaining elements from any of the two arrays add them to res array
while(i < n){
res[k]=ffinal[i];
i+=1;
k+=1;
}

while(j < m){
res[k]=sfinal[j];
j+=1;
k+=1;
}

System.out.print("New array:");
for (i=0; i<n+m;i++)
{
System.out.print(" "+res[i]); 
}

}
}

在上面的代码中,它首先检查i < n->CCD_ 23->false

则进入下一个while循环CCD_ 25->CCD_ 26->true因此循环通过第二阵列中的元素并添加到res阵列中

我将执行合并的代码与输入解析分离,这将使代码更具可测试性,此外,这将请求对两个输入数组进行排序。

import java.util.Objects;
public class MyClass {

public static int[] merge(int[] first, int[] second) {
//Check that the input are not null
Objects.requireNonNull(first, "First array cannot be null");
Objects.requireNonNull(first, "Second array cannot be null");            // duplicates are allowed so the result size is known up-front
int[] result = new int[first.length + second.length];
// cursor over the three arrays, starting from the beginning
int f = 0;
int s = 0;
int r = 0;
//loop over first array until is complete
for (; f != first.length; ++r) {
if (s == second.length) {
//if nothing left in the second array, let's copy everything
//from first into result and return
System.arraycopy(first, f, result, r, first.length - f);
return result;
}
// let's copy the smaller of the two into result
if (second[s] < first[f]) {
result[r] = second[s];
++s;
} else {
result[r] = first[f];
++f;
}
}
//we complete the loop over first, let's copy everything left from
//second to result
System.arraycopy(second, s, result, r, second.length - s);
return result;
}

//use JUnit instead, I am using an online compiler and
// I cannot bring dependencies in
public static void check(int[] a, int[] b) {
assert(a.length == b.length);
for (int i = 0; i < a.length; i++)
assert(a[i] == b[i]);
}

public static void main(String args[]) {
check(new int[]{2, 3, 10, 10, 16}, merge(new int[]{2, 3, 10}, new int[]{10, 16}));
check(new int[]{2, 3, 10}, merge(new int[]{2, 3, 10}, new int[]{}));
check(new int[]{10, 16}, merge(new int[]{}, new int[]{10, 16}));
check(new int[]{2, 3, 5, 10, 10, 16}, merge(new int[]{2, 3, 10}, new int[]{5, 10, 16}));
}
}

更新

  • 用System.arraypy替换while循环

最新更新