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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:45:49+00:00 2026-05-27T08:45:49+00:00

I am not too new to Java, though I have never really worked with

  • 0

I am not too new to Java, though I have never really worked with Sets before,
so can anybody please help me out here?

I’m having the following problem;
but first of all, this is my code:

HashMap<Position[], String> save = io.getSave();

Position[][] saved_pos = (Position[][]) save.keySet().toArray();

Though on the second line, Java throws a ClassCastException, but why?
Obviously, the Array returned by save.keySet().toArray() does contain Position[][]s, even though, unfortunately, toArray() in the class Set does only return an Object[] array.

So what can I do, I NEED to cast this.

  • 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-27T08:45:49+00:00Added an answer on May 27, 2026 at 8:45 am

    The toArray() method doesn’t actually know that it’s being called on a set of Position[]s; all it knows is that it’s being called on a set. (This is because of how generics are implemented in Java; not very much information is available at run-time.) So, it just returns an Object[] full of Partition[]s, rather than returning a true Partition[][]. (The difference being that an Object[] can contain arbitrary objects — it would be valid, after your call, to write saved_pos[0] = new Object() — whereas a true Partition[][] would raise a ArrayStoreException if you tried to set one of its elements to new Object().)

    Fortunately, there’s a separate toArray(...) method that can be used, where you tell it what array type you want, like so:

    Position[][] saved_pos = save.keySet().toArray(new Position[0][]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pretty new to ASP.NET MVC and I hope it is not a too
I'm not too inexperienced with ReWrite (not a master either, though) so I was
I'm not too familiar with networking in JAVA but what i'm basically trying to
I am not too familiar with python and have to write a script to
I am not new to java and C#. I thought I understand the concept
i have to admit, i never really completely got the *nix filesystem permission model.
Not too practical maybe, but still interesting. Having some abstract question on matrix multiplication
Not too long ago, I had a problem which required me to set WinDbg.exe
Not too sure what's going on here as this doesn't seem like standard practise
I'm not too sure what is going on here, but sometimes a particular file

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.