Aaronbob49
Aaronbob49

Reputation: 33

Finding index in arraylist always coming up at -1

Im trying to find the index of student object through their ID attribute through an arraylist. however it always comes up -1.

 public int findIndex(String id) {
    // boolean exists = studentArray.contains(id);
    int index = 0;
    for (int i = 0; i < studentArray.size(); i++) {
        if (studentArray.get(i).getId().equals(id)) {
            return index = studentArray.indexOf(i);
        }
    } return -1;
}

However in my demo

BodyBag bag = new BodyBag(3);
Student student = new Student("Joe","1234",3.5);
Student student2 = new Student("Jill", "5678",3.4);
Student student3 = new Student("Angelina","9101",4.0);
System.out.println(bag.studentArray.get(0).getId().contains("1234"));

actually comes out as true. but in the first class it comes out as false and returns -1. Thanks in advance.

Upvotes: 0

Views: 88

Answers (4)

D.B.
D.B.

Reputation: 4713

You said your goal is

find the index of student object through their ID attribute through an arraylist

I would recommend you simply implement the equals method within your Student class in such a way that it returns true if and only if two Student instances have the same id and then simply use the indexOf method provided by ArrayList

The equals method in Student might look something like this:

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Student other = (Student) obj;
    return id == null ? other.id == null : id.equals(other.id);
}

Upvotes: 2

AnandiG
AnandiG

Reputation: 11

In your findIndex() method, replace this line

          return index = studentArray.indexOf(i);

with

          return i ; 

Upvotes: 1

sdsc81
sdsc81

Reputation: 570

You're having a conceptual mistake. Actually:

studentArray.indexOf( elem );

returns the index of the element elem in the array, but elem should be of the class Student. Of course, as long as this class provides an 'equals' method. In your case, something like:

@Override
public boolean equals( Object obj ) {
    // first you should check if obj is student before casting it...
    Student aux = (Student) obj;
    return this.getId().equals( aux.getId() );
}

What you're doing in your code with the line:

return studentArray.indexOf( i );

is trying to find an integer (i) in an array of students (studentArray).

Other way to do it, as posted below, is to just return the position, wich is:

return i;

Cheers.

Upvotes: 0

Orel Eraki
Orel Eraki

Reputation: 12196

You should just return the index(i)

 return i;

Instead of after finding it equals, you again try to find the index of i which isn't correct, you should find the index of student by it's id, but you already done it and find it to be at index i.

Note: Your index variable is useless the moment you choose when finding the element not to break the loop, but to return the index.

Upvotes: 1

Related Questions