Andy Dudley
Andy Dudley

Reputation: 329

Passing an int to an Object argument

A couple of my classmates are having a problem that I'm not able to reproduce, so I'm hoping someone can shed some light on their issue.

Our assignment required us to make a method using this signature:

public void addElement(int index, Object element) {...}

This method is called by a provided (pre-written) driver class with this line:

list1.addElement(addIndex, int1);

As you may have guessed, int1 is an int. This works fine for me, but some people are receiving an error message:

"The method addElement(int, Object) in the type LinkedList is not applicable for the  arguments (int, int) "
on list1.addElement(addIndex,int1);

I've tried using different IDEs and different versions of JDK, but still can't reproduce the issue.

Thanks for the help!

Upvotes: 2

Views: 670

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726499

This issue happens for people who try this out on JDK prior to Java-5, which is when autoboxing has been introduced. If you switch to a JDK that old, you will see this error. Of course this is when variable argument lists were added, too, so you would see other errors as well.

A way that works in all JDKs is as follows:

list1.addElement(addIndex, Integer.valueOf(int1));

Upvotes: 2

Related Questions