开发者

How to initialize a multi-dimensional array of unknown array length

开发者 https://www.devze.com 2023-03-09 12:26 出处:网络
I am getting problems when generating a multi-开发者_如何转开发dimensional array with unknown size. How can I fix it?To generate a multi-dimensional array with unknown size is called a jagged array.

I am getting problems when generating a multi-开发者_如何转开发dimensional array with unknown size. How can I fix it?


To generate a multi-dimensional array with unknown size is called a jagged array.

For example:

String[][] array = new String[5][];

Java uses arrays of arrays for multi-dimensional arrays. I think you have to specify the first size. Otherwise, use list of lists.

ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();


Array is static. ArrayList is dynamic.

Before creating an array you should be aware of the size of the array. To create a multidimensional array without knowing array size is not possible.

Better you have to use a nested ArrayList or nested Vector:

ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
0

精彩评论

暂无评论...
验证码 换一张
取 消