在类中使用链接列表的java.util.ConcurrentModificationException

问题描述

enter image description here

打包PlaylistForSongs;

import java.util.ArrayList; 导入java.util.Scanner;

公共类主要{

private static ArrayList<Album> listOfAlbums = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {

    Album album = new Album("Fruit du démon","Soolking");
    album.addSong("Milano",400);
    album.addSong("Guerilla",433);
    album.addSong("Dalida",233);
    album.addSong("Liberté",323);
    album.addSong("Zemër",432);
    album.addSong("Espérance",123);
    listOfAlbums.add(album);

    album = new Album("Dellali","Cheb Mami");
    album.addSong("Le Raï c'est Chic",300);
    album.addSong("Ana Oualache",533);
    album.addSong("Zarartou",433);
    album.addSong("Khalouni",423);
    album.addSong("Ma Vie 2 Fois",432);
    album.addSong("Machi Chaba",123);
    album.addSong("Chaba",1223);
    listOfAlbums.add(album);

    PlayList myFavorite = new PlayList("My favorite",listOfAlbums);
    System.out.println("**************************************************************************");
    myFavorite.addSong("Machi Chaba");
    myFavorite.addSong("Zemër");
    myFavorite.addSong("Ana Oualache");
    myFavorite.addSong("ntia Machi Chaba");

    boolean quit = false;
    printChoice();
    System.out.print("your choice is: ");

    while (!quit) {
        int choice = scanner.nextInt();
        scanner.nextLine();
        switch (choice - 1) {
            case 0:
                quit = true;
                System.out.println("closing the play list ...");
                break;
            case 1:
                myFavorite.playNext();
                break;
            case 2:
                myFavorite.playBackward();
                break;
            case 3:
                myFavorite.replaySong();
                break;
            case 4:
                printChoice();
                break;
        }


    }
}

private static void printAlbums() {
    System.out.println("\nAlbums:\n************************");
    for (Album a : listOfAlbums) {
        System.out.println(a.getName());
    }
    System.out.println("\n************************");
}

public static void printChoice() {
    System.out.println("choices: \n" +
            "1- Quit\n" +
            "2- Skip forward to the next song\n" +
            "3- skip backwards to a previous song.\n" +
            "4- Replay the current song.\n" +
            "5- Print Choices");
}

}

打包PlaylistForSongs;

公开课歌曲{

private String title;
private int duration;

public Song(String title,int duration) {
    this.title = title;
    this.duration = duration;
}

public String getTitle() {
    return title;
}

public String getDuration() {

    int hours;
    int minutes;
    int seconds;

    seconds = duration;
    minutes = seconds / 60;
    seconds %= 60;
    if( minutes > 60 ){
        hours = minutes / 60;
        minutes %= 60;
    }else{
        hours = 0;
    }

    return hours + " : " + minutes + " : " + seconds;
}

@Override
public String toString() {
    return this.title + ": " + this.getDuration();
}

}

打包PlaylistForSongs;

import java.util.ArrayList;

公共课相册{

private ArrayList<Song> songsList;
private String name;
private String artist;
private int numberOfSongs;


public Album(String name,String artist) {
    this.songsList = new ArrayList<>();
    this.name = name;
    this.artist = artist;
    this.numberOfSongs = 0;
}

public String getName() {
    return name;
}

public int getNumberOfSongs() {
    return numberOfSongs;
}

public boolean addSong( Song song ){
    for ( Song s : this.songsList ) {
        if( s.equals(song) ){
            System.out.println("The song " + song + " Already exist in the album" + this.name);
            return false;
        }
    }
    songsList.add(song);
    System.out.println("The song " + song + " added successfully in " + this.name);
    this.numberOfSongs ++;
    return true;
}

public boolean addSong( String title,int duration){
    for( Song s : this.songsList){
        if( s.getTitle().equals(title)){
            System.out.println("The song " + title + " Already exist in the album" + this.name);
            return false;
        }
    }
    songsList.add(new Song(title,duration));
    System.out.println("The song " + title + " added successfully in " + this.name);
    this.numberOfSongs ++;
    return true;
}

public void show(){
    System.out.println("\nThe album " + this.name + " has " + this.numberOfSongs + "\nThe songs: ");
    for( Song s : this.songsList){
        System.out.println(s.toString());
    }
}

public Song getSong( String Title){
    for( int i = 0; i < songsList.size(); i++){
        if(songsList.get(i).getTitle().equals(Title)){
            return songsList.get(i);
        }
    }
    System.out.println("the song " + Title + " doesn't exist in album " + this.name + " from album");
    return null;
}

public boolean checkSong( String songTitle){
    for( int i = 0; i < songsList.size(); i++){
        if(songsList.get(i).getTitle().equals(songTitle)){
            return true;
        }
    }
    return false;
}
private boolean checkSong( Song song){
    for( int i = 0; i < songsList.size(); i++){
        if(songsList.get(i).getTitle().equals(song.getTitle())){
            return true;
        }
    }
    return false;
}

}

打包PlaylistForSongs;

导入java.util。*;

公共类PlayList {

private String name;
private LinkedList<Song> songsInPlay;
private ArrayList<Album> albumList;
private boolean forward;
private ListIterator<Song> songsInPlayIterator ;

public PlayList(String name,ArrayList<Album> albumList) {
    this.name = name;
    this.songsInPlay = new LinkedList<>();
    this.albumList = albumList;
    this.forward = true;
    songsInPlayIterator = songsInPlay.listIterator();
}

public void addSong(String title) {
    if (findSong(title) != null) {
        this.songsInPlay.add(findSong(title));
        System.out.println("The song " + title + " has been added successfully to the Play List: " + name);
    } else {
        System.out.println("we can't add the song " + title + " it doesn't exist in our albums");
    }
}

private Song findSong(String songName) {
    for (Album album : albumList) {
       if( album.checkSong(songName)){
            return album.getSong(songName);
        }
    }
    return null;
}

public void playNext(){

    if(songsInPlayIterator.hasNext()){
        System.out.println("the song: " + songsInPlayIterator.next() + "in play");
        this.forward = true;
    }
}

public void playBackward(){
    if(songsInPlayIterator.hasPrevious() && forward){
        songsInPlayIterator.previous();
        forward = false;
    }
    System.out.println("the song: " + songsInPlayIterator.previous() + "in play");
}

public void replaySong(){
    if(songsInPlayIterator.hasPrevious() && forward){
        System.out.println("the song: " + songsInPlayIterator.previous() + "in play");
        forward = false;
    }
    if (songsInPlayIterator.hasNext() && !forward){
        System.out.println("the song: " + songsInPlayIterator.next() + "in play");
        forward = true;
    }
}

这是错误 线程“主”中的异常java.util.ConcurrentModificationException 在java.base / java.util.LinkedList $ ListItr.checkForComodification(LinkedList.java:970) 在java.base / java.util.LinkedList $ ListItr.next(LinkedList.java:892) 在PlaylistForSongs.PlayList.playNext(PlayList.java:42) 在PlaylistForSongs.Main.main(Main.java:52)

在播放列表中引入以下语句

 songsInPlayIterator = songsInPlay.listIterator();

第40、47、55行之后 这样做之后,我得到这个结果 enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...