如何修复此运行时错误?线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:-1

时间:2017-02-07 21:34:53

标签: java arrays runtime java.util.scanner indexoutofboundsexception

x.df

这是一个程序,它接受用户输入以获得10个整数来创建数组。我已完成所有程序,并且没有编译时错误,但是当我运行代码时,显示方法中存在索引超出范围(-1)的问题,并且我已经完成了尝试修复它的所有内容。谢谢!

2 个答案:

答案 0 :(得分:0)

您似乎忘了创建contains方法。

这是方法:

private static boolean contains(int[] nums, int j) {
        return false;
    }

这是你的整个代码测试,似乎它正在运行:

import java.util.Scanner;  //importing scanner to get user input

public class OutOfBound {

        public static void main(String[] args)
        {
            Scanner input = new Scanner(System.in);
            int[] hello = new int[10];
            for(int i = 0; i < 10; i++) //to get right number of integers
            {
                System.out.print("Please enter an integer: ");
                hello[i] = input.nextInt();
            }
    //printing everything out 
            display(hello);
            System.out.println();
            System.out.print("Evens: ");
            display(onlyEvens(hello));
            System.out.println();
            System.out.print("Positives: ");
            display(onlyPositives(hello));
            System.out.println();
            System.out.print("Odds: ");
            display(disjoint(hello ,onlyEvens(hello)));
            System.out.println();
            System.out.print("Negatives: ");
            display(disjoint(hello ,onlyPositives(hello)));
            System.out.println();
        }

        public static void display(int[] nums)
        {
            for(int i = 0; i < nums.length -1; i++)
                System.out.print(nums[i] + ", ");

            System.out.print(nums[nums.length - 1]); //!!!!!!this is where the error occurs
        }
        public static int[] onlyEvens(int[] nums) //only even numbers in array (anything divisible by 2 including 0)
        {
            int x = 0;  //for set length
            for(int i = 0; i < nums.length; i++)
                if (nums[i]%2 == 0) //checks if even
                    x++;

            int[] y = new int[x];
            int z = 0;

            for(int i = 0; i < nums.length; i++)
                if (nums[i]%2 == 0) //checks if even
                {
                    y[z] = nums[i];
                    z++;
                }
            return y;
        }
        public static int[] onlyPositives(int[] nums) //looking for only positive integers in array
        {
            int x = 0;  //sets set length
            for(int i = 0; i < nums.length; i++)
                if (nums[i] > -1)   //checks if positive
                    x++;

            int[] y = new int[x];
            int z = 0;

            for(int i = 0; i < nums.length; i++)
                if (nums[i] > -1)   //checks if positive
                {
                    y[z] = nums[i];
                    z++;
                }

            return y;
        }

        public static int[] disjoint(int[] nums, int[] nums2)
        {

            int x = 0;

            for(int i = 0; i < nums.length; i++)
            {
                int j = nums[i];
                if(!contains(nums2 , j))    //checks if letter be there
                    x++;
            }

            for(int i = 0; i < nums2.length; i++)
            {
                int j = nums2[i];   //checks if letter be there
                if(!contains(nums , j))
                    x++;
            }

            int[] y = new int[x];
            int z = 0;

            for(int i = 0; i < nums.length; i++)
            {
                int j = nums[i];    //checks if letter be there
                if(!contains(nums2 , j))
                {
                    y[z] = nums[i];
                    z++;
                }
            }

            for(int i = 0; i < nums2.length; i++)
            {
                int j = nums2[i];   //checks if letter be there
                if(!contains(nums , j))
                {
                    y[z] = nums2[i];
                    z++;
                }
            }
            return y;
        }

        private static boolean contains(int[] nums, int j) {
            return false;
        }
    }

答案 1 :(得分:0)

当给定数组nums为空(即它包含零个元素)时,您的显示方法不处理这种情况。如果数组为空nums.length==0,那么nums[nums.length - 1]nums[-1],这会产生异常。

为什么要省略循环中的最后一个元素?你的循环正确处理空数组,所以让循环打印所有元素:

public static void display(int[] nums)
{
    for(int i = 0; i < nums.length; i++)
        System.out.print(nums[i] + ", ");
}