Creating an array of Sets in Java

后端 未结 3 396
粉色の甜心
粉色の甜心 2021-02-02 15:13

I\'m new to Java so I\'m probably doing something wrong here, I want to create an array of Sets and I get an error (from Eclipse). I have a class:

public class R         


        
3条回答
  •  [愿得一人]
    2021-02-02 15:19

    From http://www.ibm.com/developerworks/java/library/j-jtp01255/index.html:

    you cannot instantiate an array of a generic type (new List[3] is illegal), unless the type argument is an unbounded wildcard (new List[3] is legal).

    Rather than using an array, you can use an ArrayList:

    List> groupMembers = new ArrayList>();
    

    The code above creates an empty ArrayList of Set objects. You would still have to instantiate every Set object that you put into the ArrayList.

提交回复
热议问题