1

I am not getting the right answer when I try to use indexOf() of an ArrayList made up of user defined objects. Here is the code that creates one of the objects:

    State kansas = new State("KS", 5570.81, 2000)

So, the name of the object is "kansas"

Here is the code that creates the ArrayList and adds the object:

    ArrayList<State> allStates = new ArrayList<State>();    
    allStates.add(kansas);

And here is the code that I try to use to find the index of this object:

    System.out.println(allStates.indexOf(kansas));

This is the point at which my compiler (Eclipse) throws me a red X indicating that there is a problem with my code and the problem is that it does not recognize 'kansas'. So I tried this:

    String s = "kansas";
    System.out.println(allStates.indexOf(s));

and it will run but the result is -1.

I am calling a method from a different class to create the ArrayList as opposed to creating it in the same class as my main method but I'm new enough to coding that I"m not sure if that is where I am going wrong. However, in order for the program that I am writing to work, I need to have data about each of the State objects stored so that I can access it from the main method.

Any advice?

*This is my first time posting a questions and I wasn't sure how much detail to go into so if I'm missing relevant information please let me know :)

1
  • 1
    String is not a State, you will have to write a some kind of filtering method which can iterate over the List and compare the States name with the String Commented Feb 9, 2017 at 3:38

3 Answers 3

5

method indexOf uses equlas() method to compare objects. That why you have to override equals method in your custom class (if you planning use class in Map override hashCode method as well). most IDE can generate these methods (equals and hashCode).
here simple example.

public class State {

    private String stateCode;

    public State(String stateCode /* other parameters*/) {
        this.stateCode = stateCode;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;

        if (o == null || getClass() != o.getClass()) return false;

        State state = (State) o;

        return stateCode.equals(state.stateCode);
    }

    @Override
    public int hashCode() {
        return stateCode.hashCode();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

if (o == null || getClass() != o.getClass()) return false; This bit means that a String will never be equal to the object, hence defeating the purpose of your solution (if I understand correctly, you're proposing that indexOf gives a match for an object with the corresponding statecode).
I misunderstood the question. if goal find indexOf state in list class must override equals() method. if goal find object State by state'code maybe better use Map where key is state'code and value is state object
1

This is because, String is not your custom object State type. Your array list is a list of all 'State' types, which is why this -

String s = "kansas";
System.out.println(allStates.indexOf(s));

won't work.

What you can do is have a convenience method that iterates through the list and returns the index.

private int getIndexOfState(String stateName) {
  for(State stateObject : allStates)  {
     if(stateObject.getName().equals(stateName))
         return allStates.indexOf(stateObject);
   }
 return -1; 
 }

Now you can reuse this method to find index of any state name you pass, and whenever the method returns -1, it means the stateName(state) was not found in the list of states.You can pass in 'Kansas' or 'California' or anything as the parameter to the method.

In your method call you say

  System.out.println(getIndexOfState("Kansas"));      
  System.out.println(getIndexOfState("Chicago"));

Comments

0

The return value is -1 because there is no String "kansas" in allStates, and ArrayList#indexOf returns -1 if the element is not present in the list. If you try to add s to allStates, the compiler won't even let you, because State is not a String.

I don't know why you instantiated a String with the value "kansas", but if you need to refer to the State from its name (maybe the name comes from a Scanner input), you will need a Map<String, State>, such as:

Map<String, State> map = new HashMap<>();
map.put("kansas", kansas) // a String and the object named kansas

Then, you can do:

System.out.println(allStates.indexOf(map.get("kansas")))
//or    
String s = "kansas";
System.out.println(allStates.indexOf(map.get(s)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.