Results 1 to 8 of 8
Thread: "currentSize" of an Array.
- 04-09-2011, 06:48 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 48
- Rep Power
- 0
"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?
-
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.
- 04-09-2011, 07:56 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 48
- Rep Power
- 0
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!
- 04-09-2011, 08:05 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 48
- Rep Power
- 0
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.,
But this should work just as well for you since List is the interface for ArrayList.Java Code:String[] foos = {"one", "two", "thre"}; java.util.List<String> stringList = java.util.Arrays.asList(foos);
- 04-09-2011, 08:17 PM #6
Member
- Join Date
- Jan 2011
- Location
- Gainesville, FL
- Posts
- 45
- Rep Power
- 0
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'.
- 04-09-2011, 08:25 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 48
- Rep Power
- 0
Then my next question would be how to make:
Work with the newly created ArrayList?Java Code:for(n = 0; n < gpaArr.length; n++) { if(gpaArr[n] > minGpaAll) { minGpaAll = gpaArr[n] } return minGpaAll; }
- 04-09-2011, 10:29 PM #8
Member
- Join Date
- Mar 2011
- Posts
- 48
- Rep Power
- 0
Similar Threads
-
connection = DriverManager.getConnection(DATABASE_URL,'"+userid +"','"+password+"');
By renu in forum New To JavaReplies: 3Last Post: 10-12-2010, 04:21 PM -
How to change my form design from "metal" to "nimbus" in Netbeans 6.7.1?
By mlibot in forum New To JavaReplies: 1Last Post: 01-21-2010, 09:20 AM -
Selecting the greatest "double" in a string array
By gangsterooseven in forum New To JavaReplies: 6Last Post: 11-07-2009, 11:37 PM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks