java数组拷贝问题u!~!~!

来源:百度知道 编辑:UC知道 时间:2024/06/27 16:20:05
3、 数组拷贝
定义数组int[] a = {1,2,3,4,5,6,7 ,8,9,10 }和b。
b数组的长度为10;
将数组a中的所以元素拷贝到数组b中,打印b中元素。(用循环实现)
结果参考:

1,2,3,4,5,6,7,8,9,10
2)向a数组中的第八个位置,添加新的数据200.
并把数组中的新数据遍历打印出来。
3)把添加完新数据的数组中的第五个位置的数据删除,
并把就数组中的元素顺序打印
向a数组中的第八个位置,添加新的数据200. 不是覆盖!~!~

public static void main(String[] args) {
int[] a = {1,2,3,4,5,6,7 ,8,9,10 };
int[] b=new int[10];
//将数组a中的所以元素拷贝到数组b中,打印b中元素
for(int i=0;i<a.length;i++){
b[i]=a[i];
System.out.println(b[i]);
}
//向a数组中的第八个位置,添加新的数据200.
int[] temp=new int[11];
for(int i=0,j=0;i<temp.length;i++,j++){
if(j==7){
temp[i]=200;
i++;
}
temp[i]=a[j];

}
a=temp;
for(int i=0;i<a.length;i++){
System.out.println(a[i]);
}
//把添加完新数据的数组中的第五个位置的数据删除,并把就数组中的元素顺序打印
for(int i=0;i<a.length-1;i++){
if(i>=4){
a[i]=a[i+1];
}
System.out.println(a[i]);
}
}

import java.util.Arrays;

public class ArrayTest {
public static void main(String[] args) {
int[] a = {1,2,3,4,5,6,7 ,8,9,10 };
int[] b = new int[10];

for(int i=0; i<a.length; i++) {
b[i