gpt4 book ai didi

java - 在 ArrayList 中找不到超过 99 的值

转载 作者:行者123 更新时间:2023-11-30 08:37:41 25 4
gpt4 key购买 nike

<分区>

当我尝试在我的 ArrayList 中查找并删除一个超过两位数字的值时,我的方法返回 -1(如果未找到该值)。然而,它适用于低于 100 的值。要使其工作,您可以在 ArrayList 中输入值的深度(索引),以及您希望找到的值。当您找到它们时,它会从数组中删除该值。出于某种原因,当值为三位或更多位时,if 语句不起作用。

这是我的代码:

import java.util.*;

public class EP {

public static List < Integer > items = new ArrayList < Integer > (Arrays.asList(12, 13, 48, 42, 38, 2827, 827, 828, 420));

public static void main(String[] args) {
moreLines();

System.out.println("Exam List");

for (Integer i: items) {
System.out.println(i);
}

moreLines();

Scanner scan = new Scanner(System.in);
System.out.println("Enter depth");

int depth = scan.nextInt();
moreLines();

System.out.println("Enter value");
int value = scan.nextInt();

moreLines();

System.out.println(mark(depth, value));
}

public static int mark(int depth, int value) {
int ret = -1; //This ensures -1 is returned if it cannot find it at the specified place

for (int i = 0; i < items.size(); i++) {
System.out.println(items.get(i));

/** This is the condition for the value deletion
* If depth and value are correct, it deletes the value
*/
if (items.get(depth) == (Integer) value) {
ret = value;
items.remove(items.get(depth)); //removes ArrayList Item
}
}

moreLines();

System.out.println("Updated Exam List"); //Shows ArrayList with deleted item

for (Integer j: items) {
System.out.println(j);
}

moreLines();

return ret;
}

public static void moreLines() { //Just there for debugging, makes spaces when you run it.
System.out.println(" ");
System.out.println(" ");
}
}

请原谅糟糕的评论和布局。在 StackExchange 中很难正确获取它,复制和粘贴效果不佳,所以我不得不手动将它隔开。谢谢:)

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com