无需迭代即可从 Java ArrayList 中删除 [英] Removing from Java ArrayList without iteration

查看:25
本文介绍了无需迭代即可从 Java ArrayList 中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一项任务,但找不到使这项工作正常进行的方法.我有一个包含 5 个学生信息的 ArrayList.我需要构造一个方法来删除其中一个对象,但我不需要/想要迭代,因为它是静态删除.我有以下代码:

import java.util.ArrayList;导入 java.lang.String;公开课名册{静态 ArrayListmyRoster = new ArrayList<>();公共静态无效主(字符串 [] args){//添加学生数据Roster.add("1","John","Smith");添加(2",苏珊",埃里克森");添加(3",杰克",那不勒斯");添加(4",艾琳",黑色");添加(5",杰森",拉普");删除(3");删除(3");}public static void add(String studentID, String fName, String lName){学生 newStudent = new Student(studentID, fName, lName);myRoster.add(newStudent);}公共静态无效删除(字符串学生ID){如果(myRoster.contains(studentID)){Roster.remove(studentID);}别的 {System.out.println("该学生证不存在.");}}

当我编译代码时,没有删除任何条目,并且 else 语句为两个 remove 调用触发.

解决方案

myRosterStudent 对象的 List.List containsremove 将只针对类似对象,而不是 String 值 - 它不知道如何匹配一个 String 针对一个 Student.

作为替代方案,您可以利用 ListStream 支持和 filterList匹配 studentID 的元素,然后删除所有这些元素,例如...

public static void remove(String studentID) {列表<学生>匹配 = myRoster.stream().filter(student -> student.getStudentID().equals(studentID)).collect(Collectors.toList());myRoster.removeAll(匹配);}

<块引用>

但我不需要/想要迭代

只是你理解,还有一个迭代被执行了,只是你不是自己做的,API是

I am working on an assignment, and I can't find a way to make this work properly. I have an ArrayList with information on 5 students. I need to construct a method that will remove one of the objects, but I do not need/want iteration, as it is a static removal. I have the following code:

import java.util.ArrayList;
import java.lang.String; 

public class Roster {
static ArrayList<Student> myRoster = new ArrayList<>();

public static void main(String[] args){

//Add Student Data
    Roster.add("1","John","Smith");
    add("2","Suzan","Erickson");
    add("3","Jack","Napoli");
    add("4","Erin","Black");
    add("5","Jason","Rapp");

remove("3");
remove("3");

}

public static void add(String studentID, String fName, String lName){
    Student newStudent = new Student(studentID, fName, lName);
myRoster.add(newStudent);
}

public static void remove(String studentID){
    if (myRoster.contains(studentID)){
        Roster.remove(studentID);
    }
    else {
        System.out.println("This student ID does not exist.");
    }
}

When I compile the code, none of the entries are removed, and the else statement fires off for both remove calls.

解决方案

myRoster is a List of Student objects. List contains and remove will work against only like objects, not String values - it has no idea how to match a String against a Student.

As an alternative, you could make use of List's Stream support and filter the List for elements which match the studentID and then remove all those elements, for example...

public static void remove(String studentID) {
    List<Student> matches = myRoster.stream().filter(student -> student.getStudentID().equals(studentID)).collect(Collectors.toList());
    myRoster.removeAll(matches);
}

but I do not need/want iteration

Just so you understand, there is still an iteration been performed, it's just that you're not doing it yourself, the API is

这篇关于无需迭代即可从 Java ArrayList 中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆