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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:07:43+00:00 2026-05-27T02:07:43+00:00

Why does I get an exception Exception in thread main java.lang.ClassCastException: [Ljava.lang.Object; cannot be

  • 0

Why does I get an exception

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;
        at Main.main(Main.java:12)

for the following code?

import java.util.Set;
import java.util.HashMap;


public class Main
{
        public static void main(String args[])
        {
                HashMap<Integer, Double> h = new HashMap<Integer,Double>();
                h.put(1, 2.2);

                Integer[] keys = (Integer[])h.keySet().toArray();
        }
}

Shouldn’t it be possible to cast the Object[] returned to Integer[], since the key set contains integers? What is a fast alternative to copy the key set to an integer array?

  • 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-27T02:07:44+00:00Added an answer on May 27, 2026 at 2:07 am

    Use

    Integer[] keys = h.keySet().toArray(new Integer[h.keySet().size()]);
    

    Passing an array of the same size as the keyset is indeed the best way because Java will use the given array to store all the values of the keyset. If the given array is not of the same size as the keyset, Java will have to create an entirely new array to fit the size of the keyset. The passed first array will never be used, it will just be taking up memory until the garbage collector allocates it again.

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

Sidebar

Related Questions

I keep getting a ClassCastException Exception in thread main java.lang.ClassCastException: java.lang.String cannot be cast
I get this exception when i try to cast an Object array to a
Does a destructor get called if the app crashes? If it's an unhandled exception
I am completely stumped as to why this code does not get any SDL
If I do the following, does filehandle get closed automatically as it goes out
I thought that i have some good understanding of Java generics. This code DOES
I'm using code similar to whats below with the exception that the thread is
How does one get a list of running applications as shown in the "Applications"
Where does glibc get its database of unicode attributes, for such functions as eg,
where does DotNetZip get it's root directory for saving. All the save examples don'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.