如何在java中创建一个大数组 [英] How to make a big array in java

查看:798
本文介绍了如何在java中创建一个大数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个用户将输入的大小的布尔数组。例如 - 用户可能会输入一个大数字,如1000000000000;所以我必须创建一个大小为1000000000000的布尔数组。我面临的问题是,我不能将输入存储为int,因为它无法容纳如此大的数字 - 因此我无法创建数组.Double是一个选项。我可以将输入数字存储为double,但我不知道如何创建双数字大小的数组。这就是想法 -

I want to create a boolean array of a size which the user is going to put as input.For example - The user might put a big number like 1000000000000 ; so then I have to create a boolean array of the size 1000000000000.The problem I am facing is , I cant store the input as int , as it cant hold such a big number - thus I am unable to create the array .Double is an option .I can store the input number as double , but I don't know how to create the array of the size of the double number .This was the idea -

Scanner scanner = new Scanner(System.in);
int target = scanner.nextInt();
boolean [] array_a=new boolean [(target)];

如果目标超过int范围,它将无法工作。感谢任何帮助。

which wont work if target exceeds the int range.Any help is appreciated.

更新:谢谢大家。所以你只能创建一个int最大范围(2147483648)大小的数组,对吧?内存方面没有打到我早些时候。采取不同的方法。

Update : Thanks guys .So you can only create an array of the size of int's max range (which is 2147483648),right?The memory aspect didn't hit me earlier. Going to take a different approach .

推荐答案


必须创建一个大小为1000000000000的布尔数组。我面临的问题是,我不能将输入存储为int

have to create a boolean array of the size 1000000000000. The problem I am facing is , I cant store the input as int

你的问题不是那个。您的主要问题是您将没有足够的内存来分配具有1,000,000,000,000个元素的数据结构(即使您克服了 int 索引的限制)。

Your problem isn't that. Your main problem is that you won't have enough memory to allocate a data structure with 1,000,000,000,000 elements (even if you overcame the limitations of int indexing).

您需要重新考虑算法。

这篇关于如何在java中创建一个大数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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