wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes applying ArrayLists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "applying ArrayLists" Watch "applying ArrayLists" New topic
Author

applying ArrayLists

Wolfgang Obi
Ranch Hand

Joined: Dec 05, 2005
Posts: 134
okay, still on code optimisation....

i've been told once again that this programme (see below) would be more efficient using ArrayLists....
how do i start off?
what must i consider?

what else is redundant in this programme?

thx!

Sriram Sharma
Ranch Hand

Joined: Apr 12, 2006
Posts: 97
first of all, the IM.readString() will give u a compilation error as IM is not defined anywhere in the code.
Assuming that u have declared it correctly and reading the string appropriately... here goes some points...

1. Lets say, we have only 3 numbers in the string that is being read. String = "123". But the array is of size 5. So remaining 2 allocated space is waste of space. In this case, we can use array list and finally trim the arraylist to the exact size.

2. Are you sure that the string being read are only numbers? Else, u may get number format Exception when u do the parseInt stuff.

3. Why is it that u are doing "if(markedElement != noah[i])" after setting markedElement to noah[0]. Because, as per ur logic, in the createArray, elements[0], elements[1]...elements[4] are all set to the string available in the variable number. So the if loop does not make sense as the control will never enter the if loop, unless the marked element is given some different value other than noah elements.

Suggestions & Corrections for this reply is always welcome as everybody is bound to make mistakes.
Wolfgang Obi
Ranch Hand

Joined: Dec 05, 2005
Posts: 134
thanks Sriram for your reply:

as per ....

1. yes for now i'm limiting the scope of the strings to be read in to integers only first of all.

2. the "if(markedElement != noah[i])" is a way of checking when the next element DIFFERENT from the first marked element occours. If you noticed: when the markedElement == noah[i] my counter is increased by one,....thereby providing a means by which to count the marked element (i.e. the != operator tells the programme when to stop counting the markedElement and move on to the next one)...in any case it's working, this wasnt my problem. I'm trying to implement an ArrayList....how do i do this?

PS: the IM class is one that reads from the command line (i didnt bother posting it because i thought it would be self explanatory.)
 
jQuery in Action, 2nd edition
 
subject: applying ArrayLists