为什么我得到索引越界异常? [英] Why am I getting index out of bounds exception?

查看:1685
本文介绍了为什么我得到索引越界异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了这个代码,将整个基数为10的数字转换成二进制数。我相信的代码是它需要的一切,但我不能让我使用ArrayLists。我花了几个小时在这个网站和其他尝试无数的变化无济于事。我已经得到了没有错误的编译代码但是一旦我输入一个int,程序崩溃了。



这是代码:

  import java.util.Scanner; 
import java.util.ArrayList;

公共类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);
返还余额;
}
}

这些是java输入时抛出的异常一个数字。

  Jons-iMac:java jon $ java BinaryConverter 
142

异常在线程main中java.lang.IndexOutOfBoundsException:java.base / jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)的索引8越界长度为8

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)

我希望这是足够的信息。

解决方案

在您的代码中,您获取索引值,该值被错误地评估。




  for(int i = binary.size();我> -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 me ArrayLists 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 and errors but as soon as I 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆