我的错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at beerregister.BeerRegister.list(BeerRegister.java:66)
我该如何解决?
我的代码有点丑,抱歉。
package beerregister;
import java.util.*;
import beerregister.BeerRegister.NameComparator;
public class BeerRegister{
public String name;
public String style;
public String alc;
List<BeerRegister> bb = new ArrayList<BeerRegister>();
int szamlalo = 0;
//private StyleComparator sc;
//private AlcComparator ac;
public BeerRegister(){
Scanner sc = new Scanner(System.in);
String cmd[];
System.out.println("Lökjed:");
while(true){
cmd = sc.nextLine().split(" ");
if(cmd[0].equals("add")) {add(cmd[1],cmd[2],cmd[3]);}
if(cmd[0].equals("list")) {list(cmd);}
}
}
public BeerRegister(String name, String style, String alc){
this.name=name;
this.style=style;
this.alc=alc;
}
public void add(String name,String style,String alc){
BeerRegister br = new BeerRegister(name, style, alc);
// br.name = name;
// br.style = style;
// br.alc = alc;
bb.add(br);
}
public void list(String[] cmd){
System.out.println(bb.size());
//synchronized(bb){}
if(cmd[1].equals("name"))
Collections.sort(bb,new NameComparator());
//if(cmd[1].equals("style")) StyleComparator();
//if(cmd[1].equals("alc")) AlcComparator();
for(int i = 0; i < bb.size();i++){
System.out.println(bb.get(i).name+ " "+ bb.get(i).style + " " + bb.get(i).alc);
}
}
class NameComparator implements Comparator<BeerRegister>{
@Override
public int compare(BeerRegister a, BeerRegister b) {
return a.name.compareToIgnoreCase(b.name);
}
}
public static void main( String [] args){
BeerRegister xyz = new BeerRegister();
}
}
那么我该如何解决我的问题呢?请帮忙。