Java 一维数组中删除第M个元素
发布网友
发布时间:2023-10-21 11:20
我来回答
共1个回答
热心网友
时间:2024-12-06 11:59
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
String[] temp = input.split(" ");
int len = 0;
int deleteIndex =0;
try{
len = Integer.parseInt(temp[0]);
deleteIndex = Integer.parseInt(temp[1]);
input = scan.nextLine();
String[] array = input.split(" ");
if(array.length==len){
List<String> list = new ArrayList<String>();
for(int i=0;i<array.length;i++){
if(i!= deleteIndex-1){
list.add(array[i]);
}
}
StringBuffer sb = new StringBuffer();
for(String s:list){
sb.append(s).append(" ");
}
System.out.println(sb.substring(0,sb.length()-1));
}else{
System.out.println("数组长度不一致");
}
}catch(Exception e){
e.printStackTrace();
}