How to make an array without setting the size first?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ian Smith

    #1

    How to make an array without setting the size first?

    I want to create an array to store values from user input. The user input is in a while (choice=='y' || choice=='Y') loop. As long as the user keeps selecting Y/y at the end of the loop, they can enter more values and continue. I want to store the 2 values that are entered and a 3rd calculated value in an array. The user can stop after 1 value or keep going up to 100 iterations of the loop (if they so desire).

    My original thought was to create 3 array variables but I am stuck trying to figure out the exact code needed to allow an array(s) to just grow, as needed. I do not want to preset the size of the array to any single value though. Any help in this manner would be appreciated.
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    You can use some collection frameworks for this.

    Regards
    Dheeraj Joshi

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      What you're doing sounds a lot like an ArrayList or a Vector, both of which are collections.

      Greetings,
      Nepomuk

      Comment

      • Oralloy
        Recognized Expert Contributor
        • Jun 2010
        • 988

        #4
        I'd stick with the ArrayList, unless you require synchronization . But - I suspect that you aren't at the point of needing to know the difference in your training.

        Work with what you've got, write a prototype, and if you have any questions about your code - ask them.

        Comment

        Working...