• Post Reply Bookmark Topic Watch Topic
  • New Topic

Get Longest String in ArrayList of Strings  RSS feed

 
chintu reddy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have a ArrayList of strings and I need to get the longest one in it.. I wrote the following code but I thought there must be some better ways to do it.. if anyone has such ways, please help me......

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code or configuration. Unformatted code and configuration is very difficult to read. You can edit your post to include them by using the button.

And welcome to JavaRanch!
 
Bert Wilkinson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like the first homework in a computer theory class on sorting!

If you are given an ArrayList or some other unsorted collection of strings, you have the fastest sort method, because every object will need to be looked at.

However, if you have choice over how to store the items, you can look into the Collections framework or the Collection interface in javadoc. There are collections that sort the members when added, such as TreeSet, PriorityQueue, etc. based on some criteria for how to sort them.

However, these open the door to Comparators and Comparable....which may be beyond where you want to go with this...

 
chintu reddy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@bert..

So you are saying that since I was given ArrayList, whatever code I have is best?

thanks for your reply
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!