Error java.lang.IndexOutOfBoundsException: Index 333 out of bounds for length1 when manipulating and browsing ArrayList

Asked 1 years ago, Updated 1 years ago, 394 views

Based on the requirements on the page below, we have created a membership registration function in Java.

A collection of programming selection tasks to make and memorize (Java Basic Edition) - Level 4 membership registration function

When I finished writing the code and moved it, an error occurred and the data was not output well.

java.lang.IndexOutOfBoundsException—Index 333 out of bounds for length1

Enter a description of the image here

I don't think there is any problem with the code, but I don't know how to fix it.
I apologize for the inconvenience, but please let me know.

2/24 Additional
After modifying the Register.java code,
This data was displayed.
Enter a description of the image here

Source Code

Register.java

package lenyu;

import java.util.Date;
import java.util.*;

public class Register {
    static ArrayList<Kaiin>kaiinAll = new ArrayList<Kaiin>();

    public static void main(String[]args) {
        Register = new Register();
        Scanner scan = new Scanner (System.in);
        System.out.println("How many people would you like to register?");
        inti=scan.nextInt();
        int id = 0;
        String name = null;
        for(int j=0;j<i;j++){
            System.out.println("Please enter your membership ID"");
            id=scan.nextInt();
            System.out.println("Please enter a name"");
            name = scan.next();
            kaiinAll.add(r.kaiinAdd(id,name)));
            Kaiin lastElement=kaiinAll.get(kaiinAll.size()-1);
            System.out.println(lastElement);
            
        }

    }

    private Kaiin kaiinAdd(intid, String name) {
        Kaiink = new Kaiin();
        k.setId(id);
        k.setName(name);
        Date = new Date();
        k.setAddDate(d);
        returnk;

    }

}

Kaiin.java

package lenyu;
import java.util.Date;
public class Kaiin{
    intid;
    String name;
    Date addDate;
    public int getId(){
        return id;
    }
    public void setId(intid){
        this.id=id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name){
        this.name = name;
    }
    publicDategetAddDate(){
        return addDate;
    }
    public void setAddDate(DateAddDate){
        This.addDate=addDate;
    }
}

java

2022-09-30 22:01

2 Answers

This is because kaiinAll.get(id) uses id as the key to retrieve elements from a variable-length array.This is not a HashMap or associative array, so it is an array in the order of insertion.(For example, the index of the item you want to retrieve when the screenshot exception is 0)

For example, if you want to retrieve an element with a specific ID from ArrayList<Kaiin>, you can use the for-each loop.


2022-09-30 22:01

ArrayList#add() is the method to add the specified element to the end of the list

Therefore, to retrieve the previously added element, you can simply pass the ArrayList#get() argument with the index indicating the last element in the list.

Since the index is 0 beginning, the index that indicates the last element of the list is -1 in the

To sum up,

Kaiin lastElement=kaiinAll.get(kaiinAll.size()-1);

You can retrieve the member objects that you just registered in .

Implement Questionnaire

kaiinAll.get(id)

The argument is a value that is independent of the index.

(Added after receiving comments)

I fixed it, but it didn't show well.

Passing an object to the System.out.println() argument prints a string generated by the toString() method. The Kaiin class does not implement the toString() method.

getClass().getName()+'@'+Integer.toHexString(hashCode())

is used.
This is probably not what you expect.

The solution is to override toString() in the Kaiin class for proper implementation.

public class Kaiin{
    // ...

    @ Override
    public String to String() {
        // Assemble the string to output here
        return "TODO";
    }
}

(However, it does not seem that toString() should be used for this challenge.)

In addition, the issues to be linked to are

Please summarize the output at the end of the program.

Therefore, in the end, you will not think about the index of the previously registered element... and will output all the elements on the list in a loop at once.


2022-09-30 22:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.