Results 1 to 4 of 4
  1. #1
    YoungJavaBoy is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default Collections help.

    Hey, I'm writing a program that is using an arraylist.


    I've used Collections.max(arraylist) and Collections.min(arraylist) to return the longest and shortest elements in the arraylist. However, the shortest an element in the arraylist can be is 1, because otherwise the element is empty and is skipped when being counted.

    How can I get my program to include the empty elements when searching for the shortest element?



    thanks for your help, I know it's probably a simple answer and only one or two lines of code but I'm struggling to think of it

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I need more detail. What is your arraylist holding? Also, an arraylist readjusts the size when you add or remove something. So there shouldn't be empty elements.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    There are 2 max and 2 min methods. The second version of each takes a Comparator as a parameter. Therefore write your own Comparator to compare the elements however you like.

  4. #4
    YoungJavaBoy is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    Ahh thanks for your help, was just an error on my part. Collections.min already does what I wanted it to, I just called max instead of min in one part.


    thanks

Similar Threads

  1. Qustion about collections
    By nn12 in forum New To Java
    Replies: 2
    Last Post: 01-10-2011, 03:36 PM
  2. Help collections
    By nikosv in forum New To Java
    Replies: 8
    Last Post: 12-13-2010, 05:29 PM
  3. Collections
    By Cbani in forum New To Java
    Replies: 3
    Last Post: 02-16-2010, 02:46 PM
  4. Collections Help
    By Dr Gonzo in forum New To Java
    Replies: 0
    Last Post: 12-07-2008, 09:15 PM
  5. Performance Of Collections
    By thomasprabu in forum Advanced Java
    Replies: 0
    Last Post: 01-05-2008, 11:17 AM

Posting Permissions

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