2015-10-07 41 views
3

我想分配一個直接 IntBuffer在Java中,比如說有十億個元素(64位系統)。我知道的唯一方法是創建一個直接的ByteBuffer並將其視爲直接的IntBuffer。但是,4 * 1,000,000,000超出Integer.MAX_VALUE,所以我的問題是:我如何實現目標?Java:直接IntBuffer的大小限制?

int numInts = 1_000_000_000; 
IntBuffer i = IntBuffer.allocate(numInts); // works! 

ByteBuffer bb = ByteBuffer.allocateDirect(4*numInts); // does NOT work: integer overflow 
IntBuffer ib = bb.asIntBuffer(); 
System.out.println("This will never be printed"); 

非常感謝提前,

馬庫斯

+1

如何創建一個二維緩衝區數組? – nafas

+2

[java創建字節數組,其大小用長表示]的可能重複(http://stackoverflow.com/questions/1071858/java-creating-byte-array-whose-size-is-represented-by-a-長) –

+1

這不是IntBuffer的限制,它是一個java限制。 java中的數組長度不能大於2^31。 –

回答

0

也許你可以嘗試基於一個BigInteger,而不是建立一個緩衝區。有些人在Github上創建了一個'BigIntbuffer'類,它可以激勵你: See here