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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:34:28+00:00 2026-06-12T02:34:28+00:00

So, I am using 2D arrays for a platformer physics system I’m working on.

  • 0

So, I am using 2D arrays for a platformer physics system I’m working on. Unfortunately, the 2D arrays require generic types for various reasons.

I’m dynamically building the surfaces with some java Collections (linkedlist to be precise) and then converting them to 2D arrays with some hacky trickery. This currently works, giving me the appropriate 2D generic array: *

LinkedList<PhysSurface<P>> leftSurfaces = new LinkedList<PhysSurface<P>>();
LinkedList<PhysSurface<P>> rightSurfaces = new LinkedList<PhysSurface<P>>();
LinkedList<PhysSurface<P>> topSurfaces = new LinkedList<PhysSurface<P>>();
LinkedList<PhysSurface<P>> bottomSurfaces = new LinkedList<PhysSurface<P>>();

// Add surfaces to the lists

GenericArray<PhysSurface<P>[]> base = new GenericArray<PhysSurface<P>[]>(4);
PhysSurface<P>[][] surfaces = base.elements();
surfaces[0] = leftSurfaces.toArray(new GenericArray<PhysSurface<P>>().elements());
surfaces[1] = rightSurfaces.toArray(new GenericArray<PhysSurface<P>>().elements());
surfaces[2] = topSurfaces.toArray(new GenericArray<PhysSurface<P>>().elements());
surfaces[3] = bottomSurfaces.toArray(new GenericArray<PhysSurface<P>>().elements());

However, when I try to put this all into a generic, static method like this:

@SuppressWarnings("unchecked")
public static <T> T[][] to2DArray(Collection<T>... collections)
{
    GenericArray<T[]> base = new GenericArray<T[]>(collections.length);
    T[][] array = base.elements();
    for(int i = 0; i < collections.length; i++)
        array[i] = collections[i].toArray(new GenericArray<T>().elements());
    return array;
}

And then I call the method like this:

PhysSurface<P>[][] surfaces = GenericsUtils.to2DArray(leftSurfaces, rightSurfaces, topSurfaces, bottomSurfaces);

Then everything crashes, giving me a ClassCastException, saying that it cannot convert type Object to type PhysSurface. The stack trace is here:

Exception in thread "main" java.lang.ClassCastException: [[Ljava.lang.Object; cannot be cast to [[Lcom.meg.utils._2D.platformer.phys.environment.surface.PhysSurface;
    at com.meg.chaos_temple.test.world.PhysTestWorld$TestMap.<init>(PhysTestWorld.java:487)
    at com.meg.chaos_temple.test.world.PhysTestWorld$TestPlayer.<init>(PhysTestWorld.java:245)
    at com.meg.chaos_temple.test.world.PhysTestWorld.<init>(PhysTestWorld.java:95)
    at com.meg.chaos_temple.main.ChaosDebug.createWorld(ChaosDebug.java:72)
    at com.meg.jrabbit.engine.main.BaseGame.start(BaseGame.java:56)
    at com.meg.jrabbit.engine.loop.Loop.run(Loop.java:44)
    at com.meg.jrabbit.engine.main.BaseGame.run(BaseGame.java:40)
    at com.meg.jrabbit.engine.main.StandardGame.run(StandardGame.java:85)
    at com.meg.chaos_temple.main.ChaosDebug.main(ChaosDebug.java:19)

As far as I can tell, the generic method is not using the generic type T, but instead defaulting to creating a Object[][] and attempting to cast up when it returns. If this is what’s happening, why does this not work when put into a static method? And if not, what the heck is going on?


  • (GenericArray is a custom class that uses variable-length arguments to get around the usual barriers to making generic arrays and then wraps the result. The actual constructor is GenericArray(T… elements).)
  • 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-12T02:34:30+00:00Added an answer on June 12, 2026 at 2:34 am

    Unfortunately T[][] array is the same as Object[][] array at run-time. Generic type parameters will be dropped at run-time; only the compiler knows about it.

    The solution is to pass the object’s class: Class<T>.

    import java.reflect.Array;
    
    ...
    
    public static <T> T[][] to2DArray(Class<T> klazz, Collection<T>... collections)
    {
        T[][] array = (T[][]) Array.newInstance(klazz, collections.length,
                                                       collections[0].size());
        //...
            array[i] = (T[]) Array.newInstance(klazz, collections[i].size());
        return array;
    }
    

    The cast of Array.newInstance is necessary for the multidimensional result.
    No more @SuppressWarnings("unchecked")!

    Instead of passing collections[0].size() it probably suffices to pass 0.

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

Sidebar

Related Questions

I tried using arrays but I don't need every single element, and so it
I've been using arrays lately and really missing Python's in operator. e.g.: if (hello
Its known that Java ArrayList is implemented using arrays and initializes with capacity of
So I can't use initializers in my class constructor because of using arrays, so
I just started using php arrays (and php in general) I have a code
How to create nested arrays using gwt JsArray.
Possible Duplicate: Comparing Two Arrays Using Perl I am trying to find elements that
I have built a custom hashmap using two arrays. One contains keys another values.
I currently allocate my memory for arrays using the MS specific mm_malloc. I align
Up to this moment I was using simple arrays to enter and get necessary

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.