Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Finding index in arraylist always coming up at -1

Aaronbob49:

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 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.



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Finding index in arraylist always coming up at -1

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×