Comp Sci Java ArrayList Help: Troubleshooting Unexpected Type Error

  • Thread starter Thread starter csgirl504
  • Start date Start date
  • Tags Tags
    Java
Click For Summary
The discussion centers on troubleshooting a type error in Java related to using an ArrayList to store UserID objects. The error arises from using the assignment operator '=' instead of the equality operator '==' in the condition to check if a UserID already exists in the list. The correct condition should be "if (userList.indexOf(user) == -1)". Additionally, it's noted that using ArrayList.indexOf() for searching is linear and may not be efficient for large lists, suggesting alternatives like implementing the Comparable interface for binary search or using a HashSet for better performance. Understanding these distinctions is crucial for effective object management in Java collections.
csgirl504
Messages
18
Reaction score
0

Homework Statement



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

Homework Equations



Code:
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;
		}
	} 
}

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!
 
Physics news on Phys.org
A better question would be, what is the correct way to search an ArrayList for a specific object?
 
You are using the assignment operator, not the equality operator (==). Try this:
Code:
if (userList.indexOf(user) == -1)
 
Hi csgirl! :smile:

Did you know that in java '==' is used to compare for equality?
The '=' is used for assignment to a variable.
 
csgirl504 said:
A better question would be, what is the correct way to search an ArrayList for a specific object?

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 \mathcal{O}(n), 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
 
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:
public boolean addUser(UserID user) {
	if (userList.indexOf(user) != -1) {
		return false;
	} else {
	        userList.add(user);
		return true;
	}
}

Code:
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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K