gpt4 book ai didi

java - Shuffle 在 Java 中不工作

转载 作者:行者123 更新时间:2023-11-30 07:44:00 25 4
gpt4 key购买 nike

我正在尝试为一个项目编写代码。该项目包括添加、删除、打乱和重新排序(移动)字符串。我目前陷入随机播放状态。我正在使用 import java.util.Collections 并且我陷入困境。我的意思是通过使用下面的代码我得到一个异常:

private static void ShuffleTrack(int[] array) {
Collections.swap(mp3Player, 0, 1);
System.out.println(""+mp3Player);
}

这是我遇到的异常:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at java.util.Collections.swap(Collections.java:497)
at mp3Player.ShuffleTrack(mp3Player.java:78)
at mp3Player.main(mp3Player.java:40)

我在实际程序中没有收到任何错误,只是在控制台中收到任何错误。

这是代码

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class mp3Player {
public static Scanner in=new Scanner(System.in);

public static ArrayList<Track> mp3Player = new ArrayList<Track>();

public static void main(String[] args ){
//declare variables
int menu = 0;

do{
System.out.println("***WELCOME***");
System.out.println(" This is my MP3 Album ");
System.out.println("Please Choose one of the following options:");
System.out.println("1.Add a Track");
System.out.println("2.Search for a Track");
System.out.println("3.Delete an exisiting Track");
System.out.println("4.Shuffle Tracks");
System.out.println("5.Reorder Tracks");

menu = in.nextInt(); //Record user choice

switch(menu){
case 1:
InsertTrack();
break;

//case 2:
//SearchTrack();
// break;

//case 3:
//DeleteTrack();
//break;

case 4:
ShuffleTrack(null);
//break;

case 5:
//ReorderTrack();
//break;


}//end of switch

} while(menu !=0); //End of loop

} // End of static void

private static void InsertTrack(){

Track newtrack = new Track();

System.out.println("Track Number: ");
int Tracknumber = in.nextInt();

System.out.println("Artist Name: ");
String TrackArtist = in.next();

System.out.println("Track Name: ");
String TrackName = in.next();

System.out.println("Track Duration: ");
String TrackDuration = in.next();

System.out.println("Your Track has been added.");

newtrack.setTrack(Tracknumber, TrackArtist, TrackName, TrackDuration);

}//End of InsertTrack

private static void ShuffleTrack(int[] array){

Collections.shuffle(Arrays.asList(mp3Player));

}//End Of ShuffleTrack


//Set Attributes
private String InsertTrack ="";
private String SearchTrack ="";
private String DeleteTrack = "";
private String ShuffleTrack = "";
private String ReorderTrack = "";

// Setting Methods
public void setMp3(String InsertTrack, String SearchTrack, String DeleteTrack, String ShuffleTrack, String ReorderTrack){


this.InsertTrack = InsertTrack;
this.SearchTrack = SearchTrack;
this.DeleteTrack = DeleteTrack;
this.ShuffleTrack = ShuffleTrack;
this.ReorderTrack = ReorderTrack;

}// End of Method

public String getInsertTrack(){
return InsertTrack;
}
public String getSearchTrack(){
return SearchTrack;
}
public String getDeleteTrack(){
return DeleteTrack;
}
public String getShuffleTrack(){
return ShuffleTrack;
}
public String ReorderTrack(){
return ReorderTrack;
}

}//类(class)结束

最佳答案

为什么不简单地使用Collections.shuffle()

它看起来像这样:

Collections.shuffle(Arrays.asList(array));

关于java - Shuffle 在 Java 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34190429/

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