java中如何把一个int型数组分解成两个int型数组啊?

比如int c[]={1,2,3,4},分解成int a[]={1,2}和int b[]={3,4}

int[] c = {1,2,3,4};
int[] a = new int[2];
int[] b = new int[2];
System.arraycopy(c,0,a,0,2);
System.arraycopy(c,2,b,0,2);

System.out.println( Arrays.toString(a) );
System.out.println( Arrays.toString(b) );
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-04-06
arraycopy(Object src, int srcPos, Object dest, int destPos, int length) 从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。
例子如下:
int[] c = {1,2,3,4}
int[] a = System.arraycopy(a,0,2);
int[] b = System.arraycopy(a,2,4);
第2个回答  2011-04-06
system.arraycopy来做
第3个回答  2023-03-09
要将一个int型数组分解成两个int型数组,可以使用循环遍历原始数组,根据下标的奇偶性将元素添加到不同的目标数组中。

以下是一个示例代码:

```java
public class ArraySplit {
public static void main(String[] args) {
int[] c = {1, 2, 3, 4};
int[] a = new int[c.length / 2];
int[] b = new int[c.length - a.length];
for (int i = 0; i < c.length; i++) {
if (i % 2 == 0) {
a[i / 2] = c[i];
} else {
b[i / 2] = c[i];
}
}
System.out.println(Arrays.toString(a)); // 输出[1, 2]
System.out.println(Arrays.toString(b)); // 输出[3, 4]
}
}
```

在上述代码中,我们首先定义了原始数组`c`、目标数组`a`和目标数组`b`。然后,使用循环遍历原始数组`c`,如果当前下标为偶数,则将该元素添加到`a`数组中;否则将该元素添加到`b`数组中。最后,输出两个目标数组即可。

需要注意的是,在实际应用中还需要考虑更多的边界情况和错误处理。例如,如果原始数组长度为奇数,则需要在分解时处理最后一个元素的情况。
相似回答