简体   繁体   English

为什么在Java中可以使用变量作为长度来初始化数组?

[英]Why can an array can be initialized with a variable as length in java?

I try use a integer array in java with the code below: 我尝试使用下面的代码在Java中使用整数数组:

    public static void main(String[] args) {
        int[] array = testArray(100);
        System.out.println(array.length);
        for(int i = 0; i < 100; i++)
           System.out.println(array[i]);
    }

    public static int[] testArray(int size){
        int[] array = new int[size];
        for(int i = 0; i < size; i++)
           array[i] = i;
        return array;
    }

I also test an integer array in C++ as below: 我还在C ++中测试整数数组,如下所示:

       #include<iostream>
       using namespace std;

       void getArray(int size)
       {
            int array[size];
            for(int i=0; i<size; i++)
                array[i]=i; 
      }

      int main()
      {
           getArray(10);
           return 0;
      }

And I always get the right answer with the two snippets, why? 我总是用两个片段得到正确的答案,为什么? since I think that the length of an array cannot be variable for language such as java, c and c++. 因为我认为对于Java,C和C ++等语言,数组的长度不能可变。

The three languages are different, and the features of the different languages will, well, differ. 三种语言是不同的,并且不同语言的功能也将有所不同。 In particular, in C99 you can define arrays of a variable in the stack (for those that program mainly Java, this is commonly used to refer to a size that is not a compile time constant, not that the array will change sizes). 特别是在C99中,您可以在堆栈中定义变量的数组(对于那些主要编程Java的变量,通常用于表示不是编译时间常数的大小,而不是数组会更改大小)。

In C++ you cannot declare them in the stack (GCC allows this, with a non-conforming extension to the language that mimics C99 behavior), but you can dynamically allocate memory with new of the given size and store the pointer in a variable. 在C ++中,您不能在堆栈中声明它们(GCC允许这样做,并且对模拟C99行为的语言进行了不合格的扩展),但是您可以使用给定大小的new动态分配内存并将指针存储在变量中。

In Java you cannot create an array in the stack ever, and all you can do is dynamically allocate the array with new and store a reference. 在Java中,您永远无法在堆栈中创建数组,而您所能做的就是为数组动态分配new并存储引用。

int size = 100;

// C99
int data[size];

// C++
// int data[size];          // error, but allowed by gcc
int *data = new int[size]; 

// Java
int[] data = new int[size];

Java and C are different languages. Java和C是不同的语言。 What's allowed in standard C only as dynamic allocations ( malloc / calloc ) is allowed in Java by default, because all array allocations in Java are dynamic anyway; 默认情况下,标准C中仅允许动态分配( malloc / calloc ),而Java中只允许动态分配,因为Java中所有数组分配仍然是动态的。 the concept of explicit stack allocation is missing from Java. Java缺少显式堆栈分配的概念。

Length of an array in Java is not variable. Java中数组的长度不是可变的。 It is defined upon creation of the array (that is, dynamically at runtime ), but cannot be changed after that. 它是在创建数组时定义的(即在运行时动态地定义),但此后不能更改。

Moreover, every array has a length property that can be used to make a for loop able to iterate through an array of any length: 而且,每个数组都有一个length属性,可用于使for循环能够遍历任意长度的数组:

for(int i = 0; i < array.length; i++)
   System.out.println(array[i]);

There is nothing variable here. 这里没有任何变量。 You are initializing the array by passing the size to a method that creates an array of the requested size. 您正在通过将大小传递给创建所需大小的数组的方法来初始化数组。 In other words, you cannot manipulate the size of the array after initialization, unlike with ArrayList where you can vary the size. 换句话说,您无法在初始化后操纵数组的大小,这与ArrayList不同,在后者中您可以更改大小。

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

相关问题 为什么数组类型的类变量在声明后不能初始化,但是可以在Java中的方法内部初始化? - Why class variable of type array can't be initialized after declaration, but can be initialized inside a method in Java? 我们可以通过在 Java 中使用变长参数来实现动态数组吗? - Can we achieve a dynamic array by using variable length arguments in Java? 为什么 JAVA 10 中的 var 不能初始化为 null? - Why var in JAVA 10 can not be initialized to null? 为什么Java最终数组变量可以修改? - Why can Java final array variable be modified? 为什么我不能在未初始化的局部变量上放置断点? - Why I can't put a breakpoint on a local variable that is not initialized? Java变量数组长度 - Java Variable array length 为什么可以在Java中将超类初始化为子类? - Why can a super class be initialized as a child class in Java? 为什么列表不能用数组初始化,没有arrays.aslist()? - why list can't be initialized with array, without arrays.aslist()? 为什么某些Java数据类型可以在声明中初始化,而另一些则不能呢? - Why can some Java data types be initialized in the declaration and others cannot? 为什么 Object 类型的变量可以包含对 Java 中的数组的引用? - Why can a variable of type Object hold a reference to an array in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM