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
54.8k9 gold badges84 silver badges170 bronze badges
asked Feb 6, 2015 at 14:29
6
  • Generally speaking, generics and arrays don't mix in Java. Use a List instead. Commented Feb 6, 2015 at 17:28
  • @guillermoalvarez, I suggest you take a quick read through this clearly written tutorial: javacodegeeks.com/2011/04/java-generics-quick-tutorial.html Commented Feb 6, 2015 at 17:43
  • @tgm1024: I'd describe compiler errors on "new T[]" and "new Foo<Bar>[]" 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. Commented Feb 6, 2015 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<String> 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. Commented Feb 8, 2015 at 16:51
  • Usually you have to do (Foo<Bar>[]) 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. Commented Feb 8, 2015 at 17:32

3 Answers 3

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
54.8k9 gold badges84 silver badges170 bronze badges
answered Feb 6, 2015 at 14:32

Comments

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
54.8k9 gold badges84 silver badges170 bronze badges
answered Feb 6, 2015 at 14:33

Comments

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.

answered Feb 6, 2015 at 15:11

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.