简体   繁体   English

尝试反转数组时出现ArrayIndexOutOfBoundsException

[英]ArrayIndexOutOfBoundsException when trying to reverse an array

This seems simple enough but I get the error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at reverse.main(reverse.java:28)" 这似乎很简单,但是我得到错误"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at reverse.main(reverse.java:28)"

I initially take inputs from the user to write an array, and then I want to print the array backwards. 最初,我从用户那里获取输入以编写一个数组,然后再向后打印该数组。 I understand there are other ways of doing this, but I mainly want to know why this is not working. 我知道还有其他方法可以做到这一点,但是我主要想知道为什么这不起作用。 Going through it line by line makes sense? 逐行通过它有意义吗?

PS. PS。 If it's not a problem, is there any better way of doing this? 如果这不是问题,那么还有其他更好的方法吗?

import java.util.Scanner;


public class reverse {

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.printf("Enter the number of values in array: ");
        Scanner scanner = new Scanner(System.in);
        int n;
        n = scanner.nextInt();

        double[] a1 = new double[n];
        int i;

        System.out.printf("Enter the value in the array: ");
        for (i = 0; i < n; i++){
            Scanner scanner2 = new Scanner(System.in);
            a1[i] = scanner2.nextInt();
            }
         double j;
         double k;

            for (i = 0; i < n/2; i++){
                j = a1[i];
                k = a1[n-i]; //error line;
                a1[i]=k;
                a1[n-i]=j;
            }
         for(i = 0; i < n; i++){
        System.out.println(" "+a1[i]);
    }}


}

When i = 0, ni will result in n, which is one larger than the available indexes( 0 -> n-1 ). 当i = 0时,ni将得出n,它比可用索引(0-> n-1)大1。

for (i = 0; i < n/2; i++){
  j = a1[i];
  k = a1[n-i]; //error line;
  a1[i]=k;
  a1[n-i]=j;
}
Collections.reverse(Arrays.asList(array))

Will reverse an array for you, then just print its values out. 将为您反转数组,然后仅打印其值。 It's great to do these kinds of problems as exercises but if you're ever woring in the industry it's usually better to rely on the Java API for trivial things like this. 将这类问题作为练习来做是很棒的,但是如果您对此行业感到不安,通常最好使用Java API来处理这类琐碎的事情。 Probably going to be faster and a lot more simpler than anything you can come up with. 可能会比您想出的任何东西都更快,更简单。

As said by Samhain, when i = 0 , then ni == n , which is greater than the last index of the array (since arrays start with index 0). 正如Samhain所说,当i = 0 ,则ni == n ,该值大于数组的最后一个索引(因为数组从索引0开始)。

The simplest solution is to just subtract an additional 1 from ni . 最简单的解决方案是从ni减去一个额外的1。

j = a1[i];
k = a1[n-i-1];
a1[i]=k;
a1[n-i-1]=j;

Also, creating a new Scanner is totally unnecessary. 另外,完全不需要创建新的Scanner Just continue to use the first one you created. 只需继续使用您创建的第一个即可。

for (i = 0; i < n; i++){
    a1[i] = scanner.nextInt();
}

Finally, for what it's worth, if you're using nextInt you don't need to declare your array as a double[] (nor do j and k need to be doubles). 最后,对于它的价值,如果您使用nextInt ,则无需将数组声明为double[]jk不必为double)。 You can just use ints. 您可以只使用整数。

Here's it running on ideone . 这是在ideone上运行的。

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

相关问题 列印阵列背面时出现ArrayIndexOutOfBoundsException - ArrayIndexOutOfBoundsException when printing the reverse of an array 我得到ArrayIndexOutofBoundsException - 反转数组 - I get ArrayIndexOutofBoundsException - reverse the array 尝试创建用户输入的数组列表时出现ArrayIndexOutOfBoundsException - ArrayIndexOutOfBoundsException when trying to create an array list of user input 尝试查找二维数组/矩阵的行列式时出现 ArrayIndexOutOfBoundsException - ArrayIndexOutOfBoundsException when trying to find the determinant of a 2D Array/Matrix 检查数组时出现ArrayIndexOutOfBoundsException? - ArrayIndexOutOfBoundsException when checking an array? 尝试访问数组后出现ArrayIndexOutOfBoundsException - ArrayIndexOutOfBoundsException after trying to access array 尝试复制矩阵时出现ArrayIndexOutOfBoundsException - ArrayIndexOutOfBoundsException when trying to copy a matrix 尝试清空JTable时出现ArrayIndexOutOfBoundsException - ArrayIndexOutOfBoundsException when trying to empty a JTable 遍历数组时出现ArrayIndexOutOfBoundsException - ArrayIndexOutOfBoundsException when iterating through an array 尝试自动递增数据时获取“ ArrayIndexOutOfBoundsException” - Getting an “ArrayIndexOutOfBoundsException” when trying to automatically increment data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM