循环遍历字节数组 - java

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

Looping through byte array - java

java

提问by James

I would like to try and loop through this following byte array:

我想尝试遍历以下字节数组:

public byte[] mFFTBytes;

How would I go about doing this in java with a for loop?

我将如何使用 for 循环在 Java 中执行此操作?

Thanks in advance!

提前致谢!

回答by Gaktan

First of all you need to instantiate your array because you will have null pointer exceptions all over the place.

首先,您需要实例化您的数组,因为您到处都会有空指针异常。

Then you can use a for each loop

然后你可以为每个循环使用一个

for(byte b : mFFTBytes){
    System.out.println(b);
}

回答by TomS

I am not sure if I understand well what you are asking for. Going through any array in Java is quite trivial action like:

我不确定我是否理解你的要求。在 Java 中遍历任何数组都是非常简单的操作,例如:

        for (byte myByte: mFFTBytes) {
            //do something with myByte
        }

or

或者

        for (int i=0;i<mFFTBytes.length;i++) {
            //do something with mFFTBytes[i]
        }

The latter should be faster, but for a vast majority of applications is does not matter much.

后者应该更快,但对于绝大多数应用程序来说无关紧要。