gpt4 book ai didi

Java 集合操作

转载 作者:行者123 更新时间:2023-12-01 17:29:02 24 4
gpt4 key购买 nike

  1. 是否有比我在 add 方法中编写的更好的方法将列表添加到另一个现有列表?
  2. 从播放列表中删除歌曲列表的方法是否正确,或者我是否会遇到并发修改异常?

    class Playlist
    {
    List<Song> playList;
    public void addSongs(List<Song> songs)
    {
    for(Song s:songs)
    playList.add(s);
    }

    public void removeSongs(List<Song> songs)
    {
    for(Song s:songs)
    if(playList.contains(s))
    playList.remove(s);
    }


    }

最佳答案

要将一个列表中的所有元素添加到另一个列表中,请使用;

List.addAll

Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator

从另一个用途中删除一个列表中的所有元素;

List.removeAll

Removes from this list all of its elements that are contained in the specified collection

关于Java 集合操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12831028/

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