Java中将两个整数数组组合成一个数组

5

我看到了类似的问题,但没有提供我想要的答案,如果这被认为是重复的话,请提前原谅我。我正在尝试将数组{1,2,3}和{4,5,6}合并成{1,2,3,4,5,6}。我做错了什么?我是Java的新手,请不要嘲笑我的问题。

public class combine {
  public static void main(String[]args){

  int[]a = {1, 2, 3};
  int[]b = {4, 5, 6};
  int[]c = new int[a+b];
  for(int i=0; i<a.length; i++)
  System.out.print(c[i]+" ");
}
public static int[]merge(int[]a, int[]b){
  int[]c = new int[a.length+b.length];
  int i;
  for(i=0; i<a.length; i++)
     c[i] = a[i];

     for(int j=0; j<b.length; j++)
        c[i++]=b[j];
        return c;
}
}

在某些地方调用的是“merge”,但实际上并不是合并,更像是“连接” :P - nachokk
它也在方法中局部定义;它永远不会打印正确的数组。 - Jeroen Vannevel
1
可能是如何在Java中连接两个数组?的重复问题。 - Abimaran Kugathasan
12个回答

9
不要自己做,使用System.arrayCopy()将两个数组复制到一个新的组合大小的数组中。这样更有效率,因为它使用本机操作系统代码。

是因为深拷贝和浅拷贝的原因吗? - Kick Buttowski
1
不是的,两个版本都很浅显,但 System.arrayCopy() 使用操作系统例程而不是 Java 抽象。 - Sean Patrick Floyd

6

代替

int[]c = new int[a+b];

你需要调用合并方法并将结果分配给数组,例如:
int[]c = merge(a,b);

您的 for 循环应该是这样的:
int[]c = merge(a,b);
for(int i=0; i<c.length; i++)
    System.out.print(c[i]+" ");

5
  String a[] = { "A", "E", "I" };
  String b[] = { "O", "U" };
  List list = new ArrayList(Arrays.asList(a));
  list.addAll(Arrays.asList(b));
  Object[] c = list.toArray();
  System.out.println(Arrays.toString(c));

2
我知道代码很清晰,但最好还是提供一下你正在做什么的解释。没有解释的纯代码往往会被标记为删除。另外,欢迎来到SO。 - StormeHawke
感谢@StormeHawke,我是这个社区的新成员...以后会注意的...代码本身就很清楚,所以没有为它提供注释。 - Karan singh

3

请尝试这段代码,希望对您有所帮助。

String a[] = new String[4];
    String b[] = new String[2];
    String[] ab = new String[a.length + b.length];
    int i, j, d, s = 0;
    @SuppressWarnings("resource")
    Scanner x = new Scanner(System.in);
    System.out.println("Enter the first array");

    for (i = 0; i < a.length; i++) {
        a[i] = x.next();
        for (d = i; d < a.length; d++) {
            ab[d] = a[i];
        }
    }

    System.out.println("Enter the second array");

    for (j = 0; j < b.length; j++) {
        b[j] = x.next();
        for (d = a.length + j; d < ab.length; d++)
            ab[d] = b[j];
    }
    System.out.println();
    System.out.println("The new array is !!");
    System.out.println("--------------------");
    for (s = 0; s < ab.length; s++) {
        System.out.print(ab[s] + " ");
    }

1
class MerginTwoArray{
public static void mergingarr(int x[], int y[])
{
    int len=x.length+y.length;
    int arr[]=new int[len];
    //create a variable j which will begin zeroth index of second array
    int j=0;
    for(int i=0; i<arr.length; i++)
    {
        if(i<x.length)
        {
            arr[i]=x[i];
        }
        else
        {
            arr[i]=y[j];
            j++;
        }
    }
    for(int i:arr)
    {
        System.out.print(i+ " ");
    }
}
public static void main(String... args)
{
    mergingarr(new int[]{1,2,3}, new int[]{4,5,6});
}

}

希望这对你很清晰。

1

连接两个数组的另一种方法是:

System.out.println("Enter elements for a: ");
for (int i = 0; i < 5; i++) {
    int num = in.nextInt();
    a[i] = num;
}

System.out.println("Enter elements for b: ");
for (int i = 0; i < 5; i++) {
    int num = in.nextInt();
    b[i] = num;
}

void merge() {
    int c[] = new int[10];
    System.arraycopy(a, 0, c, 0, a.length);
    System.arraycopy(b, 0, c, a.length, b.length);

    System.out.println(Arrays.toString(c));  // merged array 
}

0
您可以使用以下内容:
package array;

public class Combine {

public static void main(String[] args) {

 int[]a = {1,2,3,4};
 int[]b = {4,16,1,2,3,22};
 int[]c = new int[a.length+b.length];
 int count=0;

  for(int i=0; i<a.length; i++)
    {
       c[i]=a[i];
       count++;
    }

 for(int j=0;j<b.length;j++)
    {
       c[count++]=b[j];
    }

        for(int i=0;i<c.length;i++)
        System.out.print(c[i]+" ");
}

}

0

看看我的解决方案(如果需要,你最终可以对其进行排序):

public static int[] mergeAndSortIntArrays(int[] firstInt, int[] secondInt){

    List<Integer> merged = new ArrayList<>();

    for (int i=0; i<firstInt.length; i++){
        merged.add(firstInt[i]);
    }

    for (int i=0; i<secondInt.length; i++){
        merged.add(secondInt[i]);
    }

    Collections.sort(merged);
    int[] result=new int[merged.size()];
    for (int i=0; i<merged.size(); i++){
        result[i]=merged.get(i);
    }
    return result;
}

0
   int a[] = {
        5, 10, 15, 25
    };
    int b[] = {
        12, 5, 7, 9
    };
    int c[] = new int[8];
    for (int i = 0; i < 4; i++) {
        System.out.println(a[i]);

    }
    System.out.println("**************");
    for (int j = 0; j < 4; j++) {
        System.out.println(b[j]);
    }
    //int[] c=merge(a,b);
    for (int i = 0; i < 4; i++) {

        c[i] = a[i];

    }
    for (int i = 0; i < 4; i++) {
        for (int k = 4; k < 8; k++) {
            c[k] = b[i];
        }

    }
    for (int i = 0; i < 4; i++) {
        System.out.println(c[i]);
    }

1
一些解释会很好。 - TobiasR.

0
合并两个数组但不使用ArrayList。
    public class Main {


    static int a[] = {1, 2, 3, 4};
    static int b[] = {5, 6, 7, 8};

    public static void main(String[] args) {

        System.out.println("Hello World!");
        int totalLengh = a.length + b.length;

        int c[] = new int[totalLengh];

        int j = 0;
        for (int i = 0; i < totalLengh; i++) {

            if (i < a.length) {

                c[i] = a[i];

            } else {
                c[i] = b[j];
                j++;
            }

            System.out.println("" + c[i]);
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接