-2

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 size() 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.

  • I should probably also point out that `Point` is a custom class in this case and does not use the standard Java `Point` library. – Thor Codinson Jul 07 '16 at 15:14
  • 4
    The elements of your `mergePolyList` are *arrays* not collections. Arrays do not have a `size()` method; instead, they have `length`. – John Bollinger Jul 07 '16 at 15:16
  • Something simple, of course.`Poly.Point [] pArrayBox1 = new Poly.Point[mergePolyList.get(0).length];` works perfectly. Thank you John Bollinger, forgive the simple error. – Thor Codinson Jul 07 '16 at 15:18

1 Answers1

0

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];
Simon
  • 774
  • 4
  • 21