0

I am creating a list data structure and am having trouble with the generics syntax for actually using it. All I am trying to do is create an instance of ArrayLinearList<String> and of size 2 and put some strings in it. I have been trying to figure out why setting the first slot to "one" is not correct. This is the error and my code snippet.

   myList[0] = "one";

The error message is: error: incompatible types: String cannot be converted to ArrayLinearList<String>

public class ArrayLinearList<E> implements LinearListADT<E> {


 private Object[] array;
int currentSize = 0;

//Constructor (no arguments)
public ArrayLinearList() {
    currentSize = 2;  
    // array = (ArrayLinearList[]) new Object[2];   //Start with a container of size 2
    array = new Object[2];
}
   public static void main(String[] var0) {

       ArrayLinearList<String>[] myList;
       myList = new ArrayLinearList[2];
       myList[0] = "one";
   }
}

I am having quite a bit of trouble with the syntax with using generics in java. In my mind I have an array of size 2 where I am going to be placing strings. I will add more methods later but I want to understand why my current syntax is incorrect for placing this string in the array.

Marco13
  • 50,927
  • 9
  • 71
  • 148
Guillermo Alvarez
  • 1,457
  • 2
  • 16
  • 22
  • Generally speaking, generics and arrays don't mix in Java. Use a List instead. – Louis Wasserman Feb 06 '15 at 17:28
  • @guillermoalvarez, I suggest you take a quick read through this clearly written tutorial: http://www.javacodegeeks.com/2011/04/java-generics-quick-tutorial.html –  Feb 06 '15 at 17:43
  • @tgm1024: I'd describe compiler errors on "new T[]" and "new Foo[]" as "not mixing." I fully understand the difference between invariant generics and covariant arrays, but trying to squeeze generics and arrays together is rarely a good idea in practice, and using a `List` is almost always a better alternative that doesn't force you to suppress compiler warnings or perform unsafe casts. – Louis Wasserman Feb 06 '15 at 17:49
  • @LouisWasserman, I'm not saying you're declaring that as mixed. But curious: Where do you perceive the unsafe cast in using arrays? A String array is of a type. ``ArrayList`` is of a type. Do you have an example of the unsafe cast you mention? As I see it, the only confusion left is in covariance. –  Feb 08 '15 at 16:51
  • Usually you have to do `(Foo[]) new Foo[n]` to get an array of a generic type, and that'll give you an unchecked cast warning. And that _can_ lead to ClassCastExceptions at runtime, precisely because arrays are covariant and generics are invariant. – Louis Wasserman Feb 08 '15 at 17:32
  • @LouisWasserman, Ah, Gotcha, my apologies. I misunderstood where you were going. For anyone else curious about this, see the explanation here: http://stackoverflow.com/a/2927427/4229245, and here: http://docs.oracle.com/javase/tutorial/java/generics/restrictions.html#createArrays –  Feb 09 '15 at 15:44

3 Answers3

1

Here:

   ArrayLinearList<String>[] myList;

you define an array that holds ArrayLinearList<String> elements, not Strings, this is why you get the error message.

Marco13
  • 50,927
  • 9
  • 71
  • 148
szpetip
  • 333
  • 6
  • 12
1

Your code here

ArrayLinearList<String>[] myList;
myList = new ArrayLinearList[2];//you have defining array myList of type ArrayLinearList
myList[0] = "one";//you are trying to store String to array which can hold ArrayLinearList

Here ArrayLinearList<String> means that your list will hold values of type String (provided we define it correctly in the code). But ArrayLinearList<String>[] will hold only reference of type ArrayLinearList and not String itself.

Marco13
  • 50,927
  • 9
  • 71
  • 148
SMA
  • 33,915
  • 6
  • 43
  • 65
0

You need to use

myList.array[0] = "one";

Because myList is not an array. It's an object which you use to store an array within.

omerfarukdogan
  • 799
  • 8
  • 25