Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help with Finding Max and Min Values of ArrayList

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Help with Finding Max and Min Values of ArrayList

    Hey guys, how do you find the max value and min value of an ArrayList without using collections? I know this is the way you find the max and min value of an array but I don't get how you would do it for an ArrayList.

    //highest value
    int[] numbers = new int[50];
    	int highest = numbers[0];
    	for (int i = 1; i < numbers.length; i++)
    	{
    		if (numbers[i] > highest)
    			highest = numbers[i];
    	}
     
    //lowest value
    	int lowest = numbers[0];
    	for (int i = 1; i < numbers.length; i++)
    	{
    		if (numbers[i] < lowest)
    			lowest = numbers[i];
    	}


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,355
    Thanks
    182
    Thanked 833 Times in 776 Posts
    Blog Entries
    5

    Default Re: Help with Finding Max and Min Values of ArrayList

    You would do it the same way with a slight change in syntax...iterate over the List as you would an array but using the List/ArrayList functions like get and size (see List (Java Platform SE 6) for available functions you can use)

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with Finding Max and Min Values of ArrayList

    Thanks for pointing me to the ArrayList functions.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Help with Finding Max and Min Values of ArrayList

    A more efficient method is to check if new item is MAX or MIN before adding it to the List. That way there is no need to iterate over the entire list to find them.

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. finding pi
    By gonfreecks in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 2nd, 2010, 06:15 PM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 03:08 PM
  4. Arraylist or Arraylist Object?
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: September 11th, 2009, 03:08 AM
  5. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 02:11 PM