返回两个数组的排序组合

时间:2019-03-17 23:12:43

标签: java arrays sorting methods

我的方法应返回数组“ a”和“ b”的组合,并以有序数组“ c”的形式返回组合。

对于a [5]和b [4,6] c []应返回[4,5,6]

最后,对于这些情况,我的代码返回了[4,5],我认为排序方法存在我看不到的问题。

package combinaum;
import java.util.Scanner;
/**
 *
 * @author 201627010262
 */
public class CombinaUm {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) 
{
   Scanner teclado = new Scanner (System.in);
   int n1 = teclado.nextInt();
   int n2 = teclado.nextInt();
   int[] a = new int [n1];
   int[] b = new int [n2];
   for(int i = 0; i < n1; i++)
   {
       a[i] = teclado.nextInt();
   }
   for(int i = 0; i < n2; i++)
   {
       b[i] = teclado.nextInt();
   }

  System.out.println(Arrays.toString(Combine(a, b, n1, n2)));
}

public static int Combine(int a[], int b[], int n1, int n2)
{  
    int e = 0;
    int d = 0;
    int i = 0;
    int n3 = n1+n2; 
    int [] c = new int [n3];

    while(e < a.length && d <b.length )
    {
        if(a[e] < b[d])
        {
           c[i] = a[e];
           e++;
           i++;
        }
        else
        {
            c[i] = b[d];
            d++;
            i++;
        }
    }
    while(e < a.length)
    {
        e++;
    }
    while(d <b.length)
    {
        d++;
    }

     return Arrays.copyOf(c, i);
}

}

1 个答案:

答案 0 :(得分:0)

您不必传递With rOut With .Cells(1, 1) .Offset(iFile).Hyperlinks.Add Anchor:=.Offset(iFile), Address:=sPath, TextToDisplay:=sName End With With .Cells(1, 2) .Offset(iFile).Hyperlinks.Add Anchor:=.Offset(iFile), Address:=sName, TextToDisplay:=sFile End With End With n1参数。数组知道它们的大小,您可以使用n2a.length来获取。另外,您还想返回b.length来拥有整个数组,而不是单个int[]

int