|
|
|
|
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.
|
|

05-05-2008, 01:50 PM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 1
|
|
|
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
|
|

05-05-2008, 01:55 PM
|
 |
Member
|
|
Join Date: Mar 2008
Location: Delhi, India
Posts: 92
|
|
what is array is this a Class
java.sql.Array or reflect.Array 
__________________
Life was much better in 2021
|
|

05-05-2008, 03:00 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,130
|
|
|
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
|
|

05-06-2008, 03:15 PM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 3
|
|
|
...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.
|
|

05-07-2008, 04:45 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,130
|
|
|
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
|
|

05-07-2008, 04:55 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,130
|
|
Something like this may help in that sense, I think.
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
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
All times are GMT +3. The time now is 12:01 PM.
|
|
VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org