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 6848693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:54:01+00:00 2026-05-27T00:54:01+00:00

(Title sounds a bit too fancy but I couldn’t find a match so we’ll

  • 0

(Title sounds a bit too fancy but I couldn’t find a match so we’ll hope it’s descriptive.)

I have a working piece of code as follows:

@SuppressWarnings("unchecked")
public static Copyable[] copyOf(Copyable[] objects, Class type) {
    Copyable[] copies = Arrays.copyOf(objects, objects.length, type);
    for( int i=0; i < copies.length; ++i ) {
        if( objects[i] == null )
            throw new IllegalArgumentException("Copyable[] contains null reference", new NullPointerException());
        copies[i] = objects[i].copy();
    }

    return copies;
}

Not as pretty as I would like, since I have to pass in the array class, but it works for what I want it to do: allocate a new array of a Copyable implementor and fill it using the implemented method.

The problem I’m having is that this gets compiled by GWT, whose JRE Emulation library barfs on correct Java that it hasn’t implemented. I need to do this without calling Arrays.copyOf(), and hopefully without reflection.

Note: I’m working on a clone() version but don’t really want to rely on clone() either. I feel like there must be a cleaner solution.

  • 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-27T00:54:01+00:00Added an answer on May 27, 2026 at 12:54 am

    How about this:

    public static Copyable[] copyOf(Copyable[] objects) {
        Copyable[] copies = new Copyable[objects.length];
        for( int i=0; i < copies.length; ++i ) {
            if( objects[i] == null )
                throw new IllegalArgumentException("Copyable[] contains null reference", new NullPointerException());
            copies[i] = objects[i].copy();
        }
        return copies;
    }
    

    There’s no need to actually copy the objects array into copies before making copies of the elements themselves.

    EDIT

    If your Copyable objects are serializable, you can just serialize and then deserialize the array to create a copy. There’s a utility function DeepCopy.java in gwt-test-utils that may do exactly what you need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The title sounds confusing.. but im sure this is possible! I have one button,
Title sounds a bit weird, but basically I'm wondering how I should go about
This sounds like a weird title and probably not stated too well. But here's
The title sounds a bit crazy but having the * function for java.lang.String in
The title sounds a bit odd, but what I am trying to do is
Sorry if the title sounds a bit vague. Stackoverflow posting newbie here. I have
Sorry if the title sounds confusing - but this is what I am trying
hmm, I feel my OP title sounds wrong, but I am not sure how
I know the title of the question sounds absolutely weird but I had no
I know the title makes this sound very easy, but I have a For

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.