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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:09:48+00:00 2026-06-15T03:09:48+00:00

I have a generic (I’m not too sure if that’s the correct terminology..) method

  • 0

I have a generic (I’m not too sure if that’s the correct terminology..) method that adds an object to the an array.

@SuppressWarnings("unchecked")
public static <T>T[] appendArray(T[] a, T b)
{
    T[] temp = (T[])new Object[a.length + 1];
    System.arraycopy(a, 0, temp, 0, a.length);
    temp[a.length] = b;
    return temp;
}

In Eclipse, this code gives me no errors, no warnings (except for the suppressed “unchecked” warning), and, it seems to me, that this should work.. However, when I attempt to call this method with something such as..

Integer[] a=new Integer[]{1,2,3};
Integer b=4;
a = appendArray(a, b);

It gives me a ClassCastException on, in this case, line 3 of the 2nd snippet of code I posted. The error says, [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer; I neither see, nor understand why it would be doing this.. I mean, I’m using generics, so why would it there be an Object[] other than the one which I cast to type T during instantiation? And if it is the one at line number 4 (in the 1st bit of code, of course), why isn’t the exception being thrown there rather than at line 3 of the 2nd bit of code?

  • 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-15T03:09:49+00:00Added an answer on June 15, 2026 at 3:09 am

    Instead of using System.arraycopy, use Arrays.copyOf, which instantiates the array as the generic type.

    public static <T> T[] appendArray(T[] a, T b) {
        T[] temp = (T[]) Arrays.copyOf(a, a.length + 1, a.getClass());
        temp[a.length] = b;
        return temp;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a generic Print method that iterates over a list and simply prints
I have generic type that looks like: public class GenericClass<T, U> where T :
I have a Generic List object which hold below table as its own value.
I have a generic linked-list that holds data of type void* I am trying
I have a Generic Base Class that I want to allow one of two
So now that we have generic Covariance and Contravariance on interfaces and delegates in
I'm writing a couple of classes that all have generic type arguments, but I
I have a generic method: public bool DoSomething<T>(T item) where T: IBase { return
I have a generic repository that is using Entity Framework 4 with the DbContext
I have a generic DAO class that looks like this: public class GenericDaoJpa <T

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.