ArrayList.indexOf on a Class
Author |
Message |
Zren
|
Posted: Thu Sep 09, 2010 7:49 pm Post subject: ArrayList.indexOf on a Class |
|
|
So I have a custom class, we're gonna call is Dog. This class is inside an ArrayList<Dog>. I want to find the indexOf("Joey") inside this list of Dogs. User has a String variable called name. How do I make it so this will return the index of the instance with the name Joey?
I've looked here to look for overriding equals and toString, but I still get -1 outOfBounds error when I search for it.
http://www.javapractices.com/topic/TopicAction.do?Id=17
Okay I lied, class is named Player, but same scenario k?
Java: |
int onlineIndex = connectedPlayers. indexOf(name );
private ArrayList<Player> connectedPlayers;
@Override public String toString () {
return username;
}
@Override public boolean equals (Object aThat ) {
if (this == aThat ) return true;
if (! (aThat instanceof Player )) {
if (aThat instanceof String) {
String that = (String)aThat;
if (that. equalsIgnoreCase(username ))
return true;
else
return false;
} else {
return false;
}
}
Player that = (Player )aThat;
return username. equalsIgnoreCase(that. username);
}
|
Updated: Added missing code to the equals() function, still not working. The entire function isn't even called, so I'm guessing this is to do with the String ignoring all non String, or String Inherited classes? For now I've made a temp Player with the same username and that seems to work. But the question still stands as to how to it work when comparing a string. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
TheGuardian001
|
Posted: Thu Sep 09, 2010 9:29 pm Post subject: Re: ArrayList.indexOf on a Class |
|
|
code: |
int onlineIndex = connectedPlayers.indexOf(name);
private ArrayList<Player> connectedPlayers;
|
Notice anything wrong with the ordering there?
you're reading connectedPlayers before creating it. |
|
|
|
|
|
Zren
|
Posted: Thu Sep 09, 2010 10:59 pm Post subject: Re: ArrayList.indexOf on a Class |
|
|
TheGuardian001 @ Thu Sep 09, 2010 9:29 pm wrote: code: |
int onlineIndex = connectedPlayers.indexOf(name);
private ArrayList<Player> connectedPlayers;
|
Notice anything wrong with the ordering there?
you're reading connectedPlayers before creating it.
That's not the ordering in the actual code, otherwise I'd never even get to the out of bounds error dude. I'd be getting a cannot find symbol error. Granted, I didn't state the actual question in the first paragraph so you probably scanned it and looked directly at the code [I do it too ^_^].
This is inside another class, where connectedPlayers is a private property and onlineIndex is declared inside one of it's functions (and yes, connectedPlayers is initialized when in the other class' constructor).
Ah crap, my metaphor is broken and used User instead of Dog. Oh well. And typo: "how to it work" -> "how to work it" from the update paragraph. |
|
|
|
|
|
TheGuardian001
|
Posted: Fri Sep 10, 2010 2:23 am Post subject: Re: ArrayList.indexOf on a Class |
|
|
In that case, I don't think this section of code is the problem, as nothing else in that code uses arrays or ArrayLists. |
|
|
|
|
|
Zren
|
Posted: Fri Sep 10, 2010 6:22 am Post subject: Re: ArrayList.indexOf on a Class |
|
|
There should be no OutOfBounds error. The string IS in the list under the Player instance, except that when I look for a String in the ArrayList, it ignores all Player instances (I've checked with a simple System.out.print in the overwriten equals() function) therefor it's in this part of the code. I want it to not pass over Player instances and instead look at the Player.username variable. (Or it might be it doesn't even look since the ArrayList is of the type <Player>.) Either way I'd like to know the how and why ArrayList<Player>.indexOf(String) always ends up -1 (the OutOfBounds error is there because the function isn't complete, however logically, it ought naught to be appearing).
As of right now, ALL the players name's I send into this function, are in connectedPlayers as a Player object. So theoretically, it shouldn't end up in the unfinished else section so long as it compares name to each Player, because when a Player is compared to a String instance, it should compare it to Player.username.
It works when I pass through it's own class instance (Player) when I make a temp Player like so:
Java: |
public Player getPlayer (String name ) {
Player temp = new Player (name );
int onlineIndex = connectedPlayers. indexOf(temp );
if (onlineIndex >= 0) {
return connectedPlayers. get(onlineIndex );
} else {
//When IO. Check if he's loaded, if not: load, add, save.
return connectedPlayers. get(onlineIndex );
}
}
|
However, I'm wondering why this doesn't work:
Java: |
public Player getPlayer (String name ) {
int onlineIndex = connectedPlayers. indexOf(name );
if (onlineIndex >= 0) {
return connectedPlayers. get(onlineIndex );
} else {
//When IO. Check if he's loaded, if not: load, add, save.
return connectedPlayers. get(onlineIndex );
}
}
|
|
|
|
|
|
|
TheGuardian001
|
Posted: Fri Sep 10, 2010 3:44 pm Post subject: Re: ArrayList.indexOf on a Class |
|
|
Zren @ Fri Sep 10, 2010 6:22 am wrote:
It works when I pass through it's own class instance (Player) when I make a temp Player like so:
Java: |
public Player getPlayer (String name ) {
Player temp = new Player (name );
int onlineIndex = connectedPlayers. indexOf(temp );
if (onlineIndex >= 0) {
return connectedPlayers. get(onlineIndex );
} else {
//When IO. Check if he's loaded, if not: load, add, save.
return connectedPlayers. get(onlineIndex );
}
}
|
However, I'm wondering why this doesn't work:
Java: |
public Player getPlayer (String name ) {
int onlineIndex = connectedPlayers. indexOf(name );
if (onlineIndex >= 0) {
return connectedPlayers. get(onlineIndex );
} else {
//When IO. Check if he's loaded, if not: load, add, save.
return connectedPlayers. get(onlineIndex );
}
}
|
First Case:
ArrayList<Player>.indexOf(Player temp);
Second case:
ArrayList<Player>.indexOf(String name);
In the first case, you are looking for the index of a Player object with the field username = "name", in the second case, you are looking for the index of a String object with the value "name." It's possible that the indexOf() method doesn't use equals(), meaning your override is not affecting the search. |
|
|
|
|
|
Zren
|
Posted: Fri Sep 10, 2010 8:21 pm Post subject: Re: ArrayList.indexOf on a Class |
|
|
Ah. Figured it out. First off though Guardian, equals is the one of the primary functions in searching lists. How on earth would you look for something if you couldn't compare it to anything (no Zen talk here bud).
Anyways, on to why it wasn't working. Pull from the JavaDocs:
Quote: int indexOf(Object o)
Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.
The main part we gotta look at is:
o==null ? get(i)==null : o.equals(get(i))
The beginning is basically to ignore if either the object in comparison, or the search object is null. The next part is what were looking for to solve this riddle.
When I called indexOf(Player), Player would be o, and each object in ArrayList<Players> connectedPlayers would be get(i). So We'd be calling the equals() function of the object we plunk into indexOf. So when, indexOf(String) is used, we're acutally looking at the equals() function as declared by strings!
Man I'm happy I got to the end of that. It was bugging the hell out of me this morning. Time for some coffee. |
|
|
|
|
|
|
|