Java中如何处理大数组

发布于 2024-12-17 18:09:47 字数 227 浏览 0 评论 0原文

我正在读取一个包含 10,000 个 int 值的文件,然后尝试将它们存储在数组中。抛出异常,表示数组值太大。

我想知道,我是否可以将其保存在内存中并从那里读取,而不是将其写入变量中。这是解决这个问题的合适方法吗?

编辑:

经过更多检查,抛出的错误似乎是“try 语句的代码过大”错误。我正在读取每个数组元素并将其附加到字符串中,也许这就是导致错误的原因?

I am reading a file that has 10,000 int values and then trying to store these in an array. There is an exception thrown which says that the array value is too large.

I was wondering, rather than write this array out in to a variable, could i possibly just keep it in memory and read it from there. Would this be a suitable way of solving this problem?

edit:

After more examination it appears that the error being thrown is a "code to large for try statement" error. I am reading each array element and appending it to a string, maybe this is what is causing the error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

半城柳色半声笛 2024-12-24 18:09:48

奇怪的是你不能创建 10000 个元素的长数组。我相信你的问题不是数组长度而是特定数组元素的值。无论如何,如果您需要更大的数组,请使用列表。具体来说是java.util.LinkedList。

It is strange that you cannot create 10000 elements long array. I believe that your problem is not the array length but the value of particular array element. Anyway if you need bigger arrays use Lists instead. Specifically java.util.LinkedList.

岁月苍老的讽刺 2024-12-24 18:09:48

您的问题是您正在完整地编写每个数组或字符串赋值,如下所示:

        array[0] = 0;
        array[1] = 1;
        array[2] = 2;
        // all the way up to 9999!

或这样:

        String s = "";
        s += array[0];
        s += array[1];
        s += array[2];
        // all the way up to 9999!

而不是在循环中。因此,您生成的代码比 Java 在方法中允许的代码多

这会导致编译错误,正如您所描述的:

$ javac Test.java
Test.java:7: code too large for try statement
        try {
            ^
Test.java:4: code too large
    public static void main(String[] args) {
                       ^
2 errors

根据注释中的讨论,您所说的产生此编译器错误的代码没有大量行。有些事情没有意义 - 您报告的错误与您所说的导致该错误的代码不符。在这个后期阶段,我强烈建议您发布一些代码和错误,以便其他人可以尝试了解可能导致此问题的原因。

(此外,您的问题不太可能引起太多关注,因为您已经接受了答案。如果您的问题实际上没有得到解答,您可能需要重新考虑。)

Your problem is that you are writing each array or String assignment out in full, something like this:

        array[0] = 0;
        array[1] = 1;
        array[2] = 2;
        // all the way up to 9999!

or this:

        String s = "";
        s += array[0];
        s += array[1];
        s += array[2];
        // all the way up to 9999!

instead of in a loop. So you generate more code than Java will allow in a method.

This results in a compilation error as you describe:

$ javac Test.java
Test.java:7: code too large for try statement
        try {
            ^
Test.java:4: code too large
    public static void main(String[] args) {
                       ^
2 errors

Following from discussion in comments, the code that you say is producing this compiler error does not have an enormous number of lines. Something doesn't make sense - the error you report does not line up with the code you say is causing it. At this late stage I strongly recommend that you post some code, and the error so that others can try to understand what might be causing this.

(Also, your question isn't likely to get much attention because you have accepted an answer. You might want to reconsider that if your question is not in fact answered.)

§对你不离不弃 2024-12-24 18:09:48

10,000 个整数的数组根本不是很大。我不明白为什么你会在将数据保存在内存中时遇到问题(即分配给变量)。

An array of 10,000 ints isn't very big at all. I can't think why you would have a problem keeping the data in memory (ie assigned to a variable).

暗地喜欢 2024-12-24 18:09:48

我觉得很奇怪,10,000 个整数占用了太多内存。如果耗尽你的记忆,可能是其他东西。您是否尝试过增加 Java 的可用内存? (即-Xmx512m)。如果这是不可能的,如果数字足够小,您始终可以尝试使用短整型或字节。

数组将占用与内存块一样多的空间(就像 c 那样)。

I find it odd that 10,000 ints takes up too much memory. It could be that other stuff if eating up your memory. Have you tried increasing the available memory to Java? (i.e.-Xmx512m). If this is not possible, you can always try to use shorts or bytes if the numbers are small enough.

The array will take just as much space as chunk of memory (like c does).

残龙傲雪 2024-12-24 18:09:48

这是 JVM 中的一个已知错误。它禁止您创建大小为 10,000 的整数数组(在 Mac OS X 上也是 16,384)。它与 Java 将字节码转换为机器码的方式有关。将数组大小更改为 10,001 即可解决问题。

This is a known bug in the JVM. It prohibits you from creating an array of integers with size 10,000 (and also 16,384 on Mac OS X). It has to do with the way in which Java translates the byte code into machine code. Changing the size of the array to 10,001 will solve the problem.

怀中猫帐中妖 2024-12-24 18:09:47

您可以使用 ArrayList 来代替 - 但数组应该可以容纳 10,000 个值。你能发布更多细节吗?代码、完整堆栈跟踪等。理论上,Integer.MAX_VALUE 元素应该没问题(超过 10k),但当然,您可能首先会耗尽内存!

就“将其保存在内存中并从那里读取”而言,变量只是保存在内存中,因此无论您使用数组还是列表(或任何其他数据结构),您都将始终从内存中读取它!

编辑:根据您的附加解释,那么这根本不是数组大小的问题,而是您生成 10,000 行代码放入单个块中的问题,这太多了,因此它会抱怨。更改您的代码以生成使用循环的代码,一切都应该很好,无论其中有多少元素(当然最多为 Integer.MAX_VALUE)。

You could use an ArrayList instead - but an array should be fine with 10,000 values. Can you post more detail? Code, full stack trace etc. Theoretically it should be fine with Integer.MAX_VALUE elements (a LOT more than 10k), but of course you may run out of memory first!

In terms of "just keep it in memory and read it from there", well variables are just kept in memory, so whether you use an array or a list (or any other data structure) you'll always be reading it from memory!

EDIT: Based on your additional explanation then it's not a problem with the array size at all, it's a problem with you generating 10,000 lines of code to put in a single block, which is too many and thus it complains. Alter your code to generate code that uses a loop instead and all should be well, however many elements you have in there (up to Integer.MAX_VALUE of course.)

熟人话多 2024-12-24 18:09:47

包含 10,000 个 int 值的数组大约为 40KB。

您可以尝试进一步减少使用的内存,但我怀疑这不是您的问题。

您能给我们实际的错误消息吗?仅当数组值是 long 时,数组值才太大,例如,假设您使用 File.length()/4 来确定数组的大小,在这种情况下,您需要将其转换为 int

An array of 10,000 int values is about 40KB.

You could try to reduce the memory used further however I suspect this is not your problem.

Can you give us the actual error message? An array value is only too large if its a long e.g. say you used File.length()/4 to determine the size of the array, in which case you need to cast it to an int

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文