Results 1 to 8 of 8
  1. #1
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default "currentSize" of an Array.

    I am working with partially filled arrays, and Im using the array to store GPA's. I have to set the array size to 30, yet I've heard mentioned a way to keep track of the current size of the filled part of the array? Its affecting my calculating of the minimum values of the GPA's provided, in that when there are 0's stored for the rest of the area--they assume into the minimum calculation.

    Suggestions?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Are you allowed to use ArrayList instead? It is more appropriate to this situation of a variable sized array like collection. Otherwise you'll want to create a method to iterate through the array and count all elements that are not holding default (perhaps null if an array of objects) values.

  3. #3
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    I used to think we couldn't use anything "more advanced" than what we've currently learned, but the teacher made an off hand comment to the effect of "If its legal java, you can use it".

    I'll look into ArrayList then, thanks!

  4. #4
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    Is there a way to copy the existing Array into a new ArrayList?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Teclis View Post
    Is there a way to copy the existing Array into a new ArrayList?
    Almost, you can get a List from the array using the Arrays method asList():

    e.g.,
    Java Code:
          String[] foos = {"one", "two", "thre"};
          java.util.List<String> stringList = java.util.Arrays.asList(foos);
    But this should work just as well for you since List is the interface for ArrayList.

  6. #6
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    I had an assignment a while back where we had to double the size of an array if it ever reached maximum capacity. Simply put, you have an object which has an array data field, and data fields for the max number of elements and the current number of elements. Every time you add an element, increment the variable for current number of elements (and the reverse for removing elements). Then, when calculating your GPAs, loop from element 0 to element 'current number of elements' rather than element 0 to element 'max number of elements'.

  7. #7
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    Then my next question would be how to make:

    Java Code:
    for(n = 0; n < gpaArr.length; n++)
    {
       if(gpaArr[n] > minGpaAll)
       {
          minGpaAll = gpaArr[n]
       }
    return minGpaAll;
    }
    Work with the newly created ArrayList?

  8. #8
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    In the natural course of writing the program, I had a variable that basically "kept track" of the size of the Array.

    If I'd have thought about it a bit more, it would've been easy from the beginning. Thanks though!

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2010, 04:21 PM
  2. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  3. Selecting the greatest "double" in a string array
    By gangsterooseven in forum New To Java
    Replies: 6
    Last Post: 11-07-2009, 11:37 PM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •