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 3 of 3

Thread: Minimum value of objects in ArrayList

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Minimum value of objects in ArrayList

    Hey All!
    I am a newbie in Java but have come across a problem I can't really grasp.
    If you can, please enlighten me, as my head got stuck on this issue

    Alright: assume an ArrayList with objects of class Employee which all have names, id's and salaries. Problem - find the minimum salary.

    So, I understand that I need to create a for loop, but here's where things become tricky. I have no problem of finding min/max of INT in Arrays, or longest/shortest strings in ArrayLists, and even max salary in this ArrayList (i.e. for loops).

    I do not want to use Collections or similar methods - so where do I do it wrong?

    Example:
                 int lowest = ArrayList.size();
    			for(Employee s:ArrayList){
    				if(s.getSalary()<lowest){
    				   lowest = s.getSalary();
    				}
    		}

    ? - lowest becomes ArrayList.size(), but not the smallest salary.


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    491
    Thanks
    8
    Thanked 47 Times in 45 Posts

    Default Re: Minimum value of objects in ArrayList

    Simple. Start with some other value for lowest.

    The most common idiom is to initialize a int variable that has to receive a minimum to Integer.MAX_VALUE, and to initialize a numeric variable that has to receive a maximum value to 0. In either case, you can also choose to initialize the value to the first value extracted from the collection -- list.get(0).getValue().

    db

  3. The Following User Says Thank You to Darryl.Burke For This Useful Post:

    Sputnik (October 23rd, 2010)

  4. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Minimum value of objects in ArrayList

    Darryl.Burke

    Thank you!
    It all makes sense now, it's all about that value of int variable - fix that and you're good to go!

Similar Threads

  1. [SOLVED] Problem with Ordering an Arraylist of Comparable Objects.
    By Faz in forum Collections and Generics
    Replies: 8
    Last Post: June 16th, 2010, 06:36 PM
  2. looping through an ArrayList of objects with their own attributes
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: April 2nd, 2010, 06:15 AM
  3. Arraylist Objects to Database
    By frankycool in forum JDBC & Databases
    Replies: 3
    Last Post: November 15th, 2009, 07:01 PM
  4. How can i store ArrayList objects in Access database
    By frankycool in forum JDBC & Databases
    Replies: 0
    Last Post: November 3rd, 2009, 11:44 PM
  5. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM