废话不多说了,直接给大家贴代码了。
具体代码如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
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;
}
}
|