Results 1 to 14 of 14
Thread: Constructor Array
- 10-30-2012, 11:29 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 13
- Rep Power
- 0
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
- 10-30-2012, 11:50 PM #2
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.
- 10-30-2012, 11:53 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 13
- Rep Power
- 0
Re: Constructor Array
Sorry I didn't make it clear, I declare it outside the constructor, but instantiate it within the constructor.
- 10-30-2012, 11:54 PM #4
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.
- 10-31-2012, 12:01 AM #5
Member
- Join Date
- Oct 2012
- Posts
- 13
- Rep Power
- 0
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?
- 10-31-2012, 12:04 AM #6
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?
- 10-31-2012, 02:47 PM #7
Member
- Join Date
- Oct 2012
- Posts
- 13
- Rep Power
- 0
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?
- 10-31-2012, 03:50 PM #8
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?
- 10-31-2012, 07:42 PM #9
Member
- Join Date
- Oct 2012
- Posts
- 13
- Rep Power
- 0
Re: Constructor Array
That's great thank you.
- 10-31-2012, 07:54 PM #10
Member
- Join Date
- Oct 2012
- Posts
- 13
- Rep Power
- 0
Re: Constructor Array
How would I go about removing a Room from the arraylist?
- 10-31-2012, 08:02 PM #11
Re: Constructor Array
Do hotels loose their rooms? Why would you remove it?
- 10-31-2012, 08:09 PM #12
Member
- Join Date
- Oct 2012
- Posts
- 13
- Rep Power
- 0
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.
- 10-31-2012, 08:10 PM #13
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?
- 10-31-2012, 08:17 PM #14
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
-
array in constructor.
By Juukamen in forum New To JavaReplies: 11Last Post: 10-30-2011, 10:14 PM -
array copy in class constructor.
By Juukamen in forum New To JavaReplies: 2Last Post: 10-29-2011, 12:07 AM -
Problem with using an array in a constructor
By planesinspace in forum New To JavaReplies: 3Last Post: 08-28-2009, 09:17 AM -
Sending an array in a constructor?
By dch414 in forum New To JavaReplies: 2Last Post: 09-14-2008, 09:59 PM -
Array Constructor
By Javanoob828282 in forum New To JavaReplies: 1Last Post: 04-30-2008, 10:25 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks