Thor Codinson Thor Codinson - 3 months ago 7x
Java Question

(Java) How to acquire number of elements within a item in a list?

I have found plenty of questions and answers on here about how to access the number of elements within a list, but I haven't seen a question asked about how to access the number of elements within a certain item on a list.

I have a list of Point arrays that contain the ordered pairs of a polygon. (one list element for each polygon)(there will only ever be two polygons) I need to create new lists that are dynamically sized based on the number of elements WITHIN a certain index in this list. (ex. if the first polygon has eight points, I need to create a new List of size eight and put those eight points in it. Same for the second based on its number.) I know the List Class contains a

function, but trying :

private List<Poly.Point[]> mergePolyList;
mergePolyList = LayoutMerger.mergePolyList;
//LayoutMerger is a separate Java file where I made the arrays
Poly.Point [] pArrayBox1 = new Poly.Point[mergePolyList.get(0).size()];
//^Causes a "cannot find symbol" error

results in a "cannot find symbol" error. I'm sure there is a way to do this, I'm just unsure of the correct syntax? I suspect I need to iterate through the list and call size() somehow? Thanks for the help and let me know if I need to provide any other information.


Your mergePolyList is a list of arrays. You need to use the length attribute of arrays:

Poly.Point [] pArrayBox1 = new Poly.Point[mergePolyList.get(0).length];