Mister M
Mister M

Reputation: 3

indexOf in Java - ArrayList

import java.util.ArrayList;
import java.util.Scanner;

public class JavaApplication10Arraylistandobjects {

    static Scanner user_input = new Scanner(System.in);

    public static void main(String[] args) {
        test();
    }

    public static void test(){
        ArrayList<mainclass> me = new ArrayList <> ();
        mainclass ob;
        for (int i=0;i<2;i++){
            ob = new mainclass();
            System.out.println("name");
            ob.name = user_input.nextLine();
            System.out.println("sname");
            ob.sname = user_input.nextLine();
            me.add(ob);
        }
        System.out.println("Show List: " + me);
        System.out.println("Confirm if is true or false: " + me.get(1).toString().contains("max"));
        System.out.println("what is index of: " + me.get(1).toString().indexOf("max"));
    }
}

public class mainclass {

    public String getName() {
    return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    @Override
    public String toString() {
        return "mainclass{" + "name=" + name + ", sname=" + sname + '}';
    }

    String name;
    String sname;
}

My questions is how can I find correctly indexOf string. For example when I am checking if string "max" exist - it shows me "true" and when I am trying to find index of string "max" it shows me index 15 which is not correct.

P.S. I found an article with the same problem where it says that I have to override equals and hashcode - I've done it but anyway I got the same problem.

Please point me to the right direction. What I am doing wrong here, can someone explain me pls.

These are my inputs and output.

name max sname hitman

Show List:[mainclass{name=Jony, sname=Bravo}, mainclass{name=max, sname=hitman}]

Confirm if is true or false: true what is index of: 15

BUILD SUCCESSFUL (total time: 11 seconds)

Upvotes: 0

Views: 85

Answers (1)

D M
D M

Reputation: 1410

The line:

    System.out.println("what is index of: " + me.get(1).toString().indexOf("max"));

has a problem, in that you're getting the object in the me list at index 1, getting its toString(), and looking for "max" in there. What you actually want to do, as I understand it, is look through the me list and find the place in the list with "max" in it.

P.S. I found an article with the same problem where it says that I have to override equals and hashcode - I've done it but anyway I got the same problem.

If you did that, it would allow you to do something like this:

x = new mainclass();
x.setName("Max");
System.out.println("what is index of: " + me.indexOf(x));

However, there's still a potential problem. Unless you set your equals() and hashCode() to only look at the name and not also sname, then it's not going to find anything unless the sname also matches.

Upvotes: 1

Related Questions