简体   繁体   English

使用“ for循环”添加数组值时发生异常

[英]exception occurs while adding array values using “for loop”

I have some code here 我这里有一些代码

 String array[]=new String[5];
    for(int i=0;i<array.length;i++){
        array[i]= "item "+String.valueOf(i);
        i++;
    }

and this is log message what i got after application crash 这是我在应用程序崩溃后得到的日志消息

java.lang.IndexOutOfBoundsException : Invalid array range: 5 to 5

Could you guys explain me why pls ? 你们能解释一下为什么吗? I just want to declare an array and use for loop to initialize array element, just dont know why my code didn't work . 我只想声明一个数组并使用for循环初始化数组元素,只是不知道为什么我的代码不起作用。 Thank you 谢谢

because key index More increases than index array in your loop . 因为键索引比循环中的索引数组增加更多。

String array[]=new String[5];
    for(int i=0;i<array.length;i++){
        array[i]= "item "+String.valueOf(i);

    }

remove extra i++.. it already redundant except you have some purpose to do so. 删除多余的i ++ ..它已经是多余的,除非您有一些目的。

String array[]=new String[5];
for(int i=0;i<array.length;i++){
        array[i]= "item "+String.valueOf(i);
        //i++; //remove this.. it already redundant except you have some purpose
}

its java.lang.IndexOutOfBoundsException : Invalid array range: 5 to 5 because of the counter already exceed size of your string which 5 only.. java.lang.IndexOutOfBoundsException : Invalid array range: 5 to 5因为计数器已经超出了您的字符串大小(仅5)。

use ArrayList instead 使用ArrayList代替

Just do this: 只要这样做:

String array[]=new String[5];
for(int i=0;i<array.length;i++){
    array[i]= "item "+String.valueOf(i);
}

That second i++ was causing your index out of bounds. 第二个i ++导致索引超出范围。

Don't increment the 'i' value twice, which is causing this issue. 不要将“ i”值增加两次,这会导致此问题。

String array[]=new String[5];
for(int i=0;i<array.length;i++){
        array[i]= "item "+String.valueOf(i);
        //i++; //remove this..
}

Instead of using Array[] you can you Collections such as ArrayList<String> or List<String> . 除了使用Array[]还可以使用ArrayList<String>List<String> Collections

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

相关问题 使用 ImageIcon 添加图像时出现“线程中的异常”AWT-EventQueue-0“java.lang.NullPointerException” - "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" occurs while adding images using ImageIcon 使用for循环将双精度数组值添加到arraylist - Adding double array values to an arraylist using a for loop java空指针异常-发生在while循环中的不同点 - java null pointer exception- Occurs at varying points in while loop 使用标签数组在框架中添加标签时,出现空指针异常 - Null pointer exception while adding Labels in Frame using array of Labels 空指针异常仅在比较时发生,并且不打印出值 - Null Pointer Exception Occurs only while comparison and not printing out values 解析JSON时,会发生奇怪的异常,因为:这不是JSON数组 - While parsing a JSON, strange exception occurs as :This is not a JSON Array Java:使用文件阅读器在while循环中添加到字符串数组 - Java: adding to a string array in a while loop using a file reader 使用Java将项目存储到DynamoDB时发生ConditionalCheckFailed异常 - ConditionalCheckFailed Exception occurs while storing an item into DynamoDB using java 使用SwingWorker发生异常后,即使对于正确的值,线程也不执行 - Threads are not executing even for correct values after an exception occurs using SwingWorker 使用while循环进行异常处理 - Exception Handling using while loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM