简体   繁体   English

Java数组大小

[英]Java Array size

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. 我问我Java中的数组初始化是否与C相同。在C中,您无法在程序运行时定义数组的大小。 Is it possible in Java (or just right as concept)? 在Java中是否可能(或者恰如其分)?

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 在我的情况下,我必须使用数组而不是arraylist因为我在Panel上绘制了Polyline

g.drawPolyline(xPoints[], yPoints[], n);

Thanks for help 感谢帮助

创建后不能更改数组大小,但可以使用System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)将数组的值复制到另一个更大的数组,不用担心关于它的速度,因为它是内置函数并使用JNI实现,所以速度非常快

C does not have the fundamental concept of an "array" as Java does; C没有像Java那样的“数组”的基本概念; in C, you'd 在C中,你是

malloc(some_size * sizeof(one_element))

and affect it to a one_element * (of course, that is a gross description). 并将其影响到one_element * (当然,这是一个粗略的描述)。

In Java, arrays are equally dynamically allocated; 在Java中,数组同样是动态分配的; if you know the size, at runtime, then you can, for an array of SomeType and of size someSize do: 如果你知道大小,在运行时,那么你可以,对于SomeType和大小someSize的数组做:

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 ; 在Java中,这会导致IndexOutOfBoundsException ;
  • in C, this is undefined behavior. 在C中,这是未定义的行为。

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... 总而言之,除了错误地使用“数组”的后果之外,C中的内容以及Java中的内容实际上与创建数组开始时的语法完全不同......

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(或任何其他List)并在需要的地方将其转换为简单数组,如下所示:

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. 在C中,您无法在程序运行时定义数组的大小。 Is it possible in Java (or just right as concept)? 在Java中是否可能(或者恰如其分)?

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(...) ). 如果要重新定义数组的大小,则需要创建一个新数组并复制旧数组( System.arraycopy()Arrays.copyOf(...) )。

In my case I have to use an array and not an arraylist because I'm drawing a Polyline on a Panel 在我的情况下,我必须使用数组而不是arraylist因为我在Panel上绘制了Polyline

Well, you could still use a list and call toArray(...) on it. 好吧,你仍然可以使用一个列表并调用toArray(...) 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. 由于您最终要调用Graphics.drawPolyline(...)您必须维护两个List<Integer>或最好是List<Point> ,并在该列表内部构造x和y数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM