java 如何在java中创建一个字节数组数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25652483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:24:11  来源:igfitidea点击:

How to create an array of byte arrays in java

javaarraysbyte

提问by Alex

I want to create an array of byte[]. When i try:

我想创建一个byte[]. 当我尝试:

byte[] data = new byte[10];

This is an array of byteand not an array of byte[]. Thanks

这是 的数组byte而不是 的数组byte[]。谢谢

回答by Alnitak

In Java, an array where each element is itself an array is effectively just a 2D array, although Java doesn't require that each row have the same number of elements.

在 Java 中,每个元素本身就是一个数组的数组实际上只是一个二维数组,尽管 Java 不要求每一行具有相同数量的元素。

In this particular case where you have a List<byte[]>and you wish to turn that into an array where each element of the array is itself a bytearray you could use:

在这种特殊情况下,您有 aList<byte[]>并且您希望将其转换为一个数组,其中数组的每个元素本身就是一个byte您可以使用的数组:

int rows = myList.size();
byte[][] myArray = new byte[rows][];

int i = 0;
for (byte[] col : myList) {
    myArray[i++] = col;
}

or, more succinctly, but less obviously:

或者,更简洁但不那么明显:

byte[][] myArray = myList.toArray(new byte[0][]);

which in use looks like:

使用中的样子:

List<byte[]> myList = new ArrayList<>();
myList.add(new byte[]{1,2,3,4});
myList.add(new byte[]{5,6,7});
myList.add(new byte[]{8,9});

byte[][] myArray = myList.toArray(new byte[0][]);

for (byte[] b : myArray) {
    for (byte c : b) {
        System.out.println(c);
    }
    System.out.println();
}

Note that in both cases above the elements of the array are exactlythe same arrays as those stored in the list, not a copy. Therefore if you modify one of the original byte[]elements in the list it'll also change the version accessible via the array.

请注意,在上述两种情况下,数组元素与存储在列表中的数组元素完全相同,而不是副本。因此,如果您修改byte[]列表中的原始元素之一,它也会更改可通过数组访问的版本。

回答by ajb

To address some confusion about "arrays of arrays" and 2D arrays:

为了解决关于“数组数组”和二维数组的一些混淆:

Java doesn't really have 2D arrays. At least, not in the way many people think of them, where they look like a matrix, and you have rows and columns and the same number of elements in each row.

Java 并没有真正的二维数组。至少,不像很多人想象的那样,它们看起来像一个矩阵,你有行和列,每行中有相同数量的元素。

You canuse an "array of arrays" as a 2D array. If you want an array of 5 x 8 integers, you can say

可以将“数组数组”用作二维数组。如果你想要一个 5 x 8 整数的数组,你可以说

int[][] arr = new int[5][8];

But Java does not enforce the "matrix-ness" or "2D-ness" of an array. What it gives you is an array of 5 references, each of which refers to another array of integers. Each reference starts out referring to an array of 8 integers, but this is not enforced; you could later say

但是 Java 并没有强制执行数组的“矩阵性”或“二维性”。它给你的是一个包含 5 个引用的数组,每个引用都指向另一个整数数组。每个引用开始时引用一个 8 个整数的数组,但这不是强制执行的;你以后可以说

arr[2] = new int[113];

or

或者

arr[2] = null;

and now your array is no longer really a 2D array (as we usually think of it), but Java will not stop you from doing that.

现在你的数组不再是一个真正的二维数组(正如我们通常认为的那样),但 Java 不会阻止你这样做。

So while you may often see something like

所以虽然你可能经常看到类似的东西

byte[][] 

used in textbooks to create a 2D array, it's the same syntax as an array of arrays, and Java actually treats it as an array of arrays, not all of which have to be the same size.

教科书上用来创建二维数组的,它和数组数组的语法是一样的,Java实际上是把它当作数组的数组,并不是所有的数组都必须是相同的大小。

If you want to create an array whose elements will eventually bearrays, but that are nullfor now, you can do it like this:

如果你想创建一个数组,它的元素最终将是数组,但null现在是这样,你可以这样做:

byte[][] bytesArray = new byte[10][];

which sets bytesArrayto an array of 10 nullreferences, any one of which could be set later:

它设置bytesArray为一个包含 10 个null引用的数组,其中任何一个都可以稍后设置:

bytesArray[4] = new byte[17];

That would be the way to start, if you want a "ragged array" of arrays that aren't all the same length.

如果您想要一个长度不同的数组的“参差不齐的数组”,那将是开始的方式。

回答by Samuele Panarotto

Try this:

试试这个:

byte[][] arrays = new byte[5][10];

字节[][]数组=新字节[5][10];

回答by RockOnRockOut

You mean like this?

你的意思是这样?

byte[][] data = new byte[][]{byteArray1, byteArray2, byteArray3};

回答by Lawrence Andrews

In your example, you are creating an array of bytes, if you wanted to create an array of arrays of bytes you would have to create a two dimensional array of bytes:

在您的示例中,您正在创建一个字节数组,如果您想创建一个字节数组数组,则必须创建一个二维字节数组:

byte[][] bytesArray = new byte[10][10];

byte[][] bytesArray = new byte[10][10];

You would then be able to access your array of bytes as follows:

然后,您将能够访问您的字节数组,如下所示:

bytesArray[0]; // returns a byte[]

bytesArray[0]; // returns a byte[]