1

I just start to learn Java and I am getting "@SuppressWarnings("unchecked")"

I am sure that one of my static variable is making trouble

static ArrayList<Integer>[] docNumber = (ArrayList<Integer>[]) new ArrayList[20];

eclipse said "Type safety: Unchecked cast from ArrayList[] to ArrayList[]" but i am not really sure how to avoid this problem

can you tell me how to fix this problem?

thanks

Paul Bellora
  • 51,514
  • 17
  • 127
  • 176
Dc Redwing
  • 1,621
  • 9
  • 30
  • 40
  • possible duplicate of [Initialize Java Generic Array of Type Generic](http://stackoverflow.com/questions/1025837/initialize-java-generic-array-of-type-generic) – Paul Bellora Nov 08 '12 at 20:05
  • 1
    Are you sure you want to create an array of `ArrayList`? Maybe you really just want `ArrayList`? – Jim Garrison Nov 08 '12 at 20:06
  • Actually, the accepted answer on the post I linked to points to the wrong explanation. See here instead: http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes.html#FAQ104 – Paul Bellora Nov 08 '12 at 20:09

3 Answers3

2

Can't be avoided with arrays.

Use a List<List<Integer>> instead.

Dmitri
  • 8,537
  • 4
  • 32
  • 39
1

In order to avoid the @SuppressWarnings(“unchecked”) you should give the compiler the information needed to perform all type checks that would be necessary to ensure type safety, see unchecked FAQ.

In your case, I assume that you are trying to maintain a list of document integers, for this you will need:

List<Integer> docNumber=new ArrayList<Integer>();

If you would like to keep a list of lists then you can do:

List<List<Integer>> docNumber=new ArrayList<List<Integer>>();
dan
  • 12,532
  • 3
  • 34
  • 44
0

Are you trying to achieve this (List of Array Of Integer)?

   static ArrayList<Integer[]> docNumber = new ArrayList<Integer[]>();
Yogendra Singh
  • 32,067
  • 6
  • 59
  • 71