使用 long 变量作为其大小创建长数组时对错误感到困惑 [英] Confused of the error when creating a long array using a long variable as its size

查看:70
本文介绍了使用 long 变量作为其大小创建长数组时对错误感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序需要处理非常大的数字作为输入,所以我选择了long.当我使用 long 类型的变量作为其大小创建 long 类型的 array 时出现错误.有人可以提供一些关于这里出了什么问题的见解吗?

My program needs to handle very big numbers as input, so I chose long. I have a error when I create an array of type long using a variable of type long as its size. Could someone please provide some insight about what went wrong here?

错误:

long[] Arr = new long[n];
                      ^    //incompatible types: possible lossy conversion from long to int

代码:

private static long foo(long n, long m) {
  if (n <= 1) return n;
  long[] Arr = new long[n];
  return 0;
}

推荐答案

看起来 n 是 Long 类型的.但是一个Array的长度只能是一个int

Looks like n is of type Long. But the length of an Array can only be an int

这篇关于使用 long 变量作为其大小创建长数组时对错误感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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