我正在尝试为项目执行代码。该项目包括添加,删除,改组和重新排序(移动)字符串。
我目前陷入了洗牌。我正在使用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;
}
} //班级结束
答案 0 :(得分:1)
为什么不简单地使用Collections.shuffle()
?
它看起来像这样:
Collections.shuffle(Arrays.asList(array));
答案 1 :(得分:0)
管理让它工作, 错过了一些代码,这使得它不能保存在实际的数组中,通过在CreateTrack的末尾添加它来修复它
mp3Player.add(newtrack);