Java数组中的元素删除并实现向前移的代码


废话不多说了,直接给大家贴代码了。

具体代码如下所示:

public class Test {
/**
* Java怎么删除数组中的一个元素并且向前移
*
* @param args
* @throws IOException
*/
public static void main(String[] args) {
String[] arrays = { "", "", "", "", "" };
System.out.println("数组删除前:");
for (int i = ; i < arrays.length; i++) {
System.out.print(arrays[i]+" ");
}
String[] arrays =removeitem(arrays,"");
System.out.println("");
System.out.println("数组删除后:");
for (int i = ; i < arrays.length; i++) {
System.out.print(arrays[i]+" ");
}
}
public static String[] removeitem(String[] arrays,String str){
String[] tempArr = new String[arrays.length];
int i = ;
for(String s:arrays){
if(!s.equals(str)){
tempArr[i] = s;
i++; 
}
}
return tempArr;
}
}


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3