Java-静态和动态数组初始化 [英] Java - Static and Dynamic Array Initialization

查看:124
本文介绍了Java-静态和动态数组初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是真的,在运行时初始化的每个数组都是动态的, 在编译过程中初始化的每个数组都是静态的吗?

例如:

int array[];                 
public main() {      
    array = new int[100];    
}

编译器知道数组有多少个元素,这样它就可以在编译期间将其初始化吗?还是我需要给每个int一个值,使其变为静态? 像这样:

int array[3] { 1, 2, 3};              

并且可以定义一个数组在main()函数之外应该有多少个元素? (不给每个int一个值):

int array[100];      
public main() {
}

我正在编写一个小游戏,它必须运行得非常快.我读取动态数组需要更长的时间来处理,因此我想尝试使用静态数组,但是我不确定数组何时变为静态或动态. 我搜索了许多不同的教程,但找不到答案.
感谢您的阅读.

解决方案

动态分配和静态分配的区别是模棱两可的(它在某种程度上取决于语言的含义).从最一般的意义上讲,静态分配意味着某些大小已经预先确定,也许是在编译时.

在Java中,任何对象(包括数组)始终在运行时分配.那不一定意味着它是动态的,就它在运行时不能更改的意义而言,它可能仍然是静态的.示例:

public class Test1 {
    public final int[] array1 = new int[10];

    public int[] array2 = new int[20];

    public void setArray2Size(int size) {
         array2 = new int[size];
    }
}

arrayFixed的大小为10,并且无法在运行时更改.请注意 final 关键字.这使您只能分配一次"array1"成员.因此,您不能为该成员分配其他数组.

现在array2不是最终的,因此您可以在任何时候为它分配一个不同的数组,就像setArray2Size()方法一样.如果在初始分配之后没有赋值,则array2仍将是 static ,因为它不能更改(因为没有代码可以这样做),尽管通过声明可以更改./p>

数组的具体实例一旦创建就无法调整大小 (在Java中没有语言元素可以表示数组大小的调整).对于初学者来说很难理解,但是像array2 这样的变量不是array .它是对该数组的引用.但是,您可以 将array2持有的引用替换为对另一个数组的引用,如setArray2Size()方法中对array2所示.

Is it true that every array that is initialized during runtime is dynamic and every array that is initialized during compiling is static?

for example:

int array[];                 
public main() {      
    array = new int[100];    
}

the compiler knows how many elements the array has so it can initilize it during compiling right? or do i need to give every int a value so it becomes static? like this:

int array[3] { 1, 2, 3};              

and is it posible to define how many elements an array should have outside the main() function? (without giving every int a value) like this:

int array[100];      
public main() {
}

I am programming a little game and it has to run really fast. I read dynamic arrays need a bit longer to process so i want to try it with static arrays, but I am not sure when an array becomes static or dynamic. I searched in many diffrent tutorials but i couldn't find an answer for that.
thanks for reading.

解决方案

The distinction of dynamic and static allocation is ambigous (it somewhat depends on the language what it means). In the most general sense, static allocation means that some size has been predetermined, maybe at compile time.

In java, any objects (that includes arrays) are always allocated at runtime. That doesnt necessarily mean its dynamic, it may still be static in the sense that it can't be changed at runtime. Example:

public class Test1 {
    public final int[] array1 = new int[10];

    public int[] array2 = new int[20];

    public void setArray2Size(int size) {
         array2 = new int[size];
    }
}

The arrayFixed is of size 10, and that can't be changed at runtime. Do note that the final keyword. This lets you assign the "array1" member only once. So you can't assign a different array to this member.

Now array2 is not final, so you can at any point assign a different array to it, like the setArray2Size()-method does. If there were no assignment aftter the initial assignment, array2 would still be static in the sense that it cant be changed (because there is no code to do so), although by declaration changing it is allowed.

A concrete instance of array can not be resized ever once created (there is no language element to express resizing an array in java). It is somewhat hard to grasp for beginners, but a variable like array2 is not the array. Its a reference to the array. You can however replace the reference that array2 holds with the reference to another array, as shown for array2 in setArray2Size()-method.

这篇关于Java-静态和动态数组初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆