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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:53:18+00:00 2026-06-01T19:53:18+00:00

At the moment I have an array constructed like this: Car[] garage = new

  • 0

At the moment I have an array constructed like this:

Car[] garage = new Car[5];
garage[0] = new Car("Ford", "Focus", "S301 ABN");
garage[1] = new Car("Opel", "Astra", "WA55 AAP");
garage[2] = new Car("Ford", "Explorer", "L66 4ABE");
garage[3] = new Car("Dodge", "Viper", "FA55 SAM");
garage[4] = new Car("Ferrari", "F50", "SAS3 47S");

However, I need the size of the array to be set by a user defined value which comes from a JComboBox. I’ve got the JComboBox setting the value working but it throws an ArrayIndexOutOfBoundsException because elements exist that aren’t within the array index bounds. So my question is, what way should I go about adding the elements to the array after the user selects the size of the 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-06-01T19:53:19+00:00Added an answer on June 1, 2026 at 7:53 pm

    Import java.util.ArrayList, and use the ArrayList data structure instead of a normal array.

    To do the same thing your code you posted, you’d use code like something like this:

    List<Car> garage = new ArrayList();
    garage.add(new Car("Ford", "Focus", "S301 ABN"));
    //etc
    

    To get the nth element of the garage, use garage.get(n).

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

Sidebar

Related Questions

For example, I have an array like this: array(4) ( a => string(0) b
At the moment I have an array of public Module[]moduleArray = new Module[4]; and
At the moment I have the following code which works fine. label = new
At the moment I have this code which fires a projectile: Vector3 d =
At the moment I have this in Actionscript (for Flex) to create 26 labels
I have an array: array('id' => 'really') I have a string: $string = 'This
At the moment I have this to check whether the $content is containing youtube
At the moment I have to manually add items to my array but I
I have an array of which each element is an array, like so: results
At the moment I have: //replace common characters $search = array('&', '£', '$'); $replace

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.