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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:23:48+00:00 2026-06-16T13:23:48+00:00

Possible Duplicate: Creating generic arrays in Java I want to trim a generic array

  • 0

Possible Duplicate:
Creating generic arrays in Java

I want to trim a generic array of objects down to only the first len elements. This seems like it should work:

@SuppressWarnings("unchecked")
public static <T> T[] trimArray(T[] data, int len){
    T[] result = (T[]) new Object[len];
    System.arraycopy(data, 0, result, 0, len);
    return result;
}

But it throws a ClassCastException if I do something like

public class Foo{

    double f;

    public Foo(double f){
    this.f = f;
    }

}

public static void main(String[] args){

    Foo[] A = new Foo[10];
    A[0]= new Foo(1);
    A[1]= new Foo(2);
    A[2]= new Foo(3);
    Foo[] B = trimArray(A, 3);

}

I don’t understand why this won’t work, but something similar does in java generic casting in generic classes

  • 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-06-16T13:23:49+00:00Added an answer on June 16, 2026 at 1:23 pm

    The JDK already has a builtin for this:

    Foo[] B = Arrays.copyOf(A, 3);
    

    Javadoc (since 1.6)

    Note however that it will also expand the array (padded with nulls) if the source array length is less than the wanted length. You can easily work around this by using, for instance:

    Foo[] B = Arrays.copyOf(A, Math.min(3, A.length));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Java - Creating an array of methods In java can i store
Possible Duplicate: Creating an “object” of an interface I am new to Java. Based
Possible Duplicate: Creating an abstract class in Objective C In Java I like to
Possible Duplicate: What is the 'new' keyword in JavaScript? creating objects from JS closure:
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: Creating a simple XML file using python I want to write a
Possible Duplicate: Creating a memory leak with Java There is a Garbage Collector in
Possible Duplicate: Dynamically creating/inserting into an associative array in PHP I have the following
Possible Duplicate: Dynamically creating keys in javascript associative array usually we initialize an array
Possible Duplicate: Creating a memory leak with Java What's the easiest way to cause

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.