0

codes in java like this "Integer[] integers=new Integer[10];" are fine,when I try codes like "Map.Entry<Integer, Integer>[] entrys=new Map.Entry<Integer, Integer>[number];",I get the following error :

Cannot create a generic array of Map.Entry

then I create a class:

class createTarray<T>{
T[] ts;
@SuppressWarnings("unchecked")
public  createTarray(Class<?> class1,int length) {
ts=(T[])Array.newInstance(class1, length);
}
public T[] getTarray() {
return ts;
}
}

and try to get array with codes bellow:

createTarray<Map.Entry<Integer, Integer>> tarray=new createTarray<Map.Entry<Integer,Integer>>(Map.Entry<Integer,Integer>.class,number);

entrys=tarray.getTarray();

and also get error:

Multiple markers at this line


- Integer cannot be resolved to a variable
    - Map.Entry cannot be resolved to a variable
    - Integer cannot be resolved to a variable
    - Syntax error on token ">", void expected after 
     this token

so could someone help me out? I get quite confused these days by this

4
  • You can't really create a generic array. The biggest question is why are you even trying? Commented May 29, 2018 at 5:45
  • If you have a bunch of Map.Entry objects, is there any reason not to put them in a Map? Commented May 29, 2018 at 5:51
  • Yeah, is this code supposed to do something useful, or are you just playing around? Commented May 29, 2018 at 5:52
  • In that case you case List<Map.Entry<Integer, Integer>> incase you want index based fast access you can create Map and store indexes in key and Entry<> as values. Commented May 29, 2018 at 5:56

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.