0

I have a arraylist of Bytes and i am converting them into a byte array.I have used the following method.However it gives me the following error: E/AndroidRuntime(5228): java.lang.NoClassDefFoundError: com.google.common.primitives.Bytes

ArrayList<Byte> byteArrayList_song=new ArrayList<Byte>();
byte[] bytes_song_byte;

 for(int i=0;i<int_arraylist.size();i++)
 {
            bytes_song_byte=Bytes.toArray(byteArrayList_song);                
 }

3 Answers 3

2

Looks like Google Guava is not on your class path, also you should remove the for loop from the above code, as that is what the Guava function does for you.

ArrayList<Byte> byteArrayList_song = new ArrayList<Byte>();
byte[] bytes_song_byte = Bytes.toArray(byteArrayList_song);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks dicarlo2 But i have add the library as a external jar file into my project.Bulid Path->Configure Build Path->Add External Jars.Do i need to make any more changes?
1

You can do this conversion without external libs

    byte[] bytes_song_byte = new byte[byteArrayList_song.size()];
    for (int i = 0; i < byteArrayList_song.size(); i++) {
        bytes_song_byte[i] = byteArrayList_song.get(i);
    }

note that if byteArrayList_song has any null elements this code will throw a NullPointerException

4 Comments

Indeed you could but, the purpose of all of Guava's convenience functions like Bytes.toArray is exactly that, for convenience. No reason to type the above when it can be wrapped up in a function for you.
With that said, would I include an entire external library just to use a couple of toArray functions? Probably not, I'd just write them myself.
I tried the above code but when i try print the size of the bytes_song_byte it gives me null
You probably mean 0. Your ArrayList is probably empty, add some values: byteArrayList_song.add(true); byteArrayList_song.add(false);
-1

Try the following

      ArrayList<Byte> byteArrayList_song=new ArrayList<Byte>();
        byte[] bytes_song_byte;
        bytes_song_byte=byteArrayList_song.toArray(new Byte[byteArrayList_song.size()]);                

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.