Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By quad64bit

Thread: Constructor Array

  1. #1
    ryan1234 is offline Member
    Join Date
    Oct 2012
    Posts
    13
    Rep Power
    0

    Default Constructor Array

    I've got an array which I declare (private ArrayList<Integer> occupiedRooms;), in my constructor I want to add an element to the array (occupiedRooms.add(roomNo);), however every time I create a new object it re-instantiates the array (occupiedRooms = new ArrayList<Integer>();)) and therefore previously added elements are removed.

    Is there a way of keeping the previously added elements in the array, every time a new object is created?

    Thanks

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Constructor Array

    Yes, simply declare the array outside of your constructor, and add a method to your class to add items to that array. Then you only need to make 1 instance of the class.

  3. #3
    ryan1234 is offline Member
    Join Date
    Oct 2012
    Posts
    13
    Rep Power
    0

    Default Re: Constructor Array

    Sorry I didn't make it clear, I declare it outside the constructor, but instantiate it within the constructor.

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Constructor Array

    That doesn't matter since the constructor is only used once for any given object. If you are somehow making many of the the object in question, then thats your problem right there. You should only have one.

  5. #5
    ryan1234 is offline Member
    Join Date
    Oct 2012
    Posts
    13
    Rep Power
    0

    Default Re: Constructor Array

    Oh ok. What I'm creating is a booking system for hotel rooms, so I thought you had to create a new Room object every time a room is booked. Is this not the case?

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Constructor Array

    Why would a given room contain the list of other rooms? The list of occupied rooms should exist outside of the Room class. One of the OOP principles is that your design has to make sense if you say it out loud. Does it make sense to say that a HotelRoom has occupied rooms? Or does it make more sense to say a Hotel has both occupied and vacant rooms?
    Tolls likes this.

  7. #7
    ryan1234 is offline Member
    Join Date
    Oct 2012
    Posts
    13
    Rep Power
    0

    Default Re: Constructor Array

    Ok understood. I know have a Hotel class and Room class. How do I go about searching through the arraylist rooms to see if a user entered roomno is already in the arraylist?

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Constructor Array

    Great question, now you're moving in the right direction. There are a couple ways to do this, probably the easiest is to keep all the rooms (empty or occupied) in an array. Then in the room itself, you could have a boolean called occupied or something similar. You know a room is occupied or not by looking at this value. To find vacant rooms, you just need to loop through the list (with a counting loop, like a for loop, or with fast iteration) until you find an unoccupied room. If your room numbers match your array indexes, you can even look up a room by number. Does that help?

  9. #9
    ryan1234 is offline Member
    Join Date
    Oct 2012
    Posts
    13
    Rep Power
    0

    Default Re: Constructor Array

    That's great thank you.

  10. #10
    ryan1234 is offline Member
    Join Date
    Oct 2012
    Posts
    13
    Rep Power
    0

    Default Re: Constructor Array

    How would I go about removing a Room from the arraylist?

  11. #11
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Constructor Array

    Do hotels loose their rooms? Why would you remove it?

  12. #12
    ryan1234 is offline Member
    Join Date
    Oct 2012
    Posts
    13
    Rep Power
    0

    Default Re: Constructor Array

    I've got a arraylist which just adds rooms that are occupied into it and then need to be removed when there not occupied.

  13. #13
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Constructor Array

    Did you read my advice about modeling in a way that makes sense and using a boolean flag for determining occupation status?

  14. #14
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Constructor Array

    That said, it is possible to remove things from an ArrayList. Have a look at the API docs for it, you'll find the method you are looking for. I wouldn't do it this way though, as it would mean creating and disposing of a lot of objects for no good reason, and would also make preserving random access much more difficult. My suggestions was much more flexible.

Similar Threads

  1. array in constructor.
    By Juukamen in forum New To Java
    Replies: 11
    Last Post: 10-30-2011, 10:14 PM
  2. array copy in class constructor.
    By Juukamen in forum New To Java
    Replies: 2
    Last Post: 10-29-2011, 12:07 AM
  3. Problem with using an array in a constructor
    By planesinspace in forum New To Java
    Replies: 3
    Last Post: 08-28-2009, 09:17 AM
  4. Sending an array in a constructor?
    By dch414 in forum New To Java
    Replies: 2
    Last Post: 09-14-2008, 09:59 PM
  5. Array Constructor
    By Javanoob828282 in forum New To Java
    Replies: 1
    Last Post: 04-30-2008, 10:25 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
  •