简体   繁体   English

为什么这段代码在我的netbeans中显示错误?

[英]Why this code showing error in my netbeans?

I use this code in my class. 我在课堂上使用此代码。 but its shows the error. 但它显示了错误。 why we cant assign long value in the String array declaration? 为什么我们不能在String数组声明中分配long值? It showing possible loss converstion. 它显示了可能的损失对话。

long n=10000000;
String ar[]=new String[n];

This is correct declaration. 这是正确的声明。 Use int instead of long . 使用int代替long The Java JVM does not allow creating array in the size, range of long data type, so it is producing the warning (error) at the compile time. Java JVM不允许以long数据类型的大小和范围创建数组,因此它在编译时产生警告(错误)。

int n=10000000;
String ar[]=new String[n];

I tried the following code: 我尝试了以下代码:

public class Array {
  public static void main(String args[]) {
    int a[] = new int[Integer.MAX_VALUE];
  }
}

Got the following exception: Requested array size exceeds VM limit 出现以下异常: Requested array size exceeds VM limit

Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit
    at gen2dArray.main(Array.java:7)

This question is likely the same as this 这个问题可能与此相同

create an array of long 创建一个长数组

It is related to memory allocation. 它与内存分配有关。 But we can use multi dimensional array to handle this. 但是我们可以使用多维数组来处理此问题。

On maximum integer array of string will take a huge amount of Memory. 在最大整数字符串数组上将占用大量内存。

Try use HashMap if you need to use large of array. 如果需要使用大量数组,请尝试使用HashMap。

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

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