英文:
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 < 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]);
}
}
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));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论