标签:delete 技术 code str 临时 csdn out public ora
创建两个类用于添加和删除,不改变数组长度的情况下:
public class InsectAndDelete {
public static void main(String[] args) {
int arr[] = {1,2,3,4,5,6,7,8};
for(int i:arr){
System.out.printf("%d ",i);
}
System.out.println();
ArrayDelete.Delete(arr,4);
for(int i:arr){
System.out.printf("%d ",i);
}
System.out.println();
ArrayInsect.Insect(arr,4,5);
for(int i:arr){
System.out.printf("%d ",i);
}
System.out.println();
}
}
public class ArrayInsect {
public static void Insect(int arrs[],int index,int elem) {
for (int i = arrs.length-2; i >= index; i--) {
arrs[i+1]=arrs[i];
}
arrs[index]=elem;
}
}
public class ArrayDelete {
public static void Delete(int arrs[],int index) {
for (int i = index; i < arrs.length-1; i++) {
arrs[i]=arrs[i+1];
}
arrs[arrs.length-1]=0;
}
}
20175314薛勐 程序设计中临时变量的使用(课下测试,选做)
标签:delete 技术 code str 临时 csdn out public ora
原文地址:https://www.cnblogs.com/SANFENs/p/10746258.html