0

I have read What's the reason I can't create generic array types in Java? . I came to it when I tried this code:

HashMap<String, String>[] ret = new HashMap<String, String>[arraySize];

I decided to "get wild" and try something else

ArrayList<HashMap<String, String>> ret = new ArrayList<HashMap<String, String>>();

Now, what I don't understand is why the first option is bad/not-supported and the second option compiles.

What am I missing? In the first method I get an error and cannot compile while the second method has no compiler issues.

I prefer to follow intelligent programming principles, but do not understand the difference in this particular case.

Community
  • 1
  • 1
  • 1
    An `ArrayList` is not an array. Or better yet, a random generic type is not an array (forget that it's called an `ArrayList`, it's just another type). – Sotirios Delimanolis Apr 07 '14 at 03:03
  • ty, but it does allow me to have an array of those objects, per se. Maybe that is where I am confused? –  Apr 07 '14 at 03:04
  • 1
    You can look at the underlying implementation if you want. `ArrayList` uses a `Object[]`. – Sotirios Delimanolis Apr 07 '14 at 03:05
  • Oh, so what you're saying is that b/c an ArrayList instance is of a class, then it is able to bypass this requirement in that it can store an internal array of objects w/o having a requirement on the type of the object? –  Apr 07 '14 at 03:05
  • 1
    Can you re-attempt after removing the trailing parentheses in the line where you try to make an array of the HashMaps? – Kode Charlie Apr 07 '14 at 03:07
  • 1
    I don't understand the question in your comment. Regardless of what you put in the `ArrayList`, it will store it in an `Object[]` since `Object` is the parent type of all reference types. If you look at the implementation, you will notice that it satisfies the generic signature through casting. – Sotirios Delimanolis Apr 07 '14 at 03:07
  • @KodeCharlie Corrected. –  Apr 07 '14 at 03:08
  • @SotiriosDelimanolis Thank you. I will have to see how it works when I figure out how to find the source of `ArrayList`, but I believe you have answered the question (which guest has summarized). If you post, I'll accept. I guess the essence of the answer is that I am doing it correctly even if I don't fully understand why. –  Apr 07 '14 at 03:10
  • 1
    @JeremyMiller Your JDK installation contains a `src.zip` with all the source code. Alternatively, just search for `java ArrayList source code`. Grepcode.com should have what you need. – Sotirios Delimanolis Apr 07 '14 at 03:11
  • Thanks... I haven't looked for that source before, just Android source. –  Apr 07 '14 at 03:12

1 Answers1

1

To point out what you already know, the second one compiles. An ArrayList is not an array, so your program obviously won't run into the generic array problem.

You're probably wondering how ArrayList does it then. In the implementation of ArrayList that I've seen, it internally uses an Object array--not generic. The class just does a lot of casting.

guest
  • 5,790
  • 23
  • 39