我见过类似的问题而没有提供我正在寻找的答案,所以如果这被视为重复,我会提前道歉。 我正在尝试将数组{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;
}
}
答案 0 :(得分:8)
不要自己动手,使用System.arrayCopy()
将两个数组复制到组合大小的新数组中。这样效率更高,因为它使用本机操作系统代码。
答案 1 :(得分:6)
而不是
int[]c = new int[a+b];
您需要调用merge方法并将结果分配给数组,如:
int[]c = merge(a,b);
你的循环也应该是:
int[]c = merge(a,b);
for(int i=0; i<c.length; i++)
System.out.print(c[i]+" ");
答案 2 :(得分:4)
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));
答案 3 :(得分: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] + " ");
}
答案 4 :(得分:1)
data:image/png
}
希望你明白这一点
答案 5 :(得分:0)
您可以使用以下内容:
/* add an HTML element to the chart starting at 0px from the left corner and
10% of the container's height from the top */
chart.renderer.html('<div align="center" style="font-size: 20px; width: ' +
$('#container').width() + 'px">Center Me!</div>',0,$('#container').height()*.1).add();
答案 6 :(得分: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;
}
答案 7 :(得分: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]);
}
答案 8 :(得分:0)
连接2个数组的另一种方法是:
True
答案 9 :(得分: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]);
}
}
}
答案 10 :(得分:-1)
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
}
}
答案 11 :(得分:-3)
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;
}