Java ArrayList Help: Troubleshooting Unexpected Type Error

  • Comp Sci
  • Thread starter csgirl504
  • Start date
  • Tags
    Java
In summary: So the first 2 times, the indexOf(user) call returns -1, while the 3rd time, it returns 0, and so the addUser method returns false.
  • #1
csgirl504
18
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
  • #2
A better question would be, what is the correct way to search an ArrayList for a specific object?
 
  • #3
You are using the assignment operator, not the equality operator (==). Try this:
Code:
if (userList.indexOf(user) == -1)
 
  • #4
Hi csgirl! :smile:

Did you know that in java '==' is used to compare for equality?
The '=' is used for assignment to a variable.
 
  • #5
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 [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
 
  • #6
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.
 

Related to Java ArrayList Help: Troubleshooting Unexpected Type Error

What is an ArrayList in Java?

An ArrayList is a data structure in Java that allows you to store and manipulate a collection of objects. It is similar to an array, but has additional features such as dynamic resizing and built-in methods for adding, removing, and accessing elements.

Why am I getting an "unexpected type" error when using an ArrayList in Java?

This error occurs when you try to add an element of a different data type than the one declared for the ArrayList. For example, if you have declared an ArrayList of integers and try to add a string, you will get this error. Make sure you are adding elements of the correct data type to your ArrayList.

How can I troubleshoot an "unexpected type" error in my Java ArrayList?

One way to troubleshoot this error is to check the data types of the elements you are trying to add. Make sure they match the data type declared for the ArrayList. You can also try using a debugger or printing out the values of your elements to see where the error is occurring.

Can I have an ArrayList with mixed data types in Java?

Yes, you can have an ArrayList with mixed data types in Java by using the generic type Object. However, it is not recommended as it can make your code more difficult to read and maintain. It is better to have separate ArrayLists for each data type or to use a different data structure such as a HashMap.

How can I avoid "unexpected type" errors when using an ArrayList in Java?

To avoid this error, make sure you are adding elements of the correct data type to your ArrayList. You can also use generics to specify the data type of your ArrayList when declaring it, which will help catch any errors at compile time. Additionally, it is good practice to use descriptive variable names and to check for any potential type mismatches in your code.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
4
Views
855
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top