Java – creates an ArrayList on each index of the list

I have a list like this

public List<String>[] Depth_List;

Now in each index of this list, I want to create and ArrayList The size of the array will be given by the user The function that initializes it is given as

public  void CreateList(int deep){
    Depth_List= new ArrayList<String>[deep];

        for (int d_i = 0; d_i <deep; d_i++)
        {
            Depth_List[d_i]= new ArrayList<String>();
        }

    }
}

What on earth did I do wrong?

to greet

Solution

You can do this:

public List<List<String>> depthList;
public  void CreateList(int deep) {
    depthList= new ArrayList<List<String>>();

    for (int d_i = 0; d_i <deep; d_i++) {
        depthList.add(new ArrayList<String>());
    }

}

Note: please read about Java Naming Conventions Your code will be more readable

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>