5 Replies Latest reply on May 3, 2007 4:41 PM by 807599

# getting length of int[] - need help

hi i am making a paint program and one of the things i want it to do it to be able to draw a polygon from an int array of x points and y points.

these arrays work fine but the constructor for a polygon needs the number of corners aswell and since the user decides this i cannot use a constant.

the way i thought id do it is to get the number of points in one of the arrays but there doesnt seem to be a method for doing it!

does anyone know how i can get the length of an int array? (not the capacity but the number of elements in it)
• ###### 1. Re: getting length of int[] - need help
There's no difference between capacity and number of elements in an array.
``int[] arr = new int[10];``
That array has ten elements. They all have a value. At the moment of creation the value will be 0/false/null as appropriate for the type of the elements.

If you want to know which elements have been explicitly set, you'll have to either keep a separate counter or bitmask, or initialize them all to some invalid value such as null or -1, if such value exists.
• ###### 2. Re: getting length of int[] - need help
hmm ok thanks
• ###### 3. Re: getting length of int[] - need help
is there a way i can set the capacity of an int array after i have initialised it?
• ###### 4. Re: getting length of int[] - need help
is there a way i can set the capacity of an int array
after i have initialised it?
Arrays don't have a capacity, they have a length. If you mean can you change the length of the array after initialization, then no, you can only create a new array with a new length.
• ###### 5. Re: getting length of int[] - need help
Have you looking into Sun's Graphics tutorial? http://java.sun.com/docs/books/tutorial/2d/index.html

One way to represent a polygon is to use GeneralPath:

http://java.sun.com/javase/6/docs/api/java/awt/geom/GeneralPath.html

You don't need to predeclare any arrays to use it.