我需要通过 arraylist 并将所有以x
y
or开头的单词删除到前面z
。
但是我的测试输出表明这种方法有一些逻辑错误。
代码:
/**
* Moves any word that startw with x, y, or z to the front of the arraylist, but
* otherwise preserves the order
*/
public void xyzToFront()
{
int insertAt = 0;
but otherwise preserves the order
for (int i = 0; i < list.size(); i++) {
String temp = list.get(i);
if (temp.startsWith("x") || temp.startsWith("y") || temp.startsWith("z")) {
list.remove(i);
list.add(0, temp);
}
}
}
测试输出:
Actual: [yak, zebra, xantus, ape, dog, cat] - what is after executing
Expected: [xantus, zebra, yak, ape, dog, cat] - what we should have
如何解决这个麻烦?