Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

find the index of the longest string in an arraylist

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i need to return the index of the longest string in the arraylist in java
so far i have



but all it does is return 0
 
Ranch Hand
Posts: 40
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ashley,
I see there is no problem in the code and logic. It works fine.
Can you first print the ArrayList and see what are the values inside. It may be that the First string is largest itself.

Here how I tested it.



Output is 2
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ashley Feng wrote:so far i have...


Like Vijay, I can't see anything logically wrong with your method, but it does contain a few possibilities for improvement:

1. I'd definitely get into the habit of using for-each loops for collections, because
(a) they usually have less boilerplate.
(b) they're less likely to be affected by the type of Collection they operate on.
For example, your method could be re-written:and it will work just as fast whether your List is an ArrayList or a LinkedList (your loop won't).
Their main disadvantage is that you have to define the index outside the loop, but in this case I reckon it's probably worth it.

2. Prefer variables to method calls. Your loop has to call size() for each iteration, butonly calls it once. Now in this case it probably doesn't make a lot of difference, but it is worth remembering.

3. Cover all your bases. Your method silently returns a valid index (0) if the supplied List is empty, whereas the example above returns -1. An alternative is to throw an Exception, which is equally valid. Just remember to allow for it and document what happens.

HIH

Winston
 
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic