java 在android中调整字节数组的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10595064/
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-10-31 01:44:59  来源:igfitidea点击:

resize a byte array in android

javaandroid

提问by Riskhan

I am newbie in android. I want to resize a byte array in function. Is it possible or not. If any problem please suggest a solution to do it.

我是安卓新手。我想在函数中调整一个字节数组的大小。有没有可能。如果有任何问题,请提出解决方案。

public void myfunction(){
    byte[] bytes = new byte[1024];
    ....................
    .... do some operations........
    ................................
    byte[] bytes = new byte[2024];
}

采纳答案by UVM

You can do it like this:

你可以这样做:

bytes = new byte[2024];

But your old content will be discarded.If you need the old data as well, then need to create a new byte array with a diff size and call System.arrayCopy()method to copy data from old to new.

但是你的旧内容会被丢弃。如果你还需要旧数据,那么需要创建一个具有差异大小的新字节数组,并调用System.arrayCopy()方法将数据从旧数据复制到新数据。

回答by Hartmut Pfitzinger

To achieve the effect of resizing a byte array without loosing the content, there are several solutions in Java already mentioned:

为了达到在不丢失内容的情况下调整字节数组大小的效果,Java中已经提到了几种解决方案:

1)ArrayList<Byte>(see the answers of a.ch. and kgiannakakis)

1)ArrayList<Byte>(见a.ch.和kgiannakakis的回答)

2)System.arraycopy()(see the answers of jimpic, kgiannakakis, and UVM)

2)System.arraycopy()(见jimpic、kgiannakakis、UVM的回答)

Something like:

就像是:

byte[] bytes = new byte[1024];
//
// Do some operations with array bytes
//
byte[] bytes2 = new byte[2024];
System.arraycopy(bytes,0,bytes2,0,bytes.length);
//
// Do some further operations with array bytes2 which contains
// the same first 1024 bytes as array bytes

3)I would like to add a third way which I think is most elegant: Arrays.copyOfRange()

3)我想添加第三种我认为最优雅的方式:Arrays.copyOfRange()

byte[] bytes = new byte[1024];
//
// Do some operations with array bytes
//
bytes = Arrays.copyOfRange(bytes,0,2024);
//
// Do some further operations with array bytes whose first 1024 bytes
// didn't change and whose remaining bytes are padded with 0

Of course there are further solutions (e.g. copying the bytes in a loop). Concerning efficiency, see this

当然还有进一步的解决方案(例如在循环中复制字节)。关于效率,看这个

回答by kgiannakakis

You can't resize an array in Java. You could use a Listto do what you want:

您不能在 Java 中调整数组的大小。你可以使用List来做你想做的事:

List<Byte> bytes = new ArrayList<Byte>();

Another way will be to use System.arraycopy. In that case you will create a second array and copy the content of the first array into it.

另一种方法是使用System.arraycopy。在这种情况下,您将创建第二个数组并将第一个数组的内容复制到其中。

回答by jimpic

You cannot do that. But you can create a new array and use System.arraycopy to copy the old contents to the new one.

你不能这样做。但是您可以创建一个新数组并使用 System.arraycopy 将旧内容复制到新内容。

回答by a.ch.

Use ArrayList<Byte>instead, Java arrays don't allow resizing, ArrayLists do, but make it a little bit different. You can't specify their size (actually you needn't - it is done automatically), you may specify initial capacity (which is good practice if you know how many elements will the ArrayListcontain):

使用ArrayList<Byte>代替,Java 数组不允许调整大小,ArrayLists 允许,但让它有点不同。您无法指定它们的大小(实际上您不需要 - 它会自动完成),您可以指定初始容量(如果您知道将ArrayList包含多少个元素,这是一种很好的做法):

byte myByte = 0;
ArrayList<Byte> bytes = new ArrayList<Byte>(); //size is 0
ArrayList<Byte> bytes2 = new ArrayList<Byte>(1024); //initial capacity specified
bytes.add(myByte); //size is 1
...

I'd recommend you to look through this Java Collections tutorial.

我建议您阅读此Java 集合教程