views:

253

answers:

2

I have a vector of float arrays i.e. Vector . I want to convert this to one float array i.e. move every element in every float[] within the vector to a new float[]. Am a bit puzzled on using the Java built in method vector.toArray() to do this. Any ideas pls?

+1  A: 

There are several ways to do this. You can invoke toArray() to get several arrays, and then join them into one big array (Guava has utility method to do this). Or you can allocate one big array and then use System.arraycopy to copy the components array into the appropriate portion of the big array.

Here's an example of the latter:

import java.util.*;

public class Flatten {
    static float[] flatten(float[]... arrs) {
        int L = 0;
        for (float[] arr : arrs) {
            L += arr.length;
        }
        float[] ret = new float[L];
        int start = 0;
        for (float[] arr : arrs) {
            System.arraycopy(arr, 0, ret, start, arr.length);
            start += arr.length;
        }
        return ret;
    }
    public static void main(String[] args) {
        Vector<float[]> v = new Vector<float[]>();
        v.add(new float[] { 1,2,3, });
        v.add(new float[] { 4, });
        v.add(new float[] {  });
        v.add(new float[] { 5,6, });

        float[] flattened = flatten(v.toArray(new float[0][]));
        System.out.println(Arrays.toString(flattened));
        // prints "[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]"
    }
}

With Guava, it looks like you can do this with one line using Floats.concat(float[]...):

float[] flattened = Floats.concat(v.toArray(new float[0][]));

See also


It should also be noted that unless you need the thread-safety of Vector, you should probably use ArrayList instead.

See also

polygenelubricants
Thanks, I will try this now. "It should also be noted that unless you need the thread-safety of Vector, you should probably use ArrayList instead." Can you explain this please. Currently I have a thread adding to my vector, will what your suggesting affect my implementation?
Afro Genius
@Afro: If there's concurrent access to the `Vector`, then yeah you may want to keep it a `Vector` then. Synchronization can be costly, and it's not always needed, which is why I suggested `ArrayList`. It's like `StringBuilder` vs `StringBuffer` issue.
polygenelubricants
@polygenelubricants: Aha now I get you. I'll give you the bigger picture of what am trying to do which is going horribly wrong at the moment. The vector of arrays are actually arrays of sound bytes which instead of writing to the underlying system audio, I divert into this vector. When its finished, I read in the vector and convert these arrays of bytes into arrays of floats as my application processes sound as floats. When I do this and try to feed my application the floats to process all I get back is static sound not the audio expected.
Afro Genius
I can only imagine this has to do with not processing the data as frames? Have you any idea how to convert this raw data in sound frames?
Afro Genius
@Afro: that's a different question entirely, and no, I don't know the specifics of audio processing, sorry. If this particular question is satisfiably answered, you can accept it (click on check mark), and you can ask another question for the audio data stuff. Title it clearly what you need help with, and I'm sure others more qualified can help you.
polygenelubricants
Thanx allot. It did work for me.
Afro Genius
@Afro: you're welcome! I'm glad to have helped with this question, and I'm sorry I couldn't help with the bigger picture stuff, but I wish you the best of luck in finding the answer you're looking for.
polygenelubricants
A: 

Vector.toArray() will give you an array of arrays, i.e a two dimensional array. once you have that, you can iterate the array till the length and copy each individual array into one big array.

Works for you?

Vaishak Suresh