简体   繁体   中英

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!

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM