I'm asking me if the initialization of array in Java is the same as C. In C you can't define the size of the array while the program is running. Is it possible in Java (or just right as concept)?
public int[] createArray(int size) {
return new int[size];
}
In my case I have to use an array and not an arraylist because I'm drawing a Polyline on a Panel
g.drawPolyline(xPoints[], yPoints[], n);
Thanks for help
4 Answers 4
You can't change array size once it's created,but you can use System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
to copy the value of the array to another bigger array,don't worry about its speed,because it's built-in function and implemented with JNI,so it's very fast
-
In Java that would be System._array_copy :)fge– fge2016年03月07日 16:00:13 +00:00Commented Mar 7, 2016 at 16:00
-
@fge,yes,it's
arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
starkshang– starkshang2016年03月08日 01:14:26 +00:00Commented Mar 8, 2016 at 1:14
C does not have the fundamental concept of an "array" as Java does; in C, you'd
malloc(some_size * sizeof(one_element))
and affect it to a one_element *
(of course, that is a gross description).
In Java, arrays are equally dynamically allocated; if you know the size, at runtime, then you can, for an array of SomeType
and of size someSize
do:
final SomeType[] myArray = new SomeType[someSize];
In essence, it's quite the same; including the fact that in both cases arrays are NOT resizable, but with a huge difference on what happens if you specify an invalid index:
- in Java, this leads to an
IndexOutOfBoundsException
; - in C, this is undefined behavior.
All in all, apart from the consequences of using "arrays" incorrectly, what goes in C and what goes in Java only really differs by the syntax to create the array to begin with...
-
Thanks for your very detailed answer :)user3721668– user37216682016年03月07日 15:56:48 +00:00Commented Mar 7, 2016 at 15:56
As others have mentioned, you cannot do this. But instead you can use ArrayList (or any other List) and where needed, convert it to simple array, like this:
ArrayList<String> arrayList = new ArrayList<>();
String strings[] = (String[])arrayList.toArray();
In C you can't define the size of the array while the program is running. Is it possible in Java (or just right as concept)?
Yes you can define the size of an array at runtime, just not redefine it. This will create an object on the heap with either enough space to hold the required number of primitives (in case of a primitive array) or object references (think of them as pointers).
If you want to redefine the size of an array you'd need to create a new one and copy the old (System.arraycopy()
or Arrays.copyOf(...)
).
In my case I have to use an array and not an arraylist because I'm drawing a Polyline on a Panel
Well, you could still use a list and call toArray(...)
on it. This also is an example of creating an array at runtime.
Since you want to eventually call Graphics.drawPolyline(...)
you'd have to either maintain two List<Integer>
or preferably a List<Point>
and construct the x and y arrays internally out of that list.
-
Maybe the problem of your second answer, related to my own case, is that the list of points that I have to draw the Polyline is not composed just by int elements, but a couple of points (x and y)user3721668– user37216682016年03月07日 16:01:31 +00:00Commented Mar 7, 2016 at 16:01
-
@user3721668 yes, that's why I edited it. From a design point of view I'd rather provide a list of points and construct the two arrays before calling the draw method. That way you can't run into errors where the length of both arrays differs or one is sorted and the other is not. (Another option might be to use
GeneralPath
and collect the coordinates there).Thomas– Thomas2016年03月07日 16:06:02 +00:00Commented Mar 7, 2016 at 16:06 -
exactly, that's what i've just done and it seems that works properlyuser3721668– user37216682016年03月07日 16:10:39 +00:00Commented Mar 7, 2016 at 16:10
can't define the size of the array while the program is running
? you can´t resize an array, but you can create a new array with a sizex
, or just straight up reassign an array variable.