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

  • SEARCH
  • Home
  • 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 6693457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:59:05+00:00 2026-05-26T05:59:05+00:00

I wrote a data model, into one class I need to merge 3 arrays.

  • 0

I wrote a data model, into one class I need to merge 3 arrays. These arrays has a fixed size (of course) and initialized to null. I use this code :

public static <T> T[] merge(T[]... arrays) {
    int size = 0;
    for (T[] array : arrays) {
        size += array.length;
    }

    T[] merged = (T[]) Array.newInstance(arrays[0][0].getClass(), size);

    int start = 0;
    for (T[] array : arrays) {
        System.arraycopy(array, 0,
           merged, start, array.length);
        start += array.length;
    }
    return (T[]) merged;
}

This line is not correct :

T[] merged = (T[]) Array.newInstance(arrays[0][0].getClass(), size);

I need to precise which class I use, but I get a null pointer !

  • 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-26T05:59:05+00:00Added an answer on May 26, 2026 at 5:59 am

    I would leverage the Collections library

    public static <T> T[] merge(T[]... arrays) {    
        List<T> list = new LinkedList<T>();
        for(T[] array : arrays) {
            for(T t : array) {
                list.add(t);
            }
        }
        return (T[])(list.toArray());    
    }
    

    Test program:

    import java.util.*;
    class Merger {
        // break out total length logic. Will probably be inlined, but this seems like
        // functionality that deserves its own method.
        public static <T> int totalLength(T[]... arrays) {
            int length = 0;
            for(T[] arr : arrays) length += arr.length;
            return length;
        }
    
        public static <T> T[] merge(T[]... arrays) {
            int length = totalLength(arrays);
            if(length == 0) return (T[])new Object[0];
            List<T> list = new ArrayList<T>(length);
            for(T[] array : arrays) {
                for(T t : array) {
                    list.add(t);
                }
            }
            return (T[])(list.toArray(arrays[0])); 
        }
    
        public static void main(String[] args) {
            String[] first = new String[] { "This", "is", "a", "test" };
            String[] second = new String[] { "of", "the", "merger" };
            String[] third = new String[] { "and", "it", "works!" };
    
            String[] merged = merge(first,second,third);
            for(String s : merged) System.out.println(s);
    
            // validate that it works for merging all 0 arrays:
            merge();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Our data model is separated into schemas on two databases. The schemas are used
I wrote the below method to archive files into one file using binary mode:
I recently wrote some data access methods (plain old Java) that use immutable objects
I wrote a windows service using VB that read some legacy data from Visual
I wrote a Java program to add and retrieve data from an MS Access.
i wrote a java app that communicates and stores data in oracle; my question
i have wrote a script to produce an array of data but now want
Data comes in by email as a zipped file. The Java solution we wrote
I wrote a handler (javax.xml.rpc.handler.Handler) for a SOAP web service that inspects header data.
In an Open Source program I wrote , I'm reading binary data (written by

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.