2013-03-24 110 views
0

我有一个数组错误,说它出界了,我不知道什么是错的。这里是我的代码:字符串数组错误

import java.util.*; 
public class gameVar { 
    public static int size; 
    public static int counter; 
    public static Scanner input = new Scanner(System.in); 
    public static String currentIn; 
    public static String nameArray[] = new String[size]; 
} 

和第二类(这里我得到第6行)中的错误:

public class mainThread extends gameVar { 
public static void main(String[] args){ 
    System.out.println("Please type the desired amount of players: "); 
    size = input.nextInt(); 
    for(int counter = 0; counter < size; counter++){ 
     System.out.println("Please enter the name of player " + nameArray[counter]) 
     } 
    } 
} 

你的帮助深表感谢!

+0

确定'nameArray [计数器]'是在循环'counter'的每个值有效的价值? – Aiias 2013-03-24 08:17:33

回答

1

下分配一个零元素的数组:

public static int size; 
public static String nameArray[] = new String[size]; // <<<< Here, `size` is zero 

你需要数组初始化进入main()

public static void main(String[] args){ 
    System.out.println("Please type the desired amount of players: "); 
    size = input.nextInt(); 
    nameArray = new String[size]; // <<< THIS 
    for(int counter = 0; counter < size; counter++){ 
     System.out.println("Please enter the name of player " + nameArray[counter]) 
     } 
    } 
} 

然后,您可以从nameArray声明删除= new String[size]

0

在获取所需大小后,您没有重新初始化数组类。你的设计作为一个整体需要一些工作,因为你过度依赖静态(全局)状态。

在gameVar:

public static int size; // <-- by default this is zero 
public static String nameArray[] = new String[size]; // <-- Initialized here to size zero! 

在mainThread:

size = input.nextInt(); // <-- size is no longer zero 
for(int counter = 0; counter < size; counter++) { 
    System.out.println("Please enter the name of player " + nameArray[counter]); // <-- But your array is still size zero! 
} 

简单的修复会得到新的大小后,要做到这一点:

nameArray = new String[size]; 

但正如我之前提到的,你应该重新考虑你的设计(设计一个没有静态变量的适当类)。

+0

好吧我对此很陌生,但是谢谢,我现在明白了。 – 2013-03-24 08:28:12

0

在你的领域的声明,当你设置public static int size; Java的默认大小为0。所以,当你创建你的字符串数组,数组的大小为0

一般来说,不建议在创建新对象现场声明。相反,只是有

public static String nameArray[]; 

然后设置nameArray到一个新的String数组后,你知道的大小将是什么。

size = input.nextInt(); 
nameArray[] = new String[size]; 
for(...... 
+0

好的,谢谢你的帮助! – 2013-03-24 08:26:18

0

Array尺寸在本质上是静态的,nameArray声明了sizestatic size variable没有初始化,它会得到默认的0

所以nameArray[counter]必须填充数组出界异常。

您必须以正确的大小初始化数组。

nameArray = new String[size]; 
+0

好的非常感谢你! – 2013-03-24 08:25:58

0

MainThread类应该是这样的:

public class mainThread extends gameVar { 
public static void main(String[] args){ 
    System.out.println("Please type the desired amount of players: "); 
    size = input.nextInt(); 
    nameArray = new String[size];//reinitialize the nameArray here. 
    for(int counter = 0; counter < size; counter++){ 
     System.out.println("Please enter the name of player " + nameArray[counter]=input.next()) 
     } 
    } 
} 
+0

好的谢谢你的帮助。 – 2013-03-24 08:25:16

0

大小的值是由defualt为零。所以数组被创建为0。 如:public static String nameArray[] = new String[0];

因此,你得到的异常。为变量大小分配一些值。

+0

好的,谢谢!这帮助了我。 – 2013-03-24 08:25:33

+0

如果它对你有帮助,你可以评价答案。 :-) – 2013-03-24 08:27:11

0

在初始化它的大小被设置为0

公共静态字符串nameArray [] =新的String [大小]的阵列;

如果改变大小后者 大小= input.nextInt的值();

它改变整型变量的大小的值,但不改变数组的大小,这就是你得到的错误。你可以通过打印数组的大小(nameArray.length)来检查它。

你需要让“大小”的值后对其进行初始化。