gpt4 book ai didi

java - 在java中分配不同的值后,每一行在二维数组中获得相同的值

转载 作者:行者123 更新时间:2023-12-01 17:41:45 26 4
gpt4 key购买 nike

'''

FileChannel channel = new FileInputStream("test.txt").getChannel();
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0,50000);
byte[] data = new byte[102];
byte[][] bufferArray = new byte[(buffer.capacity() / 102)][100];
int i = 0;
while (buffer.hasRemaining()) {
int remaining = data.length;
if (buffer.remaining() < remaining)
remaining = buffer.remaining();
buffer.get(data, 0, remaining);

bufferArray[i++] = data;
System.out.println("Value before " + new String(bufferArray[43777]));
System.out.println("Value before 0 " + new String(bufferArray[0]));
System.out.println("Value before 100 " + new String(bufferArray[100]));
}
System.out.println("Value after 0 " + new String(bufferArray[0]));
System.out.println("Value after 100 " + new String(bufferArray[100]));
System.out.println("Value after 43777 " + new String(bufferArray[43777]));

'''* 前面的值不同,但后面的值都相同*buffer 是从 txt 文件读取的 MappedByteBuffer

最佳答案

将数据数组初始化移至循环内。通过将其保留在代码之外,代码会重复覆盖相同的内存

FileChannel channel = new FileInputStream("test.txt").getChannel();
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0,50000);
byte[] data;
byte[][] bufferArray = new byte[(buffer.capacity() / 102)][100];
int i = 0;
while (buffer.hasRemaining()) {
data = new byte[102];
int remaining = data.length;
if (buffer.remaining() < remaining)
remaining = buffer.remaining();


buffer.get(data, 0, remaining);
bufferArray[i++] = data;
System.out.println("Value before " + new String(bufferArray[43777]));
System.out.println("Value before 0 " + new String(bufferArray[0]));
System.out.println("Value before 100 " + new String(bufferArray[100]));
}
System.out.println("Value after 0 " + new String(bufferArray[0]));
System.out.println("Value after 100 " + new String(bufferArray[100]));
System.out.println("Value after 43777 " + new String(bufferArray[43777]));

关于java - 在java中分配不同的值后,每一行在二维数组中获得相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60942079/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com