Sunday, February 1, 2015

ArrayList indexOf




Hi i'm just studying over ArrayLists and i have encountered a problem, i'm trying to get the index of an element but it keeps returning -1. Here's the code






Java Code:






import java.util.ArrayList;


public class PhoneEntry {
String name;
String phone;

public PhoneEntry(String name, String phone) {
this.name = name;
this.phone = phone;
}
public static void main(String[] args){

ArrayList<PhoneEntry> theDirectory = new ArrayList<PhoneEntry>();
theDirectory.add(new PhoneEntry("Jane","12"));
System.out.println(theDirectory.size());
int index = theDirectory.indexOf(new PhoneEntry("Jane","12"));
System.out.println(index);
}


}




I know the list is not empty because it returns a size.




No comments:

Post a Comment