为什么我的索引越界异常? [英] Why am I getting index out of bounds exception?
问题描述
我已经制作了这段代码来将整个以 10 为基数的数字转换为二进制.我相信代码是它需要的一切,但我无法让 ArrayList
工作.
我在这个网站上花了几个小时,其他人尝试了无数次更改,但无济于事.
我已经编译了没有错误的代码,但是一旦我运行它并输入int
,程序就会崩溃.
代码如下:
import java.util.Scanner;导入 java.util.ArrayList;公共类 BinaryConverter {公共静态无效主(字符串 [] args){扫描仪输入 = new Scanner(System.in);ArrayList<整数>binary = new ArrayList();int a = in.nextInt();二进制 = binaryConverter(a);for (int i = binary.size(); i > -1; i = i - 1){System.out.print(binary.get(i));}}公共静态 ArrayListbinaryConverter(整数n){ArrayList<整数>余数 = 新的 ArrayList();而 (n/2 != 0) {余数.add(n%2);n = n/2;}余数.add(n%2);返回余数;}}
这些是当我输入一个数字时 java 向我抛出的异常.
Jons-iMac:java jon$ java BinaryConverter142线程main"中的异常java.lang.IndexOutOfBoundsException:长度为 8 的索引 8 越界在 java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)在 java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)在 java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)在 java.base/java.util.Objects.checkIndex(Objects.java:372)在 java.base/java.util.ArrayList.get(ArrayList.java:440)在 BinaryConverter.main(BinaryConverter.java:16)
我希望这是足够的信息.
在你的代码中,你get
通过被错误评估的索引值.
for (int i = binary.size(); i > -1; i = i - 1){System.out.print(binary.get(i));}
binary.size()
的赋值确实越界了,因为索引从 binary.size()
开始计数到零,但是超出了被绑定的范围从零到 binary.size()-1
.
I've made this code to convert whole base 10 numbers into binary. The code I believe is everything that it needs to be but I can't get ArrayList
to work.
I've spent a few hours on this site and other trying countless changes to no avail.
I've gotten the code to compile without errors but as soon as I run it and enter an int
the program crashes.
Here is the code:
import java.util.Scanner;
import java.util.ArrayList;
public class BinaryConverter {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList<Integer> binary = new ArrayList<Integer>();
int a = in.nextInt();
binary = binaryConverter(a);
for (int i = binary.size(); i > -1; i = i - 1){
System.out.print(binary.get(i));
}
}
public static ArrayList<Integer> binaryConverter(Integer n) {
ArrayList<Integer> remainder = new ArrayList<Integer>();
while (n/2 != 0) {
remainder.add(n%2);
n = n/2;
}
remainder.add(n%2);
return remainder;
}
}
These are the exceptions java throws at me when I input a number.
Jons-iMac:java jon$ java BinaryConverter
142
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 8 out-of-bounds for length 8
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.get(ArrayList.java:440)
at BinaryConverter.main(BinaryConverter.java:16)
I hope this is enough information.
In your code you get
the value by index which is evaluated incorrectly.
for (int i = binary.size(); i > -1; i = i - 1){ System.out.print(binary.get(i)); }
Assignment of binary.size()
indeed out of bounds, because indexes counted from binary.size()
to zero, but it is out of range which is bound from zero to binary.size()-1
.
这篇关于为什么我的索引越界异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!