Results 1 to 4 of 4
  1. #1
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

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

    Java 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:
    Java Code:
    102
    Output should be 872. I am not sure what is happening.
    Please look into this.

    Thanks.

  2. #2
    revathi17 is offline Member
    Join Date
    Aug 2007
    Posts
    26
    Rep Power
    0

    Default

    Java 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

  3. #3
    gulapala is offline Member
    Join Date
    Dec 2007
    Posts
    11
    Rep Power
    0

    Default

    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

  4. #4
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default

    Thanks for the feedback. Yes, its works.
    A small thing can make such a big difference :D

Similar Threads

  1. Finding Largest Prime Factor
    By perito in forum New To Java
    Replies: 7
    Last Post: 11-08-2010, 08:25 PM
  2. ArrayList problem with images
    By Cymro in forum New To Java
    Replies: 2
    Last Post: 02-05-2008, 06:22 PM
  3. Finding largest and smallest integer
    By mlhazan in forum New To Java
    Replies: 2
    Last Post: 01-12-2008, 10:30 PM
  4. ArrayList problem
    By khamuruddeen in forum New To Java
    Replies: 7
    Last Post: 12-22-2007, 05:46 AM
  5. Finding largest no
    By bugger in forum New To Java
    Replies: 11
    Last Post: 11-29-2007, 12:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •