java中将两个整数数组合并为一个数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20316526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Combine two integer arrays into one array in java
提问by Jeremy Stone
I've seen similar questions and none provide the answer that I'm looking for, so I apologize in advance if this is considered a duplicate. I'm trying to combine arrays {1, 2, 3} and {4, 5, 6} into {1, 2, 3, 4, 5, 6}. What am I doing incorrectly? I'm super new to java. Sorry if the question is stupid.
我见过类似的问题,但没有人提供我正在寻找的答案,所以如果这被认为是重复的,我提前道歉。我正在尝试将数组 {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;
}
}
采纳答案by Alexis C.
Instead of
代替
int[]c = new int[a+b];
You need to call your merge method and assign the result to the array like :
您需要调用合并方法并将结果分配给数组,例如:
int[]c = merge(a,b);
Also you for loop should be :
你 for 循环也应该是:
int[]c = merge(a,b);
for(int i=0; i<c.length; i++)
System.out.print(c[i]+" ");
回答by Sean Patrick Floyd
Don't do it yourself, use System.arrayCopy()
to copy both arrays into a new array of the combined size. That's much more efficient, as it uses native OS code.
不要自己动手,用于System.arrayCopy()
将两个数组都复制到组合大小的新数组中。因为它使用本机操作系统代码,所以效率更高。
回答by Karan singh
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));
回答by Siddharth Singh Rawat
You can use the following:
您可以使用以下内容:
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]+" ");
}
}
回答by Mona Jalal
Have a look at my solution (you can eventually sort it if need be):
看看我的解决方案(如果需要,您最终可以对其进行排序):
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;
}
回答by Mona Jalal
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]);
}
回答by Sanyam Thukral
public static void main(String[]args){
int[]a = {1, 2, 3};
int[]b = {4, 5, 6};
int[]c ;
c=merge(a,b);
for(int i=0; i<c.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];
System.out.println(i);
for(int j=0; j<b.length; j++)
c[i++]=b[j];
return c;
}
回答by Ghaith Al-Tameemi
Please try this code, I hope it's useful for you
请试试这个代码,我希望它对你有用
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] + " ");
}
回答by Bablu kumar
public class MergeArrays {
public static void main(String[]args){
int[] a = {1, 2, 3};
int[] b = {4, 5, 6};
int[] c = new int[a.length+b.length];// Here length of int[] c will be 6
int count = 0;
//looping to store the value length of i
for(int i = 0; i<a.length; i++) {
c[i] = a[i];
count++;
}
//looping to store the value length of j
for(int j = 0;j<b.length;j++) {
c[count++] = b[j];
}
//looping to retrieve the value of c
for(int i = 0;i<c.length;i++)
System.out.print(c[i]);// Displaying looped value/output in single line at console
}
}
回答by Ijhar Ansari
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});
}
}
}
Hope this is clear to you
希望这对你来说很清楚