Search code examples
javagenerics

My generics test in Java doesn't work!


I am doing some Java studying, especially in the area of generics.

I'm fairly familiar with generics in C#, but in Java, it's a whole different story.

I used a few samples that worked just fine for testing, and I am able to replicate most of my C# code in Java just fine.

However, when I try the following example, it doesn't work:

private static <T> void swapKundeData(ArrayList<T> data, int index1, int index2) {

    T temporary = (T) data.get(index1);

    data.set(index1, data.get(index2)); //Does not compile
    data.set(index2, temporary); //Does not compile

}

The error I am receiving is:

The method set(int, capture#5-of ? extends ExtendTest) in the type ArrayList is not applicable for the arguments (int, ExtendTest)

An equivalent of this works just fine in C# - so what's going on?

I've read that Java has received a lot of criticism when it comes to generics. Is this a part of that criticism? The Remove and Add method of the data variable works just fine.


Solution

  • The problem was Eclipse.

    I had not saved the file, so apparently nothing was recompiled.

    Wow, I am really impressed of that IDE's incapabilities and annoyances, such as speed.

    Oh well.