Why am I getting a compile time error for the two method headers at the end of my code in my class ArrayListTest?

ArrayListTest: http://pastebin.com/dUHn9vPr

Student: http://pastebin.com/3Vz1Aytr

I have a compiler error at the two lines:

delete(CS242, s3)

replace(CS242, s, s4);

When I try to run the code it states:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    The method replace(ArrayList<Student>, Student, Student)in the type ArrayListTest is not applicable for the arguments (List<Student>, Student, Student)
    The method delete(ArrayList<Student>, Student) in the type ArrayListTest is not applicable for the arguments (List<Student>, Student)
        at ArrayListTest.main(ArrayListTest.java:54)

I fixed the compile time errors since I use Eclipse and Eclipse gives options of code you can use to fix the compile time error. I chose to "Change method to 'replace(ArrayList, Student, Student)' to 'replace(List, Student, Student)'

Although it fixed that compile time error, I don't understand why I was getting a compile time error to begin with and why that effectively fixed the error

I don't really know what missing code I need to write to correct these two methods below:

public static void replace(List<Student> cS242, Student oldItem,
                Student newItem) {

public static void delete(List<Student> cS242, Student target){
share|improve this question
The errors are pretty self explanatory the types in the method declaration don't match what you're calling the method with. – Borgleader Sep 22 at 0:21
read inheritance in generics. – Nambari Sep 22 at 0:24
3  
You should include an excerpt of the ArrayListTest class in the question. As pastebin content is external and if missing might render question ininteligible. Actually it expires in 23 hours right now. – Javier Sep 22 at 0:33
feedback

closed as not a real question by Mark Elliot, bažmegakapa, NSPostWhenIdle, Mechanical snail, pmr Sep 22 at 12:37

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

4 Answers

The method public static void replace(ArrayList<Student> aList, Student oldItem, Student newItem) will not allow a List<Student> list type to be passed in.

In other words, the first argument is required to be an ArrayList (or subclass). When you changed the signature to public static void replace(List<Student> aList, Student oldItem, Student newItem), the argument CS242 of type List<Student> matched the expected argument type.

share|improve this answer
feedback

For your two missing methods, the methods List.indexOf() and List.remove() will be invaluable here.

share|improve this answer
feedback

That's because you were declaring your ArrayList<Student> as:

List<Student> CS242 = new ArrayList<Student>(25);

And your replace method as:

public static void replace(ArrayList<Student> aList, Student oldItem, Student newItem) {
}

For Java, you are passing something that is a List to a method that'll only work with ArrayLists. If it allowed you to do that, you could for example, change the implementation to a LinkedList<Student>, and still pass it to replace which wouldn't be correct.

Either use replace(List<Student>), or declare CS242 as ArrayList<Student> CS242, although the former is considered best practice.

share|improve this answer
feedback

In the pasted code, the two methods replace and delete are not changed to List<Student>. This is what is causing the compile errors.

They are there in the first place because in line 9 you are creating an ArrayList and save it in an List variable. This works fine because ArrayList implements List, so every ArrayList is also a List. Later, you try to call functions that want an ArrayList as parameter with said List. While this particular List is also an ArrayList (because you used the ArrayList constructor), this is not true for all Lists. So you have to either cast this explicitly or save it as ArrayList in the first place.

share|improve this answer
feedback

Not the answer you're looking for? Browse other questions tagged or ask your own question.