Java 删除ArrayList中大于x的数字
我是java的noob,现在我正在学习泛型。此代码应删除任何大于5的整数。我输入了[10,11,12,1],理论上,我应该只得到[3,4,6,1]。但是我得到了[3,4,6,11,1],我不明白为什么Java 删除ArrayList中大于x的数字,java,generics,arraylist,Java,Generics,Arraylist,我是java的noob,现在我正在学习泛型。此代码应删除任何大于5的整数。我输入了[10,11,12,1],理论上,我应该只得到[3,4,6,1]。但是我得到了[3,4,6,11,1],我不明白为什么 public static void main(String args[]) throws IOException{ BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); Arra
public static void main(String args[]) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(3);
list.add(4);
list.add(56);
list.add(6);
for (int i = 0; i < 4; i++){
String s = reader.readLine();
list.add(Integer.parseInt(s));
}
for (int i = 0; i < list.size(); i++){
if (list.get(i) > 5)
list.remove(i);
//else
//i++;
}
System.out.println(list);
}
十,
11
12
一,
[3,4,6,11,1]如果您阅读了的文档,您将看到索引i之后的元素向左移动。这意味着在当前实现中,每次删除后都会跳过一个元素,因此,如果相邻的两个元素大于5,则只会删除其中一个元素 您可以做的是,在删除带有i-;的元素后,将当前索引向后移动一步;。因此,您的代码将成为
public static void main(String args[]) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(3);
list.add(4);
list.add(56);
list.add(6);
for (int i = 0; i < 4; i++){
String s = reader.readLine();
list.add(Integer.parseInt(s));
}
for (int i = 0; i < list.size(); i++){
if (list.get(i) > 5) {
list.remove(i);
i--;
}
}
System.out.println(list);
}
这将输出[3,4,1],删除所有大于5的数字。如果您阅读了的文档,您将看到索引i之后的元素向左移动。这意味着在当前实现中,每次删除后都会跳过一个元素,因此,如果相邻的两个元素大于5,则只会删除其中一个元素 您可以做的是,在删除带有i-;的元素后,将当前索引向后移动一步;。因此,您的代码将成为
public static void main(String args[]) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(3);
list.add(4);
list.add(56);
list.add(6);
for (int i = 0; i < 4; i++){
String s = reader.readLine();
list.add(Integer.parseInt(s));
}
for (int i = 0; i < list.size(); i++){
if (list.get(i) > 5) {
list.remove(i);
i--;
}
}
System.out.println(list);
}
这将输出[3,4,1],删除所有大于5的数字。如果您使用的是Java 8+,您可以这样使用:
list.removeIf(i -> i > 5);
如果您使用的是Java 8+,您可以这样使用:
list.removeIf(i -> i > 5);
我想这是理解如何从列表中删除的练习,但除此之外,在添加到列表之前,您可以通过以下方式进行检查:
public static void main(String args[]) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 4; i++){
System.out.println("type a number: ");
String s = reader.readLine();
int j = Integer.parseInt(s);
if(j < 5) {
list.add(Integer.parseInt(s));
}
}
System.out.println(list);
}
我想这是理解如何从列表中删除的练习,但除此之外,您可以在添加到列表之前进行检查:
public static void main(String args[]) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 4; i++){
System.out.println("type a number: ");
String s = reader.readLine();
int j = Integer.parseInt(s);
if(j < 5) {
list.add(Integer.parseInt(s));
}
}
System.out.println(list);
}
如果您从数组的末尾开始检查,它就工作了
public static void main(String args[]) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(3);
list.add(4);
list.add(56);
list.add(6);
for (int i = 0; i < 4; i++){
String s = reader.readLine();
list.add(Integer.parseInt(s));
}
for (int i = list.size() - 1; i >=0 ; i--){
if (list.get(i) > 5)
list.remove(i);
}
System.out.println(list);
}
1011121
[3,4,1]如果从阵列的末尾开始检查,则检查正常
public static void main(String args[]) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(3);
list.add(4);
list.add(56);
list.add(6);
for (int i = 0; i < 4; i++){
String s = reader.readLine();
list.add(Integer.parseInt(s));
}
for (int i = list.size() - 1; i >=0 ; i--){
if (list.get(i) > 5)
list.remove(i);
}
System.out.println(list);
}
1011121
[3,4,1]为什么[10,11,12,1]应该输出[3,4,6,1]?而且你的代码没有[10,11,12,1]。@Gendarme列表中预先填充了3,4,56,6,然后添加了用户输入,这就是10,11,12,1的来源。哦,我现在明白了。@OP,如果代码应该删除大于5的数字,那么预期的输出怎么会包含6?这不是一个特别的Java问题。在Java中,您需要按条件元素i筛选列表,[10,11,12,1]为什么要输出[3,4,6,1]?而且你的代码没有[10,11,12,1]。@Gendarme列表中预先填充了3,4,56,6,然后添加了用户输入,这就是10,11,12,1的来源。哦,我现在明白了。@OP,如果代码应该删除大于5的数字,那么预期的输出怎么会包含6?这不是一个特别的Java问题。在Java中,您需要按条件元素i过滤列表