1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

JAVA ArrayList help

  1. Oct 9, 2012 #1
    1. The problem statement, all variables and given/known data

    I'm working on a program that uses an ArrayList to hold a collection of UserID objects (which contains the first and last names, plus username and password).

    When I add a new UserID to the list, I want to check to make sure that it is not already there.

    This is the error I am getting, and my code follows.

    UserList.java:36: error: unexpected type
    if (userList.indexOf(user) = -1)
    ^
    required: variable
    found: value


    2. Relevant equations

    Code (Text):
    import java.util.ArrayList;

    public class UserList
    {  
        private ArrayList<UserID> userList;


        /**
         * Constructor to create an ArrayList to store a collection of UserID objects
         */
        public UserList ()
        {
            userList = new ArrayList<UserID>();
        }
       
       
        /**
         * Method to add a new UserID to the collection.
         * Returns true if a user with the same ID is not already in the collection.
         * Returns false if the user already exists.
         *
         * @param user the UserID object to be added
         * @return true or false
         */
        public boolean addUser(UserID user)
        {
            if (userList.indexOf(user) = -1)
                return false;
            else
            {
                userList.add(user);
                return true;
            }
        }
    }
     

    3. The attempt at a solution

    I am using the "userList.indexOf(user) = -1 " to check to see if the object is already there. Why is it telling me that user is an unexpected type when my ArrayList is a collection of UserID objects, and user is a UserID object? I'm just learning ArrayLists and am very confused!
     
  2. jcsd
  3. Oct 9, 2012 #2
    A better question would be, what is the correct way to search an ArrayList for a specific object?
     
  4. Oct 9, 2012 #3

    Mark44

    Staff: Mentor

    You are using the assignment operator, not the equality operator (==). Try this:
    Code (Text):
    if (userList.indexOf(user) == -1)
     
  5. Oct 9, 2012 #4

    I like Serena

    User Avatar
    Homework Helper

    Hi csgirl! :smile:

    Did you know that in java '==' is used to compare for equality?
    The '=' is used for assignment to a variable.
     
  6. Oct 9, 2012 #5

    gabbagabbahey

    User Avatar
    Homework Helper
    Gold Member

    Using ArrayList.indexOf() will work, and is totally fine for small lists, especially if you are only going to be using this method a few times. However, it searches the array linearly and so it is [itex]\mathcal{O}(n)[/itex], so if you have a large list that you are going to be searching repeatedly, and performance is important (as it often is), then there are at least 2 better alternatives:

    (1) Implement the Comparable interface in UserID and use the compareTo method to compare 2 UserId objects. You can then sort the list using Collections.sort and perform a binary search for each object using Collections.binarySearch.

    (2) Use a HashSet of the objects in your ArrayList
     
  7. Oct 10, 2012 #6

    gabbagabbahey

    User Avatar
    Homework Helper
    Gold Member

    You should also note two important things about ArrayList.indexOf(Object obj):

    (1) It will return -1 if the object reference is not found, so your if statement is backwards.

    (2) It will, by default, look for an object reference in the ArrayList, which is not the same as looking for an identical object (one of the same Class, containing the same values for all attributes). Consider, for example what happens if you try to use the same object reference to add several different objects to an ArrayList using a fixed version of your addUser method:

    Code (Text):
    public boolean addUser(UserID user) {
        if (userList.indexOf(user) != -1) {
            return false;
        } else {
                userList.add(user);
            return true;
        }
    }
    Code (Text):
    public class TestUsers {
            public static void main(String[] args) {
            UserList uList = new UserList();
            UserID user = new UserID("Steve", "Jacobson", "SJacobson", "password1");
                    System.out.println(uList.addUser(user));
                    user = new UserID("Carl", "Carlson", "CCarlson", "password1234");
                    System.out.println(uList.addUser(user));
                    UserID user2 = new UserID("Steve", "Jacobson", "SJacobson", "password1");
            System.out.println(uList.addUser(user2));
            }
    }
    Here, Steve Jacobson would get added to the list twice, while Carl Carlson would not be added at all. The reason is that the reference variable user used for the first 2 calls to the addUser method is the same, even though it refers to a different user in each case, while for the 3rd call to the addUser method, a different reference variable user2 is used.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: JAVA ArrayList help
  1. Java Help (Replies: 6)

Loading...