数组索引超出界限异常

时间:2013-11-13 09:38:59

标签: java arrays

问题:

  

一个程序,在下降期结束后更新学生成绩列表。该程序应该从丢弃的学生的用户数和他们的索引中读取,然后程序应该将剩余学生的成绩复制到新阵列。此外,程序应显示原始列表和更新列表。 [提示:新数组必须具有与剩余学生数量相当的长度]

我的代码:

public class Q5{
  static Scanner scan = new Scanner (System.in);
  public static void main (String args[]){

    double [] list={1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10};
    System.out.println("enter number of students who dropped:");
    int num=scan.nextInt();
    double [] list2 = new double [num];

    System.out.println("Enter index Of  the student who dropped ");
    for (int j=1 ; j<=num ; j++)
    {
      System.out.println("student" + j + ":");
      int index=scan.nextInt();
      list[index]=0;
    }

    int  j=0;
    for(int i=0; i<list.length ; i++)
    if (list[i]!=0)
    {
      list2[j]=list[i];
      j++;
    }
    System.out.print("The original list : " );
    for(int i=0; i<list.length ; i++)
    System.out.print(list[i] + " " );

    System.out.print("remaining students " );
    for(int i=0; i<list2.length ; i++)
    System.out.print(list2[i] + " " );
  }
}

有什么问题?它不工作!!! 第16行说:

  

线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:4       在Q5.main(Q5.java:23)

我如何纠正这个

7 个答案:

答案 0 :(得分:2)

这是因为list2

的大小

您不应将其大小设置为num,而应将其大小设置为原始列表的大小。因为list2将包含与原始list一样多的元素。您只需要num从用户那里获取那么多输入,并为这些索引分配值 0

double[] list2 = new double[list.length]; // This should be the size of your list2

如果您不需要保留原始尺寸,则需要按@Joetjah的建议从原始尺寸中减去数字。

double[] list2 = new double[list.length - num]; // This should be the size of your list2

答案 1 :(得分:0)

如果用户输入的值超出数组范围,您的代码将失败:

int index=scan.nextInt();
list[index]=0; // This may fail. Value entered by the user may exceed the array length

答案 2 :(得分:0)

的伪代码:

你从10名学生开始。假设你想放弃2名学生。这意味着您将list2大小设置为2。

您接下来要做的是尝试将所有学生(10)从list添加到list2。但是,list2对于这个来说太小了。因此,你得到了你的例外。

你想要的是这样的:

double [] list2 = new double [list.length - num];

答案 3 :(得分:0)

猜测预定义数组'list'的问题。

步骤1:使用其中一种初始化方式创建列表。这里的大小是固定的。

步骤2:从用户那里获取输入:假设它是11。

列表不需要包含作为输入提供的大小。

答案 4 :(得分:0)

您使用错误的尺寸初始化list2

您从已弃用学生的用户编号中获取,然后尝试将所有剩余学生list2放入其中。

double [] list2 = new double [list.length - num];

答案 5 :(得分:0)

工作正常

import java.util.Scanner;

public class Q5 {
    static Scanner scan = new Scanner(System.in);

    public static void main(String args[]) {

        double[] list = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        System.out.println("enter number of students who dropped:");
        int num = scan.nextInt();
        double[] list2 = new double[num - 1]; // should be num-1

        System.out.println("Enter index Of  the student who dropped ");
        for (int j = 0; j < num; j++) {
            System.out.println("student" + j + ":");
            int index = scan.nextInt();
            list[index] = 0;
        }

        int j = 0;
        for (int i = 0; i < num; i++) {
            if (list[i] != 0) {
                list2[j] = list[i];
                j++;
            }
        }
        System.out.print("The original list : ");
        for (int i = 0; i < list.length; i++) {
            System.out.print(list[i] + " ");
        }

        System.out.print("remaining students ");
        for (int i = 0; i < list2.length; i++) {
            System.out.print(list2[i] + " ");
        }
    }
}

输出

 enter number of students who dropped:
    2
    Enter index Of  the student who dropped 
    student0:
    1
    student1:
     2
    The original list : 1.0 0.0 0.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 remaining students 1.0 

答案 6 :(得分:-1)

这将删除您获得的异常。从下次您不使用静态值。

修改 自己为任何变量赋值都不是一个好习惯。由于数组的大小为10.如果用户输入超过10名学生,则会导致问题。

import java.util.*;

public class Q5 {
    static Scanner scan = new Scanner(System.in);

    public static void main(String args[]) {

        double[] list = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        System.out.println("enter number of students who dropped:");
        int num = scan.nextInt();
        double[] list2 = new double[list.length-num]; // should be num-1

        System.out.println("Enter index Of  the student who dropped ");
        for (int j = 0; j < num; j++) {
            System.out.println("student" + j + ":");
            int index = scan.nextInt();
            list[index] = 0;
        }

        int j = 0;
        for (int i = 0; i < list.length; i++) {
            System.err.println(""+list[i]);
            if (list[i] > 0) {

                list2[j] = list[i];
                j++;
            }
        }
        System.out.print("The original list : ");
        for (int i = 0; i < list.length; i++) {
            System.out.print(list[i] + " ");
        }

        System.out.print("remaining students ");
        for (int i = 0; i < list2.length; i++) {
            System.out.print(list2[i] + " ");
        }
    }
}