DewSee
DewSee

Reputation: 97

Use indexOf with an ArrayList that has multiple objects

In the code below I would like to use indexOf(), but I can't find the proper way.

Movie class:

public class Movie {


    private String title;
    private String genre;
    private String actor;
    private int yearPublished;

    public Movie(String title, String genre, String actor, int yearPublished) {
        this.title = title;
        this.genre = genre;
        this.actor = actor;
        this.yearPublished = yearPublished;
    }

    @Override
    public String toString() {
        return  title + ", Genre: " + genre + ", Actor(s):" + actor + ", Year of publishing: " + yearPublished;
    }



    public String getTitle() {
        return title;
    }

    public String getGenre() {
        return genre;
    }

    public String getActor() {
        return actor;
    }

    public int getYearPublished() {
        return yearPublished;
    }


}

Controller class:

public class Controller {

    void start() {
        scanning();
        printing("Movies:");
        selectYourMovie();

    }

    private List<Movie> movies = new ArrayList<>();

    private void scanning() {
        try {
            Scanner fileScanner = new Scanner(new File("movies.txt"));

            String row;
            String []data;

            while (fileScanner.hasNextLine()) {     
                row = fileScanner.nextLine();
                data = row.split(";");
                movies.add(new Movie(data[0], data[1], data[2], Integer.parseInt(data[3])));

            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void printing(String cim) {
        for (Movie movie : movies) {
            System.out.println(movie);
        }
    }

    private void selectYourMovie() {

        System.out.println(movies.indexOf(/*What to put here?*/);
    }

}

And the content of the txt file:

The Matrix;action;Keanu Reeves;1999

The Lord of the Rings;fantasy;Elijah Wood;2001

Harry Potter;fantasy;Daniel Radcliffe;2001

The Notebook;drama;Ryan Gosling;2004

Step Up;drama, dance;Channing Tatum;2006

Pulp Fiction;crime;Samuel L. Jackson, John Travolta;1994

Star Wars: A New Hope;action;Mark Hamill;1977

So I would like to return the index of the given movie, but I couldn't find how to do it with a list which has multiple objects. Since passing an entire line of the txt doesn't work.

Any hints on this?

Upvotes: 0

Views: 2317

Answers (4)

Tanmay Baid
Tanmay Baid

Reputation: 469

Because you are using a custom object [i.e. Movies] you might want to override its equals() and hashCode() methods so the indexOf() method can identify if given Movies object is same as any existing Movies object available in List.

You can read this post to know more about: Java: howto write equals() shorter

I would recommend using EqualsBuilder available in Apache's library commons-lang3

Upvotes: 1

Adrian K.
Adrian K.

Reputation: 118

I believe you are using the List#indexOf(Object) method wrong.

The List#indexOf(Object)

returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

As you see, the method takes one parameter, an Object which has to be the same type as the elements in your list. If you store objects of the type Movie then you have to use List#indexOf(Movie) as it will always return -1 if you use a different type of object.

You also need to make sure that you don't add an object to the list, then create it again and then use List#indexOf(Object).

If you want to look for the index of a Movie by it's name, just use a for()-loop to iterate through all objects in the list and check if the movie's name equals (ignoring case-sensitivity) your given name. Then just return it and voilà, you made it.

//EDIT: You could of course alternatively override the equals() method in your Movie class.

Upvotes: 0

ArraysList uses equals in the implementation of the method indexOf

look:

 public int indexOf(Object o) {
        if (o == null) {
            for (int i = 0; i < size; i++)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = 0; i < size; i++)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }

so your movie class is not completely developed to be able to work with that... :)

override properly equals (and hashCode) so the code can work!

Upvotes: 3

Incepter
Incepter

Reputation: 2958

You can of course use the List#indexOf(T) method. To do it in a proper way, re-implement the Object#equals method in your Movie Object.

Upvotes: 2

Related Questions