Java 字节数组是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4019837/
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
What do we mean by Byte array?
提问by daydreamer
Could someone please explain, I do not exactly get the concept.
有人可以解释一下,我不完全明白这个概念。
What is a Byte Array?
什么是字节数组?
Where and when we use it in applications/programs?
我们何时何地在应用程序/程序中使用它?
what are the advantages and disadvantages of using a byte array?
使用字节数组的优缺点是什么?
采纳答案by Phil
A byte is 8 bits (binary data).
一个字节是 8 位(二进制数据)。
A byte array is an array of bytes (tautology FTW!).
字节数组是字节数组(重言式 FTW!)。
You could use a byte array to store a collection of binary data, for example, the contents of a file. The downside to this is that the entire file contents must be loaded into memory.
您可以使用字节数组来存储二进制数据的集合,例如文件的内容。这样做的缺点是必须将整个文件内容加载到内存中。
For large amounts of binary data, it would be better to use a streaming data type if your language supports it.
对于大量二进制数据,如果您的语言支持,最好使用流数据类型。
回答by Miguel Sevilla
From wikipedia:
来自维基百科:
In computer science, an array data structure or simply array is a data structure consisting of a collection of elements (values or variables), each identified by one or more integer indices, stored so that the address of each element can be computed from its index tuple by a simple mathematical formula.
在计算机科学中,数组数据结构或简单的数组是由一组元素(值或变量)组成的数据结构,每个元素由一个或多个整数索引标识,存储以便可以从其索引计算每个元素的地址元组由一个简单的数学公式。
So when you say byte array, you're referring to an array of some defined length (e.g. number of elements) that contains a collection of byte (8 bits) sized elements.
因此,当您说字节数组时,您指的是某个定义长度(例如元素数量)的数组,其中包含一组字节(8 位)大小的元素。
In C# a byte array could look like:
在 C# 中,字节数组可能如下所示:
byte[] bytes = { 3, 10, 8, 25 };
The sample above defines an array of 4 elements, where each element can be up to a Bytein length.
上面的示例定义了一个包含 4 个元素的数组,其中每个元素的长度最多为Byte。
回答by user433534
I assume you know what a byte is. A byte array is simply an area of memory containing a group of contiguous (side by side) bytes, such that it makes sense to talk about them in order: the first byte, the second byte etc..
我假设你知道字节是什么。字节数组只是包含一组连续(并排)字节的内存区域,因此按顺序讨论它们是有意义的:第一个字节,第二个字节等。
Just as bytes can encode different types and ranges of data (numbers from 0 to 255, numbers from -128 to 127, single characters using ASCII e.g. 'a' or '%', CPU op-codes), each byte in a byte array may be any of these things, or contribute to some multi-byte values such as numbers with larger range (e.g. 16-bit unsigned int from 0..65535), international character sets, textual strings ("hello"), or part/all of a compiled computer programs.
正如字节可以编码不同类型和范围的数据(从 0 到 255 的数字,从 -128 到 127 的数字,使用 ASCII 的单个字符,例如“a”或“%”,CPU 操作码),字节数组中的每个字节可能是这些东西中的任何一个,或者有助于一些多字节值,例如具有更大范围的数字(例如,来自 0..65535 的 16 位无符号整数)、国际字符集、文本字符串(“hello”)或部分/所有编译好的计算机程序。
The crucial thing about a byte array is that it gives indexed (fast), precise, raw access to each 8-bit value being stored in that part of memory, and you can operate on those bytes to control every single bit. The bad thing is the computer just treats every entry as an independent 8-bit number - which may be what your program is dealing with, or you may prefer some powerful data-type such as a string that keeps track of its own length and grows as necessary, or a floating point number that lets you store say 3.14 without thinking about the bit-wise representation. As a data type, it is inefficient to insert or remove data near the start of a long array, as all the subsequent elements need to be shuffled to make or fill the gap created/required.
字节数组的关键在于它提供对存储在该部分内存中的每个 8 位值的索引(快速)、精确、原始访问,并且您可以对这些字节进行操作以控制每一位。不好的是,计算机只是将每个条目视为一个独立的 8 位数字——这可能是您的程序正在处理的,或者您可能更喜欢一些强大的数据类型,例如跟踪其自身长度并不断增长的字符串必要时,或一个浮点数,让您可以存储 3.14 而不考虑按位表示。作为一种数据类型,在长数组的开头附近插入或删除数据是低效的,因为需要对所有后续元素进行混洗以形成或填充创建/需要的间隙。