简体   繁体   English

Java阵列概念

[英]Java Array concept

 for(int m=0; m< checkBoxValue.length ; m++)
 {
    System.out.println("check box in model class"+checkBoxValue[m]+"\n");

 }

This loop is to print two values in array. 此循环将在数组中打印两个值。 It prints the values But after that it shows array out of bound exception 它输出值,但之后显示数组超出范围的异常

It seems you're on the wrong track. 看来您走错了路。 It's best to set a breaking point on your for loop and debug your code, then go through it step wise. 最好在for循环上设置一个断点并调试代码,然后逐步进行操作。 This will reveal where the exception is thrown... 这将揭示引发异常的位置...

Especially since you say " after that ", you might want to review your code after that for loop :-) 特别是因为您说“ after that ”,因此您可能要 for loop 之后查看代码:-)

您确定在这里引发了异常吗?

Ohh.. Looks like a mess. 哦..看起来像一团糟。 The information looks very abstract. 该信息看起来非常抽象。 You need to be specific, may be you can give more code over here. 您需要具体说明,也许您可​​以在此处提供更多代码。 One possible cause I think of, may be, is Multi-threading. 我想到的一个可能的原因可能是多线程。

Only multi-threaded application can do this trick . 只有多线程应用程序才能做到这一点 If so, you better provide synchronization on the origin object of checkBoxValue variable. 如果是这样,则最好在checkBoxValue变量的原始对象上提供同步。

Hope that helps.... 希望有帮助...

只要您正确完成了数组初始化,代码就可以正常工作。

The posted code should not throw ArrayIndexOutOfBoundsException. 发布的代码不应引发ArrayIndexOutOfBoundsException。 Most likely, you are doing something after the loop which accesses an incorrect index of an array. 最有可能的是,您在循环之后正在执行访问数组的错误索引的操作。

The only way that the code shown in the question could throw an ArrayIndexOutOfBoundsException is if the toString() method of one of the checkBoxValue[m] objects throws the exception. 问题中显示的代码可能引发ArrayIndexOutOfBoundsException的唯一方法是checkBoxValue [m]对象之一的toString()方法是否引发异常。

Maybe you have overridden the toString() method of the checkBoxValue -class (the array initializer would help identifying this class). 也许您已经覆盖了checkBoxValue -class的toString()方法(数组初始化程序将有助于识别此类)。 Following this theory, the toString() implementation might work fine for the first two elements of the array (they are printed) and may throw an exception for the third element in the array. 按照这种理论, toString()实现对于数组的前两个元素(打印出来)可能工作正常,并且可能为数组中的第三个元素抛出异常。

This could lead to the error description: This loop is to print two values in array. 这可能会导致错误描述: 此循环将在数组中打印两个值。 It prints the values But after that it shows array out of bound exception 它输出值,但之后显示数组超出范围的异常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM