gpt4 book ai didi

java - java中从文件中删除对象

转载 作者:行者123 更新时间:2023-12-01 16:53:10 25 4
gpt4 key购买 nike

示例代码:

else if (ninput == 4) {
System.out.println("Select a method\n");
System.out.println("1) Remove recording by index\n");
System.out.println("2) Remove recording by name\n");

sinput = scan.next();
ninput = Integer.parseInt(sinput);

if (ninput == 1) {
System.out.println("INDEX ");
for (int i = 0; i < Users.get(loginID).User_Playlist.numberOfRecordings; i ++) {
System.out.println((i+1)+ ") "+ Users.get(loginID).User_Playlist.recordingList.get(i) +"\n");
}
System.out.println("Enter the index of the recording to remove: ");
sinput = scan.next();
ninput = Integer.parseInt(sinput);
Users.get(loginID).User_Playlist.recordingList.remove(ninput - 1);
Users.get(loginID).User_Playlist.numberOfRecordings--;

System.out.println(Users.get(loginID).User_Playlist.toString());
}
if (ninput == 2) {
for (int i = 0; i < Users.get(loginID).User_Playlist.numberOfRecordings; i ++) {
System.out.println((i+1)+ ") "+ Users.get(loginID).User_Playlist.recordingList.get(i) +"\n");
}
System.out.println("Enter the name of the recording to remove: ");
sinput = scan.next();



}

}

需要修复的代码:

if (ninput == 2) {
for (int i = 0; i < Users.get(loginID).User_Playlist.numberOfRecordings; i ++) {
System.out.println((i+1)+ ") "+ Users.get(loginID).User_Playlist.recordingList.get(i) +"\n");
}
System.out.println("Enter the name of the recording to remove: ");
sinput = scan.next();



}

问题:第一种方法根据播放列表中歌曲的索引从播放列表中删除歌曲。第二种不完整的方法是根据歌曲名称从播放列表文件中删除歌曲。有谁知道如何根据对象的名称从文件中删除单个项目?

最佳答案

您获取名称,循环播放所有歌曲,并将获取的名称与每首歌曲的名称进行比较。一旦找到那个,就把它拿出来(你有它的索引 - 它是循环计数器)并跳出循环。这是最基本的方法。

关于java - java中从文件中删除对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61646515/

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