Create an array of ArrayList elements

后端 未结 12 1900
遥遥无期
遥遥无期 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:38

    You may try this:

    ArrayList[] arr=new ArrayList[5];
    for(int i=0;i<5;i++)
         arr[i]=new ArrayList<String>();
    arr[1].add("35");
    arr[1].add("ME");
    arr[1].add("HELLO");
    System.out.println(arr[1]);
    

    Output:

    [35, ME, HELLO]
    
    0 讨论(0)
  • 2020-12-03 01:44

    I know this is a bit old but I am going to respond to this anyway for future views.

    If you really want an ArrayList<String>[] structure, you can simply create a class that extends ArrayList and make an array of that class:

    public class StringArrayList extends ArrayList<String>{}
    

    And in your implementation:

    ArrayList<String> name[] = new StringArrayList[9];
    

    Here is a sample:

    package testspace.arrays;
    
    import java.util.List;
    
    public class TestStringArray {
    
        public static void main(String[] args) {
            List<String>[] arr = new StringArrayList[10];
            for(int i = 0; i < arr.length; i++){
                // CANNOT use generic 'new ArrayList<String>()'
                arr[i] = new StringArrayList(); 
                for(int j = 0; j < arr.length; j++){
                    arr[i].add("list item #(" + j + "|" + i + ")");
                }
            }
    
            StringBuilder sb = new StringBuilder();
            for(final List<String> list : arr){
                for(final String str : list){
                    sb.append(str + " ");
                }
                sb.append("\n");
            }
            System.out.println(sb.toString());
        }
    
    }
    

    NOTE You will get a runtime error if you use this instead : arr[i] = new ArrayList<String>()

    0 讨论(0)
  • 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<String>[]
    @SuppressWarnings("unchecked")
    ArrayList<String> name[] = new ArrayList[ size];
    for( int i = 0; i < size; i++) {
        name[ i] = new ArrayList<String>();
    }
    
    0 讨论(0)
  • 2020-12-03 01:46

    This is not proper OO and this sort of code is very implementation coupled. If you need to do such thing, probably you did something wrong. If the code is not yours, the person who made it probably did something wrong.

    If you know how many elements are there (or even if you didn't), why not use Map<Integer,List<String>>?

    0 讨论(0)
  • 2020-12-03 01:46

    There is a way to do it with only little modification of your code.

    ArrayList<String>[] name = new ArrayList[10]; 
    

    This will give you a array of ArrayList.

    0 讨论(0)
  • 2020-12-03 01:47

    Array of ArrayLists, example with Loops

    //Declare
    Object[] arrayLists = new Object[size];    
    
    //Fill
    for (int j=0; j < size; j++) {
         arrayLists[k]= generateAnArrayList(j);  //or whatnot
    }
    
    //Usage
    for (int j=0; j < size; j++) {
        ArrayList<Long> al = (ArrayList<Long>) arrayLists[j];
        //do something here
    }
    
    0 讨论(0)
提交回复
热议问题