Java 索引越界问题

Java 索引越界问题,java,arraylist,indexoutofboundsexception,Java,Arraylist,Indexoutofboundsexception,我正在写一个小应用程序,它可以生成世界杯预选赛抽签。该应用程序有6个列表(pot1、pot2、pot3等),每个列表由9个团队组成,9个组(group1、group2等)的列表由6个团队组成。我试图从每个pot列表中删除一个团队,并将其放入组列表中 例如:从列表pot1中选择一个团队,将其从该列表中删除并放入列表组1 我目前可以从每个列表中删除一个团队并将其放入一个组中,但随后我遇到了以下问题: Exception in thread "main" java.lang.IndexOutOfBou

我正在写一个小应用程序,它可以生成世界杯预选赛抽签。该应用程序有6个列表(pot1、pot2、pot3等),每个列表由9个团队组成,9个组(group1、group2等)的列表由6个团队组成。我试图从每个pot列表中删除一个团队,并将其放入组列表中

例如:从列表pot1中选择一个团队,将其从该列表中删除并放入列表组1

我目前可以从每个列表中删除一个团队并将其放入一个组中,但随后我遇到了以下问题:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 6, Size: 6.
下面的代码显示了我到目前为止的工作。对于组列表中的每个组,我希望从每个pot列表中删除一个组,并将其放入第一个组中。然后,一旦我从每个pot中删除了一个团队,我想开始添加到第二组列表中,依此类推,直到每个组由6个团队组成。 我认为我遇到的问题是因为pot列表只包含6个团队,这是造成问题的原因。第一,我认为这是问题所在,对吗?第二,有人知道如何解决这个问题吗?提前谢谢

    for(ArrayList<String> lists : groups){
            for(int x = 0; x < 9 ; x++ ){   
                System.out.println( "\nthis is x : " +x);
                runDraw(groups, pots,x);    
            }
    }

public static void runDraw(List groups, List pots, int z){
    x = 0;
    Collections.shuffle((List<String>) pots.get(z));
    System.out.println("this is  z : " + z);

    System.out.println("\nTeams in Pot " + y +" : " +pots.get(z).toString());

    String[] chosen3 = new String[8];
    chosen3[x] = ((List<String>) pots.get(z)).remove(x);
    System.out.println( "\nThe team selected from Pot " + y  +" is : " + chosen3[x].toUpperCase() + " and they will go into Group " + y);

     //pot1.add(chosen3[i]);
    ((List<String>) groups.get(x)).add(chosen3[x]);

     String t = groups.get(x).toString();
     System.out.println( "Group " + y  +" contains : " + t.substring(1, t.length()-1));

     String s = pots.get(z).toString();
     System.out.println("\nRemaining teams in Pot "+ y +" are : "  + s.substring(1, s.length()-1)); //s.substring(1, s.length()-1) - removes brackets when printing list

     y++;       
}
for(数组列表:组){
对于(intx=0;x<9;x++){
System.out.println(“\n这是x:+x”);
runDraw(组、盆、x);
}
}
公共静态void runDraw(列表组、列表pots、int z){
x=0;
Collections.shuffle((List)pots.get(z));
System.out.println(“这是z:+z”);
System.out.println(“\n池中的内存”+y+:“+pots.get(z.toString());
String[]chosen3=新字符串[8];
chosen3[x]=((列表)pots.get(z)).remove(x);
System.out.println(“\n从Pot“+y+”中选择的团队是:“+chosen3[x].toUpperCase()+”,他们将进入组“+y”);
//pot1.add(chosen3[i]);
((List)groups.get(x)).add(chosen3[x]);
字符串t=groups.get(x).toString();
System.out.println(“组”+y+”包含:“+t.substring(1,t.length()-1));
字符串s=pots.get(z).toString();
System.out.println(“\n锅中的维护团队”+y+”是:“+s.substring(1,s.length()-1));//s.substring(1,s.length()-1)-打印列表时删除括号
y++;
}
更改:

String[] chosen3 = new String[8];
致:

或更改:

for(int x = 0; x < 9 ; x++ )
for(int x=0;x<9;x++)
致:

for(int x=0;x<8;x++)

最好将这个值(8或9)发送到方法
runDraw
,并相应地分配数组。

for(int x=0;x<9;x++)
应该是
for(int x=0;x<8;x++)
x
被传递到
runDraw
,然后您尝试
chosen3[x]
,其中
chosen3
的长度为8。您能否提供带有IndexOutOfBoundException的stacktrace?不要在代码中使用魔法值:
对于(int x=0;x<9;x++)
,这里的
9
应该是一个常量,或者使用类似
length()
size()
的方法,具体取决于您使用的对象类型。您好,这是导致问题的行:Collections.shuffle((List)pots.get(z));谁是落选者?我看这个答案没有错。
for(int x = 0; x < 9 ; x++ )
for(int x = 0; x < 8 ; x++ )