1

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?

Thanks in advance!

1
  • 1
    And how do you fetch the value of mFFTBytes. It's no wonder that you get null pointer exception. Commented Nov 15, 2014 at 13:19

2 Answers 2

7

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I am now getting an error on the for(byte b : mFFTBytes){ line - it is a nullpointerexception - do you know what could be causing this
That's because you didn't initialize mFFTBytes. You need to do : public byte[] mFFTBytes = new byte[size];
0

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

        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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.