0:00
What is in the list nums
if it initially contained [5, 3, 1]
and the following code is executed?
nums.add(6); nums.add(0,4); nums.remove(1);
[4, 5, 3, 1, 6]
中删去5
。2
Assume that nums
has been created as an ArrayList
object and initially contains the following Integer values: [0, 0, 4, 2, 5, 0, 3, 0]
. What will nums
contain as a result of executing the following method numQuest
?
private List<Integer> nums; //precondition: nums.size() > 0 //nums contains Integer objects public void numQuest() { int k = 0; Integer zero = new Integer(0); while (k < nums.size()) { if (nums.get(k).equals(zero)) nums.remove(k); k++; } }
method会删除找到的
0
,但漏过了对删除元素之后元素的检查。1