简体   繁体   English

最大值是多少。 字节数组的容量?

[英]What is the max. capacity of byte-Array?

I made a JavaClass which is making addition, sub, mult.我制作了一个 JavaClass,它可以进行加法、减法、乘法运算。 etc.等等

And the numbers are like (155^199 [+,-, ,/] 555^669 [+,-, ,/]..... [+,-,*,/] x^n);数字就像 (155^199 [+,-, ,/] 555^669 [+,-, ,/]..... [+,-,*,/] x^n);

each number is stored in Byte-Array and byte-Array can contain max.每个数字都存储在字节数组中,字节数组可以包含最大值。 66.442 66.442

example:例子:

(byte) array = [1][0] + [9][0] = [1][0][0] (字节)数组 = [1][0] + [9][0] = [1][0][0]

(byte) array = [9][0] * [9][0] = [1][8][0][0] (字节)数组 = [9][0] * [9][0] = [1][8][0][0]

My Class file is not working if the number is bigger then (example: 999^999)如果数字更大,我的 Class 文件将不起作用(例如:999^999)

How i can solve this problem to make addition between much bigger numbers?我如何解决这个问题以在更大的数字之间进行加法?

When the byte-Array reachs the 66.443 values, VM gives this error:当 byte-Array 达到 66.443 值时,VM 会给出以下错误:

Caused by: java.lang.ClassNotFoundException .原因: java.lang.ClassNotFoundException which is actually not the correct error-description.这实际上不是正确的错误描述。

well it means, if i have a byte-array with 66.443 values, the class cannot read correctly.这意味着,如果我有一个包含 66.443 个值的字节数组,则 class 无法正确读取。

Solved: Used multidimensional-Byte Array to solve this problem.解决:使用多维字节数组来解决此问题。

array{array, ... nth-array} [+, -, /] nth-array{array, ... nth-array}数组{数组, ... nth-array} [+, -, /] nth-array{数组, ... nth-array}

only few seconds to make an addition between big numbers.只需几秒钟即可在大数字之间进行加法。

Thank you!谢谢!

A single method in Java is limited to 64KB of byte code. Java 中的单个方法限制为 64KB 字节码。 When you initialise an array in code it uses byte code to do this.当您在代码中初始化一个数组时,它使用字节码来执行此操作。 This would limit the maximum size you can define an array to about this size.这会将您可以定义的数组的最大大小限制为大约此大小。

If you have a large byte array of value I suggest you store it in an external file and load it at runtime.如果您有一个大字节数组的值,我建议您将其存储在外部文件中并在运行时加载它。 This way you can have a byte array of up to 2 GB.这样您就可以拥有一个最大为 2 GB 的字节数组。 If you need more than this you need to have an array of arrays.如果你需要更多,你需要有一个 arrays 数组。

What does your actual code look like?你的实际代码是什么样的? What error are you getting?你遇到了什么错误?

A Java byte array can hold up to 2^31-1 values, if there is that much contiguous memory available.如果有那么多连续的 memory 可用,则 Java 字节数组最多可以容纳 2^31-1 个值。

Each array can hold a maximum of Integer.MAX_VALUE values.每个数组最多可以保存Integer.MAX_VALUE值。 If it crashes , I guess you see an OutOfMemoryError .如果它崩溃了,我猜你会看到OutOfMemoryError Fix that by starting you java vm with more heap space:通过启动具有更多堆空间的 java vm 来修复该问题:

 java -Xmx1024M  <...>

(example give 1024 MByte heap space) (例如给出 1024 MByte 的堆空间)


java.lang.ClassNotFoundException is thrown if the virtual machine needs a class and can't load it - usually because it is not on the class path (sometimes the case when we simply forget to compile a java source file..). java.lang.ClassNotFoundException is thrown if the virtual machine needs a class and can't load it - usually because it is not on the class path (sometimes the case when we simply forget to compile a java source file..). This exception is totally unrelated to java array operations.此异常与 java 数组操作完全无关

To continue the discussion in the comments section:要在评论部分继续讨论:

The name of the missing class is very important.缺少的 class 的名称非常重要。 At the line of code, where the exception is thrown, the VM tries to load the class ClassBigMath for the very first time and fails.在引发异常的代码行,VM 首次尝试加载 class ClassBigMath并失败。 The classloader can't find a file ClassBigMath.class on the classpath.类加载器在类路径上找不到文件ClassBigMath.class

Double check first if the compiled java file is really present and double check that you don't have a typo in your source code.首先仔细检查编译的 java 文件是否真的存在,并仔细检查你的源代码中没有错字。 Typical reasons for this error:此错误的典型原因:

  • We simply forget to compile a source file我们只是忘记编译源文件
  • A class file is on the classpath at compilation time but not at execution time class 文件在编译时位于类路径中,但在执行时不在
  • We do a Class.forName("MyClass") and have a typo in the class name我们执行Class.forName("MyClass")并在 class 名称中有错字

java.math.BigInteger is much better solution to handle large number. java.math.BigInteger是处理大量数字的更好解决方案。 Is there any reason, you have choosed byte array?有什么原因,你选择了字节数组吗?

The maximum size of an array in Java is given by Integer.MAX_VALUE . Java 中数组的最大大小由Integer.MAX_VALUE给出。 This is 2^31-1 elements.这是 2^31-1 个元素。 You might get OOM exceptions for less if there is not enough memory free.如果没有足够的 memory 免费,您可能会以更少的价格获得 OOM 异常。 Besides that, for what you are doing you might want to look at theBigInteger class.除此之外,对于您正在做的事情,您可能需要查看BigInteger class。 It seems you are doing your math in some form of decimal representation, which is not very memory efficient.看来您正在以某种形式的十进制表示进行数学运算,这不是很有效 memory 。

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

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