Java 集合按索引删除

Java 集合按索引删除,java,collections,Java,Collections,伙计们,我将如何使用removeobject;根据用户输入的id删除元素。我不知道如何实现indexOfobject;因为我的论点中有很多变量,所以我不能像indexOfid这样做; removeindexOfid;教我怎么做 public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); List<Item> items = new Ar

伙计们,我将如何使用removeobject;根据用户输入的id删除元素。我不知道如何实现indexOfobject;因为我的论点中有很多变量,所以我不能像indexOfid这样做; removeindexOfid;教我怎么做

public class Main {
public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    List<Item> items = new ArrayList<>();
    while (true) {
        System.out.println("ITEM MANAGEMENT");
        lineBreak(20, "*");
        System.out.println("[1] - Add Item");
        System.out.println("[2] - Delete Item");
        System.out.println("[3] - Edit Item");
        System.out.println("[4] - List All Items");
        System.out.println("[5] - View Item");
        System.out.println("[6] - Exit");
        lineBreak(20, "-");
        System.out.print("Enter your choice>> ");
        int choice = s.nextInt();
        int id;
        String name, des;
        float price;
        Item i;

        switch(choice){
            case 1:
                System.out.print("INPUT ID: ");
                id = s.nextInt();
                s.nextLine(); //trap
                System.out.print("INPUT NAME: ");
                name = s.nextLine();
                System.out.print("INPUT DESCRIPTION: ");
                des = s.nextLine();
                System.out.print("INPUT PRICE: ");
                price = s.nextFloat();
                i = new Item(id,price,name,des);
                items.add(i);
                break;
            case 2:
                System.out.println("ENTER ID: ");
                break;
            case 3:
                System.out.println("ENTER ID: ");
                break;
            case 4:
                System.out.printf("%-10s%-10s%10s\n","ID","NAME","PRICE");
                lineBreak(20," -");
                for(Item item: items){
                    System.out.printf("%-10d%-10s%10.2f\n",item.getId(),item.getName(),item.getPrice());
                }
                break;
            case 5:
                System.out.println("ENTER ID: ");
                break;
            case 6:
                System.exit(1);
            default:
                System.out.println("Invalid Input!");
        }
    }

}

public static void lineBreak(int n, String ch) {
    for (int i = 0; i < n; i++) {
        System.out.print(ch);
    }
    System.out.println("");
}
}
您应该使用映射而不是列表来存储项对象。在映射中,您可以根据给定的Id获取项目,并从映射中删除项目对象

如果您只需要使用列表,那么每次从列表中删除一个项目时,您都需要迭代列表。您可以使用以下代码删除列表中的项目。假设在Item类中有getId方法


考虑到您当前的实现,您需要在列表上循环并找到具有匹配id的项

类似于

int deleteId = s.nextInt();
Iterator<Item> it = items.iterator();
while (it.hasNext()) {
    Item next = it.next();
    if (next.getId() == deleteId) {
        it.remove();
    }
}
int deleteId = s.nextInt();
List<Item> results = items.stream().filter((Item t) -> { return t.getId() == deleteId; }).collect(Collectors.toList());
*另一种可能性

查找与所需id匹配的第一项

int deleteId = s.nextInt();
Item item = list.stream().filter(x -> 
    {
        return x.getId() == deleteId;
    })
.findFirst()
.get();
if (item != null) {
    items.remove(item);
}

这就是我将输入的ID与对象中的ID进行比较的方法。 它起作用了

                id = s.nextInt();
                for(Item itm: items){
                    if(id == itm.getId()){items.remove(itm);}
                }

让您的equals方法与idy进行比较您必须使用某种循环并找到具有相同idy的匹配项您可以使用类似List results=items.stream.filterItem t->{return t.getId==id;}.collectCollectors.toList;它将返回id等于查询的项的列表。结果列表的大小应为0或更大,但您可以简单地使用items.removeAllresults;从项目中删除结果中的元素…太复杂了,我无法理解哈哈。例如,我输入123作为id,苹果作为名称,我可以得到整数123的索引吗?然后使用它删除项目。删除123;移除123苹果?对我来说太复杂了,无法理解兄弟,我们还没有解决这个问题。我不能只用id获取对象的索引吗?例如indexOfid;那么该索引将用于remove.index?像这样简单的东西哈哈哈我不能只用id来获取对象的索引吗在当前实现中,必须使用循环,列表无法搜索对象的属性谢谢兄弟,我很感谢你的回答,它可以工作,但我们不能使用迭代器,我们还没有在java类中学到这一课,但谢谢。我也不知道哈哈。我找到了一个解决方案:id=s.nextInt;forItem itm:items{ifid==itm.getId{items.removeitm;}}如果无法使用迭代器,然后,您可以使用Item对象创建另一个列表,然后使用removeAll方法从原始列表中删除这些对象。Bro您可以帮助我如何说,如果在添加新项目时输入现有id,程序会说该id已经存在,而在删除itll时,如果输入的id不存在,程序也会说该id不存在同样存在于编辑中。我需要这样一个函数,我不知道在这种方法中如何获得ConcurrentModificationException异常。Java集合类是快速失效的,这意味着当某个线程遍历集合时,该集合是否会被更改。
int deleteId = s.nextInt();
Item item = list.stream().filter(x -> 
    {
        return x.getId() == deleteId;
    })
.findFirst()
.get();
if (item != null) {
    items.remove(item);
}
                id = s.nextInt();
                for(Item itm: items){
                    if(id == itm.getId()){items.remove(itm);}
                }