【发布时间】:2016-05-17 04:38:25
【问题描述】:
我在下面的代码中从数组中删除元素。在此特定代码中,我将删除位置 2 处的元素。我将如何删除此数组中的随机元素?
public class QuestionOneA2 {
public static void main(String[] args) {
int size = 5;
int pos = 2;
String[] countries = {"Brazil", "France", "Germany", "Canada", "Italy", "England"};
for (int i = 0; i < size; i++) {
if(i == pos) {
countries[i] = countries[size];
}
System.out.println(countries[i]);
}
}
}
【问题讨论】:
-
使用例如
int size = countries.length - 1或只是countries.length - 1而不是固定大小;否则你做错了。