我在開始按時間順序對陣列進行排序時遇到了麻煩,首先,我構建了一個將在最后一行合并的 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
uj5u.com熱心網友回復:
問題在于您的 while 條件while(i< n && j<m )。
假設所有第一陣列的元素比第二陣列小,添加這些新的陣列后,i將等于n,這樣的條件i<n在while(i< n && j<m )變假,退出回圈。
第二個陣列的元素不會添加到新陣列中。
假設您的 a1[] = {1,2} 和 a2[] = {3,4,5}。
n是 2 和m3。
根據您的代碼,1并將2添加到新陣列 a3[]。
現在i將變為 2 而 j 仍然是0。
在這while(i< n && j<m ),i<n這2<2是false。所以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;
}
}
uj5u.com熱心網友回復:
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] " ");
}
}
}
謝謝我回答了!
uj5u.com熱心網友回復:
在此處查看您的代碼的跟蹤
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->2<2將給出false并且條件失敗并退出回圈。
但是正如您所看到的,第二個陣列中仍有元素,因此您需要做的是添加兩個額外的回圈。
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-> 2<2-> false,
然后進入下一個 while 回圈j < m-> 0 < 3-> trueso 回圈遍歷第二個陣列中的元素并添加到 res 陣列中
uj5u.com熱心網友回復:
我會將執行合并的代碼與輸入決議分開,這將使代碼更具可測驗性,此外,這要求對兩個輸入陣列進行排序。
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.arraycopy 替換 while 回圈
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/351822.html
標籤:爪哇
