Converting non-generic List type to Generic List type in Java 1.5

后端 未结 5 2120
鱼传尺愫
鱼传尺愫 2021-02-07 06:21

I have a List that is guaranteed to contain just one type object. This is created by some underlying code in a library that I cannot update. I want to create a List
5条回答
  •  名媛妹妹
    2021-02-07 07:10

    Try this:

    List objectsWithType = Arrays.asList((ObjectType[])wildcardObjects).toArray());
    

    But remember that this will produce a fixed length List. If you try to add or remove element from this list, it will throw an error. So be always careful while using Arrays.asList().

提交回复
热议问题