Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 795599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:30:22+00:00 2026-05-14T22:30:22+00:00

I have a vector of float arrays i.e. Vector . I want to convert

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:30:23+00:00Added an answer on May 14, 2026 at 10:30 pm

    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

    • How to flatten 2D array to 1D array?

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

    See also

    • ArrayList vs. Vectors in Java if thread safety isn’t a concern
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the vector d<-1:100 I want to sample k=3 times from this vector
I have the following case: boost::ptr_vector<float> vec; float* array = new float[4](); vec.push_back(array); //
I have a c++ vector: vector<float> floats; Later, this vector is initialized. I don't
I have a struct that looks like this: typedef struct _my_struct { float first_vector[SOME_NUM][OTHER_NUM];
I have a boost::unordered_multimap< std::vector<int>, float> . The keys that I use to query
I have vector< pair<int, int>> myVec (N); I want to have all pairs initialized
I have a vector where I keep an incrementing data. Normally each element of
I have the vector output = PV_out(:); I am trying to break this down
I have an array of arrays P, which represents a Matrix, as an array
I have an std::vector defined as: std::vector<glm::vec3> faces; And I want to use the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.