Results 1 to 4 of 4
  1. #1
    endgame is offline Member
    Join Date
    Jun 2014
    Posts
    7
    Rep Power
    0

    Default Problem with ArrayLists Objects checking

    I have to make ask for 2 houses or more (INSTANSED CLASS, with 2 ints ) with the same size(int ), but i they cant use the same door number(int). how do i check in my ArrayList that they have different numbers while not caring about different size? .

    The ArrayList is in a class called system. I dont know if i should try to Override the equal or try something else.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,375
    Rep Power
    5

    Default Re: Problem with ArrayLists Objects checking

    If you are using ArrayList you just need to do a linear search to see if the house number has already been used. You could also use a BitSet of arbitrary size to record the house number as a bit position. Then just see if the bit position is set to determine if the house number has been used. There are other ways using sets and maps too which would require overriding equals (which you may want to do anyway).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    endgame is offline Member
    Join Date
    Jun 2014
    Posts
    7
    Rep Power
    0

    Default Re: Problem with ArrayLists Objects checking

    what do you mean do a linear search? run by every item and check .getNumber() ; . does that work ??

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,375
    Rep Power
    5

    Default Re: Problem with ArrayLists Objects checking

    Yes, that works. You add one entry. When it's time to add another, you see if it already exists. If it does, you don't add it. As long as you continue in this fashion, no duplicate house numbers will be added.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 1
    Last Post: 05-08-2013, 07:19 PM
  2. ArrayLists Of Objects help!
    By OmegaDracoMax in forum New To Java
    Replies: 2
    Last Post: 11-14-2012, 11:01 PM
  3. Problem with ArrayLists
    By fatedorigin in forum New To Java
    Replies: 1
    Last Post: 03-15-2011, 12:18 AM
  4. adding objects into arrayLists
    By socboy6579 in forum New To Java
    Replies: 19
    Last Post: 12-10-2010, 06:59 AM
  5. arraylists problem
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-12-2008, 07:38 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
  •