提交:
编译运行没有问题后,git add . git commit -m "数组元素删除,插入" git push; 提交码云上你完成的代码的连接。
任务:
//定义一个数组,好比
int arr[] = {1,2,3,4,5,6,7,8};git
//打印原始数组的值
for(int i:arr){
System.out.print(i + " ");
}
System.out.println();数组
// 添加代码删除上面数组中的5
...code
//打印出 1 2 3 4 6 7 8 0
for(int i:arr){
System.out.print(i + " ");
}
System.out.println();blog
// 添加代码再在4后面5
...get
//打印出 1 2 3 4 5 6 7 8
for(int i:arr){
System.out.print(i + " ");
}
System.out.println();
2、实验代码it
public class kexiaceshi { public static void main(String[] args) { int arr[] = {1,2,3,4,5,6,7,8}; for(int i:arr){ System.out.print(i + " "); } System.out.println(); // 添加代码删除上面数组中的5 int temp = -1; for(int i:arr){ if(arr[i] == 5){ temp = i; break; } } for(int i=temp+1;i<arr.length;i++){ arr[i-1] = arr[i]; } arr[arr.length-1] = 0; // //从数组中删除一个元素,并后面元素前移: 首先找到该元素所在的下标,将该下标以后的全部元素前移,将末尾元素赋值为0 for(int i:arr){ System.out.print(i + " "); } System.out.println(); // 添加代码再在4后面5 for(int i:arr){ if(arr[i] == 4){ temp = i; break; } } for(int i=arr.length-1;i>temp+1;i--){ arr[i] = arr[i-1]; } arr[temp+1] = 5; //从数组中添加一个元素,并后面元素后移: 首先找到该元素以后的所在的下标,将该下标以后的全部元素后移移,将找到元素赋值为设定值。 //打印出 1 2 3 4 5 6 7 8 for(int i:arr){ System.out.print(i + " "); } System.out.println(); } }
3、运行结果截图
代码托管编译