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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:10:24+00:00 2026-05-26T14:10:24+00:00

How can I expand an array without knowing its type? I have an Object[]

  • 0

How can I expand an array without knowing its type?

I have an Object[], and I know it contains only, say, Car instances. When I try to typecast it to a Car[], it throws a ClassCastException. I’m working in an environment where Generics are not available.

Must I use a for-loop to copy each element manually into a new array like this:

Car[] cars = new Car[objects.length];
for (int i = 0; i < objects.length; i++) {
    cars[i] = (Car) objects[i];
}

?

  • 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-26T14:10:25+00:00Added an answer on May 26, 2026 at 2:10 pm

    Just because the Object[] happens only to contain Cars, doesn’t mean it can be cast to Car[]. You would need to copy the contents of the array to a new Car[], casting where necessary.

    Remember that arrays are themselves objects, and their type isn’t governed by what’s in them – it’s the other way around.

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

Sidebar

Related Questions

I know you can't dynamically expand a normal array but is this a valid
If you have a statically allocated array, the Visual Studio debugger can easily display
What I want is lots of nodes which can expand making a mind map.
How can I make the anchor tag expand to the bottom of the list
In Eclipse you can double-click on the tab of the code window and expand
In PHP, how can I replicate the expand/contract feature for Tinyurls as on search.twitter.com?
Can you cast a List<int> to List<string> somehow? I know I could loop through
So, I have a cell-array of 1xN vectors of different lengths. I want to
I have the need to store a list/collection/array of dynamically created objects of a
I have a table used by one piece of 3rd party software (say user_fields)

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.