Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
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 12-12-2007, 01:26 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
ArrayList problem (finding largest no)
I am working on an assignment and I have to find largest number from an ArrayList using Iterator. I wrote following code.

Code:
ArrayList <Integer>alist = new ArrayList<Integer>(); // populating alist.add(101); alist.add(777); alist.add(872); alist.add(102); alist.add(23); // finding the largest number in the ArrayList Iterator<Integer> it = alist.iterator(); int max = 0; while(it.hasNext()) { if( it.next() > max) max = it.next(); } System.out.println(max);
It compiles fine but the output is not as required.

Output:
Code:
102
Output should be 872. I am not sure what is happening.
Please look into this.

Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-12-2007, 02:33 PM
Member
 
Join Date: Aug 2007
Posts: 22
revathi17 is on a distinguished road
Code:
import java.util.ArrayList; import java.util.Iterator; public class LargestNumber { public static void main(String[] args){ ArrayList<Integer> aList = new ArrayList<Integer>(); aList.add(101); aList.add(872); aList.add(777); aList.add(102); aList.add(23); Iterator<Integer> it = aList.iterator(); int max = 0; while(it.hasNext()){ int temp = it.next(); if(temp > max) max = temp; } System.out.println("Max value:"+max); } }
I think what happens is, only if you call it.next() it goes to the next element and continues iteration. This was something new for me too, i just tried giving a temp variable and it worked!!

-R
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-12-2007, 02:46 PM
Member
 
Join Date: Dec 2007
Posts: 11
gulapala is on a distinguished road
Bugger,

When you call it->next, it goes to the next iterator element. So in your code the statement is being called twice due to which, it goes to 3 element, if it satisfies the if condition. So, you should call it only once.
__________________
Thanks & Regards, G.Rajasekhar
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-12-2007, 02:47 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
Thanks for the feedback. Yes, its works.
A small thing can make such a big difference
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
Finding Largest Prime Factor perito New To Java 4 10-16-2008 08:10 AM
ArrayList problem with images Cymro New To Java 2 02-05-2008 08:22 PM
Finding largest and smallest integer mlhazan New To Java 2 01-13-2008 12:30 AM
ArrayList problem khamuruddeen New To Java 7 12-22-2007 07:46 AM
Finding largest no bugger New To Java 11 11-29-2007 02:49 PM


All times are GMT +3. The time now is 03:40 AM.


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