java中两个int型数组怎么合并啊?
比方两个数组inta[]={1,2,3,4};intb[]={5,6,7,8};合并成intc[]={1,2,3,45,6,7,8};...
比方两个数组 int a[] = {1,2,3,4};
int b[] = {5,6,7,8};
合并成int c[] = {1,2,3,4
5,6,7,8}; 展开
int b[] = {5,6,7,8};
合并成int c[] = {1,2,3,4
5,6,7,8}; 展开
6个回答
展开全部
新建一个C数组,长度为a和b长度之和,然后做个遍历a和b添加进c就可以了
int a[] = {1,2,3,4};
int b[] = {5,6,7,8};
int c[]=new int[a.length+b.length];
for(int i=0;i<a.length;i++){
c[i]=a[i];
}
for(int j=0;j<b.length;j++){
c[a.length+i]=b[j];
}
for(int k=0;k<c.length;k++){
System.out.println(k);
}
int a[] = {1,2,3,4};
int b[] = {5,6,7,8};
int c[]=new int[a.length+b.length];
for(int i=0;i<a.length;i++){
c[i]=a[i];
}
for(int j=0;j<b.length;j++){
c[a.length+i]=b[j];
}
for(int k=0;k<c.length;k++){
System.out.println(k);
}
展开全部
用System.arraycopy()就能将这两个数组合并成一个,具体做法看下面程序
public static void main(String[] agrs){
int a[] = {1,2,3,4};
int b[] = {5,6,7,8};
int c[]=null;
c=new int[a.length+b.length];
System.arraycopy(a,0,c,0,a.length);
System.arraycopy(b,0,c,a.length,b.length);
for(int i=0;i<c.length;i++){
System.out.println(c[i]);
}
}
public static void main(String[] agrs){
int a[] = {1,2,3,4};
int b[] = {5,6,7,8};
int c[]=null;
c=new int[a.length+b.length];
System.arraycopy(a,0,c,0,a.length);
System.arraycopy(b,0,c,a.length,b.length);
for(int i=0;i<c.length;i++){
System.out.println(c[i]);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class ContactIntegerArray {
public static int[] contact(int a[], int b[]){
int[] f = new int[a.length+b.length];
for(int i = 0 ; i < f.length; i++)
if(i < a.length)
f[i] = a[i];
else
f[i]= b[i-a.length];
return f;
}
public static void main(String[] args) {
int a[] = {1,3,4,7};
int b[] = {2,3,5};
int c[] =ContactIntegerArray.contact(a, b);
}
}
public static int[] contact(int a[], int b[]){
int[] f = new int[a.length+b.length];
for(int i = 0 ; i < f.length; i++)
if(i < a.length)
f[i] = a[i];
else
f[i]= b[i-a.length];
return f;
}
public static void main(String[] args) {
int a[] = {1,3,4,7};
int b[] = {2,3,5};
int c[] =ContactIntegerArray.contact(a, b);
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
int a1[] = new int[4];
a1[0] = 1;
a1[1] = 2;
int a2[] = { 3, 4 };
System.arraycopy(a2, 0, a1, 2, 2);
System.out.println(Arrays.toString(a1));
结果是:[1, 2, 3, 4]
a1[0] = 1;
a1[1] = 2;
int a2[] = { 3, 4 };
System.arraycopy(a2, 0, a1, 2, 2);
System.out.println(Arrays.toString(a1));
结果是:[1, 2, 3, 4]
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
刚运行过 //有问题可以继续交流
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[] a = {1, 2, 3, 4};
int[] b = {5, 6, 7, 8};
int[] c = new int[a.length + b.length];
for(int i = 0; i < a.length; i++) {
c[i] = a[i] ;
}
for(int j = 0; j < b.length; j++) {
c[a.length + j] = b[j];
}
System.out.println(Arrays.toString(c));
}
}
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[] a = {1, 2, 3, 4};
int[] b = {5, 6, 7, 8};
int[] c = new int[a.length + b.length];
for(int i = 0; i < a.length; i++) {
c[i] = a[i] ;
}
for(int j = 0; j < b.length; j++) {
c[a.length + j] = b[j];
}
System.out.println(Arrays.toString(c));
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询