Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

applying ArrayLists

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Don't destroy the earth! That's where I keep all my stuff! Including this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic