Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How do I let the user choose how big an array is...

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I let the user choose how big an array is...

    ...but that user doesn't know how big the array is going to be off the top of his head.

    Let's say I have a for loop that takes user input (integers) and stores them into an array. But the loop doesn't exit until a negative number is typed. How would I do that to make sure I don't go out of bounds of the array, but I don't want a ton of extra values left over either? Because I'm going to sum these numbers once it's all done, and I don't want the 0 values effecting my sum.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How do I let the user choose how big an array is...

    ArrayList (Java Platform SE 6) gives you a dynamic array where an initial size isn't required.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How do I let the user choose how big an array is...

    The underlying implementation of Arraylists (and Vectors for that matter):

    Create an array of some capacity. You will need 1 other variable to hold the actual number of elements in the array. Then, all you need to do is define a few operators:

    1. Add - Adds an element to the end of the array. If the size of the array is equal to the current capacity, you need to allocate a larger array (generally 1.5x or 2x as large) and copy over all the items inside your old array. Increase size by 1.
    2. Set - same as assigning an element in the array a value. You will need to check to make sure the index passed is less than size otherwise you're assigning an invalid location.
    3. Remove - moves all emements behind the given index up by 1. decrease the size by 1.
    4. Insert - Check to see if the current size is less than the capacity. If it is, move all elements behind the given index one space back and put the item into that spot. Increase the size. If it's not, allocate a new array (in the same process as described in add). Add all items from the original array up to the index. Add the new item. Add all the items after that index. Increase the size.

Similar Threads

  1. Opening a File for User
    By aussiemcgr in forum Java Theory & Questions
    Replies: 3
    Last Post: July 29th, 2010, 03:00 AM
  2. Implementing LinkedList as a user?
    By vluong in forum Collections and Generics
    Replies: 3
    Last Post: October 15th, 2009, 03:00 AM
  3. how to know user pressed a key in the keyboard
    By cilang in forum File I/O & Other I/O Streams
    Replies: 16
    Last Post: September 11th, 2009, 10:08 AM
  4. why should i choose java over others?
    By docesam in forum Java Theory & Questions
    Replies: 9
    Last Post: August 30th, 2009, 10:49 PM
  5. Replies: 0
    Last Post: May 21st, 2009, 11:17 AM