我在 java 中遇到一个尴尬的 ArrayList 索引越界异常
I am facing an awkward ArrayList index out bounds exception in java
我正在尝试在 Java 中编写图书馆管理程序。我在这里遇到 ArrayList 的问题。我想用作变量索引的整数 i
似乎有问题,但我不明白这个问题。我提供了以下代码。
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();
}
}
输出为:
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)
我不明白问题出在哪里。请帮助我。
问题出在您在 library.registrationdetails()
中的索引中
System.out.printf("Your name: %s\nYour phone no.: %ld",this.name.get(this.i),this.phone_no.get(this.i));
当你做this.name.get(this.i)
时this.i
等于1。Java中的数组从0开始,所以索引1实际上指的是秒* 元素。此时您的列表中只有 1 个元素,因此程序失败 - 您正在尝试获取具有 1 个元素的列表中的第二个元素!
要解决此问题,只需从索引中减去一个即可:(即用代码中的这一行替换上面的行)
System.out.printf("Your name: %s\nYour phone no.: %ld",this.name.get(this.i-1),this.phone_no.get(this.i-1));
我正在尝试在 Java 中编写图书馆管理程序。我在这里遇到 ArrayList 的问题。我想用作变量索引的整数 i
似乎有问题,但我不明白这个问题。我提供了以下代码。
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();
}
}
输出为:
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)
我不明白问题出在哪里。请帮助我。
问题出在您在 library.registrationdetails()
中的索引中System.out.printf("Your name: %s\nYour phone no.: %ld",this.name.get(this.i),this.phone_no.get(this.i));
当你做this.name.get(this.i)
时this.i
等于1。Java中的数组从0开始,所以索引1实际上指的是秒* 元素。此时您的列表中只有 1 个元素,因此程序失败 - 您正在尝试获取具有 1 个元素的列表中的第二个元素!
要解决此问题,只需从索引中减去一个即可:(即用代码中的这一行替换上面的行)
System.out.printf("Your name: %s\nYour phone no.: %ld",this.name.get(this.i-1),this.phone_no.get(this.i-1));