Linked Questions
313 questions linked to/from How can I create a generic array in Java?
89 votes
4 answers
122k views
How to create a generic array? [duplicate]
I don't understand the connection between generics and arrays. I can create array reference with generic type: private E[] elements; //GOOD But can't create array object with generic type: elements ...
52 votes
3 answers
149k views
Error: Generic Array Creation [duplicate]
I don't understand the error of Generic Array Creation. First I tried the following: public PCB[] getAll() { PCB[] res = new PCB[list.size()]; for (int i = 0; i < res.length; i+...
5 votes
2 answers
10k views
How to convert hashmap to Array of entries [duplicate]
I have a map private HashMap<Character, Integer> map; I want to convert it to array but when I do that/I get this: Entry<Character, Integer> t = map.entrySet().toArray(); **Type ...
5 votes
4 answers
16k views
How to create generic array? [duplicate]
Possible Duplicate: Java how to: Generic Array creation How to create an array of type T[] in Java? I can't use Arrays.newInstance() since I have no objects of Class<T>. Is there a generic ...
6 votes
3 answers
3k views
Java Generics Error [duplicate]
Possible Duplicate: Java how to: Generic Array creation Error: Generic Array Creation I am getting this error: Cannot create a generic array of T This is my code (error on line 6): 1 public ...
8 votes
3 answers
4k views
Java Generics: Array containing generics [duplicate]
Possible Duplicate: Java how to: Generic Array creation Error generic array creation I have been tasked with writing a Hash Table in Java, which must work with any data type. The rules on the ...
9 votes
1 answer
2k views
How to safely convert from a Collection of generic types to an array? [duplicate]
For various reasons I want to convert a list to an array, however the Collection contains objects that are themselves generics. I have tried the following four options to get it to compile without ...
1 vote
3 answers
9k views
array of type T (Java generics) [duplicate]
Although this code compiles successfully, it throws a class cast exception error at run-time: class Test { public static void main(String[] args) { MyArray<String> x = new MyArray&...
1 vote
3 answers
8k views
Can we create an Array of Generic class? [duplicate]
Possible Duplicate: Java how to: Generic Array creation I want to create a Stack which contains 10 elements, I'm trying to do this with Generics. I started by creating a Stack class of Type T, but ...
1 vote
4 answers
4k views
Generic array creation in java [duplicate]
Possible Duplicate: Create instance of generic type in Java? Java how to: Generic Array creation I am trying to create a create a class of generic type. This is my class file. public class ...
2 votes
3 answers
5k views
Implementation of Queue<E> interface [duplicate]
I am having trouble understanding the nuances of generics, interfaces, and arrays and how to use them together. I have an assignment for an intermediate Java class. The assignment is as follows: ...
1 vote
3 answers
2k views
Multidimensional generic array Java [duplicate]
Possible Duplicate: Java how to: Generic Array creation I wanna create something like this: public class MyClass<T> { private int row; private int column; private T[][] result; ...
3 votes
2 answers
697 views
Create generic array for toArray method [duplicate]
I have this following method: public static <T, U> T[] getKeysForValue(Map<T,U> map,U value){ if(map == null || map.isEmpty()) { return null; } Set<T> keys = ...
-3 votes
2 answers
2k views
Generic array in ArrayList implementation [duplicate]
How to make following code completely generic? Why I'm not allowed to write: private E[]store = new E[length]; import java.util.Arrays; public class MyArrayList<E> { private int size = 0; ...
0 votes
0 answers
4k views
Why IDEA shows that “Type parameter 'T' cannot be instantiated directly”. What I want to do is declare a generic array [duplicate]
enter image description here the code is below: package linear_list; public class SeqList<T> { protected int n; protected T[] element; private static final int MIN_CAPACITY=...