繁体   English   中英

数组索引超出范围异常

[英]Array Index Out Of Bounds Exception

问题:

放学期结束后更新学生成绩列表的程序。 该程序应从辍学的学生人数及其索引中读取数据,然后该程序应将其余学生的成绩复制到新数组中。 此外,该程序应同时显示原始列表和更新列表。 [提示:新数组的长度必须等于剩余学生的数量]

我的代码:

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行中说:

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

我该如何纠正

那是因为list2的大小。

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

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

如果不需要保留原始大小,则需要按照@Joetjah的建议从原始大小中减去num。

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

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

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

伪代码:

您从10名学生开始。 假设您要删除2个学生。 这意味着您将list2大小设置为2。

接下来,您尝试将所有学生(10)从listlist2 但是, list2太小了。 这样就得到了例外。

您想要的是:

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

使用预定义的数组“列表”来猜测该问题。

步骤1:使用初始化方法之一创建列表。 在这里大小是固定的。

步骤2:接受用户输入:假设它是11。

列表不需要包含输入的大小。

您使用错误的大小初始化了list2

您从被放弃的学生的用户数中获取,然后尝试将所有剩余的学生放入list2

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

工作正常

导入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 

这将删除您遇到的异常。 从下次开始,您就不再使用静态值。

编辑:最好不要自己将值分配给任何变量。 由于数组的大小为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] + " ");
        }
    }
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM