简体   繁体   English

我在 java 中面临尴尬的 ArrayList 索引越界异常

[英]I am facing an awkward ArrayList index out bounds exception in java

I am attempting to code a library management program in Java. I am facing a problem with ArrayList here.我正在尝试在 Java 中编写图书馆管理程序。我在这里遇到 ArrayList 的问题。 It seems like there is a problem with the integer i that I want to use as a variable index, but I don't understand the problem.我想用作变量索引的 integer i似乎有问题,但我不明白这个问题。 I have provided the code below.我提供了下面的代码。

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Scanner;

// Create a library management system which is capable of issuing books to the students.
// Book should have info like:
// 1. Book name
// 2. Book Author
// 3. Issued to
// 4. Issued on
// User should be able to add books, return issued books, issue books.
// Assume that all the users are registered with their names in the central database.

class library{
    Scanner sc = new Scanner(System.in);

    ArrayList<String> name = new ArrayList<>();
    ArrayList<Long> phone_no = new ArrayList<>();
    
    int i = 0;
    public void registration(){
        System.out.print("Enter your name: ");
        this.name.add(this.i,sc.next());
        
        System.out.print("Enter your phone no.: ");
        this.phone_no.add(this.i,sc.nextLong());
        
        this.i++;
    }

    

    public void registrationdetails(){
        System.out.printf("Your name: %s\nYour phone no.: %ld",this.name.get(this.i),this.phone_no.get(this.i));
    }

    // public void issuebook() {
    //     System.out.println("Enter the name of the book: ");
    //     String bookname = sc.next();
    //     System.out.println("Enter the name of the book: ");
    //     String bookauthor = sc.next();
    //     System.out.println("Enter the name of the book: ");
    //     String issuedto = sc.next();
    //     System.out.println("Enter the name of the book: ");
    //     LocalDate issuedon = LocalDate.parse(sc.next());
    // }


    
}

public class Library_management {
    public static void main(String[] args) {
        
        library Anish = new library();
        Anish.registration();
        Anish.registrationdetails();

    }
}

The output is: output 是:

Enter your name: ijfwoa
Enter your phone no.: 456
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1
        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:266)
        at java.base/java.util.Objects.checkIndex(Objects.java:359)
        at java.base/java.util.ArrayList.get(ArrayList.java:427)
        at library.registrationdetails(Library_management.java:34)
        at Library_management.main(Library_management.java:57)

I can't understand what the problem is.我不明白问题出在哪里。 Please help me.请帮我。

The issue is in your indexing in library.registrationdetails()问题出在您在 library.registrationdetails() 中的索引中

System.out.printf("Your name: %s\nYour phone no.: %ld",this.name.get(this.i),this.phone_no.get(this.i));

when you are doing this.name.get(this.i) this.i equals to 1. Arrays in Java start at 0, so the index 1 actually refers to the second * element.当你做this.name.get(this.i) this.i等于 1. Java 中的 Arrays 从 0 开始,所以索引 1 实际上指的是第二个* 元素。 At this point in time you only have 1 element in your list so the program fails - you are trying to get the 2nd element in a list with 1 element!此时您的列表中只有 1 个元素,因此程序失败 - 您正在尝试获取具有 1 个元素的列表中的第二个元素!

To fix this simply subtract one from the index: (ie replace the above line with this one in your code)要解决这个问题,只需从索引中减去一个:(即用代码中的这一行替换上面的行)

System.out.printf("Your name: %s\nYour phone no.: %ld",this.name.get(this.i-1),this.phone_no.get(this.i-1));

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

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