Create an array of ArrayList elements

后端 未结 12 1972
遥遥无期
遥遥无期 2020-12-03 01:27

I want to create an array that contains ArrayList elements.

I\'ve tried

ArrayList name[] = new ArrayList()[];
         


        
12条回答
  •  孤街浪徒
    2020-12-03 01:44

    If you do want an array of ArrayList, you'll have to initialise each position of the array individually:

    int size = 9; // 9 is just an example
    // you can remove the annotation, but you'll be warned:
    // Type safety: The expression of type ArrayList[] needs unchecked conversion 
    // to conform to ArrayList[]
    @SuppressWarnings("unchecked")
    ArrayList name[] = new ArrayList[ size];
    for( int i = 0; i < size; i++) {
        name[ i] = new ArrayList();
    }
    

提交回复
热议问题