Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-05-2008, 01:50 PM
Member
 
Join Date: Apr 2008
Posts: 2
hai789 is on a distinguished road
Looping ArrayList
array t1={2,6,4,9}


can anyone give a for or while loop code such that i need the largest and the secondlargest values from this array



ie the values 6,9
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-05-2008, 01:55 PM
rjuyal's Avatar
Senior Member
 
Join Date: Mar 2008
Location: Delhi, India
Posts: 129
rjuyal is on a distinguished road
Quote:
array t1={2,6,4,9}

what is array is this a Class
java.sql.Array or reflect.Array
__________________
Newton said Gravitaion, Rakesh says Earth Sucks
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-05-2008, 03:00 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,264
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
In simple way, loop the array on each element and at the same time maintain two variables to hold largest values.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-06-2008, 03:15 PM
Member
 
Join Date: Apr 2008
Posts: 3
abhishek paul is on a distinguished road
...in case you think of replacing the simple array with a ArrayList obj you can very well go for the Collections.max(Collection obj) to get the maximum value.

Regards,
Abhishek.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-07-2008, 04:45 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,264
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Yes, it's true. But he is looking to do it in a loop.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-07-2008, 04:55 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,264
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Something like this may help in that sense, I think.

Code:
private void findMax(ArrayList arl) { int max = 0; for(int i = 0; i < arl.size(); i++) { if(Integer.parseInt((String) arl.get(i)) > max) { max = Integer.parseInt((String) arl.get(i)); } } System.out.println(max); }
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't solve error message while looping BHCluster New To Java 15 04-22-2008 11:51 AM
Java Project Trouble: Searching one ArrayList with another ArrayList BC2210 New To Java 2 04-21-2008 12:43 PM
ArrayList kizilbas1 New To Java 1 01-12-2008 09:48 PM
Java Looping and decision susan AWT / Swing 1 08-07-2007 05:29 AM
looping a function Username New To Java 2 07-30-2007 06:37 PM


All times are GMT +3. The time now is 10:33 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org