合并两个Java数组

huangapple go评论162阅读模式
英文:

Merge two Arrays in Java

问题

我正在尝试将两个不同的数组合并成一个合并后的数组。我已经尝试过使用ArrayList,但是我得到一个错误,显示数字已被使用但从未被调用。

以下是我目前的代码。

public class main {

     static class Solution {
        public void findLength(int[] A, int[] B) {

            
            
        }

    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] row1 = {1,2,3,4,5,6};
        int[] row2 = {7,8,9,10,11,12};
        solution.findLength(row1,row2);


    }
}
英文:

I am trying to merge two different arrays into one merged Array. I have tried ArrayList but then I get an error stating that the numbers are used but never called.

Below is the code I have so far.

public class main {

     static class Solution {
        public void findLength(int[] A, int[] B) {

            
            
        }

    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] row1 = {1,2,3,4,5,6};
        int[] row2 = {7,8,9,10,11,12};
        solution.findLength(row1,row2);


    }
}

答案1

得分: 1

这是如何合并两个数组的示例代码。

static class Solution {
    public int findLength(int[] A, int[] B) {
        return A.length + B.length;
    }

    public int[] getMergedArray(int[] A, int[] B) {
        int arrayLength = this.findLength(A, B);
        int mergedArray[] = new int[arrayLength];

        // 复制第一个数组的数据
        for (int i = 0; i < A.length; i++) {
            mergedArray[i] = A[i];
        }

        for (int i = A.length; i < arrayLength; i++) {
            mergedArray[i] = B[i - A.length];
        }

        return mergedArray;
    }
}

public static void main(String[] args) {
    Solution solution = new Solution();
    int[] row1 = {1, 2, 3, 4, 5, 6};
    int[] row2 = {7, 8, 9, 10, 11, 12};
    solution.findLength(row1, row2);
    System.out.println("First Array");
    for (int i = 0; i < row1.length; i++) {
        System.out.println(" " + row1[i]);
    }
    System.out.println("Second Array ");
    for (int i = 0; i < row2.length; i++) {
        System.out.println(" " + row2[i]);
    }
    System.out.println("Merged Array ");

    int mergedArray[] = solution.getMergedArray(row1, row2);
    for (int i = 0; i < mergedArray.length; i++) {
        System.out.println(" " + mergedArray[i]);
    }
}

输出:

First Array
1
2
3
4
5
6
Second Array 
7
8
9
10
11
12
Merged Array 
1
2
3
4
5
6
7
8
9
10
11
12
英文:

This is how you can merge two arrays.

static class Solution {
public int findLength(int[] A, int[] B) {
return A.length+B.length;                        
}
public int[] getMergedArray(int[] A, int[] B){
int arrayLength=this.findLength(A, B);
int mergedArray[]= new int[arrayLength];
//copying first array data
for (int i = 0; i &lt; A.length; i++) {
mergedArray[i]=A[i];
}
for (int i = A.length; i &lt; arrayLength; i++) {
mergedArray[i]=B[i-A.length];
}            
return mergedArray;
}
}
public static void main(String[] args) {
Solution solution = new Solution();
int[] row1 = {1,2,3,4,5,6};
int[] row2 = {7,8,9,10,11,12};
solution.findLength(row1,row2);
System.out.println(&quot;First Array&quot;);
for (int i = 0; i &lt; row1.length; i++) {
System.out.println(&quot; &quot;+row1[i]);
}
System.out.println(&quot;Second Array &quot;);
for (int i = 0; i &lt; row2.length; i++) {
System.out.println(&quot; &quot;+row2[i]);
}
System.out.println(&quot;Merged Array &quot;);
int mergedArray[]=solution.getMergedArray(row1, row2);
for (int i = 0; i &lt; mergedArray.length; i++) {
System.out.println(&quot; &quot;+mergedArray[i]);
}
}

Output
run:

First Array
1
2
3
4
5
6
Second Array 
7
8
9
10
11
12
Merged Array 
1
2
3
4
5
6
7
8
9
10
11
12

答案2

得分: 0

你只需像合并数组一样复制数组即可

int[] row1 = {1, 2, 3, 4, 5, 6};
int[] row2 = {7, 8, 9, 10, 11, 12};
int x1 = row1.length;

int x2 = row2.length;

int[] result = new int[x1 + x2];

System.arraycopy(row1, 0, result, 0, x1);
System.arraycopy(row2, 0, result, x1, x2);
System.out.println(Arrays.toString(result));
英文:

you can just copy the arrays as like you were merging them

int[] row1 = {1,2,3,4,5,6};
int[] row2 = {7,8,9,10,11,12};
int x1 = row1.length;
int x2 = row2.length;
int[] result = new int[x1 + x2];
System.arraycopy(row1, 0, result, 0, x1);  
System.arraycopy(row2, 0, result, x1, x2);  
System.out.println(Arrays.toString(result));

huangapple
  • 本文由 发表于 2020年9月3日 15:50:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63719129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定