I need to build a program - hotel, which has from 1 to XXX rooms.


It is ok, when I have only the:

private static int[] array_of_rooms;

<... CLASS CONSTRUCTOR ...>
array_of_rooms = new int[size]; // Size is defined size, of rooms in the hotel.
// Fill default room status
for(int i = 0; i < size; i++)
{
	array_of_rooms[i] = 0;
}
<... END OF CLASS CONSTRUCTOR ...>

But I'm search sth like I had I C++.

struct type_room
{
	int id;
	int status;
	String start_date;
	String end_date;

};

I tried sth like this in Java:

public class room {
	public int status;
	public String start_date;
	public String end_date;
}

And then define the new type:

private static room[] array_of_rooms; // "room" is my new type

<... CLASS CONSTRUCTOR ...>
// All ok
array_of_rooms = new room[size];

// Fill room's array
for(int i = 0; i < size; i++)
{
	array_of_rooms.room_id[i] = i+1;
	array_of_rooms.room_status[i] = 0;
	array_of_rooms.start_date[i] = "2009-09-19";
	array_of_rooms.start_date[i] = "2009-09-29";
}
<... END OF CLASS CONSTRUCTOR ...>

But elements filling don't work. How I can fill & access the elements of array of my own type, which is sth like struct in c++.

Thanks.

Edited 7 Years Ago by Xamas: n/a

I need to build a program - hotel, which has from 1 to XXX rooms.


It is ok, when I have only the:

private static int[] array_of_rooms;

<... CLASS CONSTRUCTOR ...>
array_of_rooms = new int[size]; // Size is defined size, of rooms in the hotel.
// Fill default room status
for(int i = 0; i < size; i++)
{
	array_of_rooms[i] = 0;
}
<... END OF CLASS CONSTRUCTOR ...>

But I'm search sth like I had I C++.

struct type_room
{
	int id;
	int status;
	String start_date;
	String end_date;

};

I tried sth like this in Java:

public class room {
	public int status;
	public String start_date;
	public String end_date;
}

And then define the new type:

private static room[] array_of_rooms; // "room" is my new type

<... CLASS CONSTRUCTOR ...>
// All ok
array_of_rooms = new room[size];

// Fill room's array
for(int i = 0; i < size; i++)
{
	array_of_rooms.room_id[i] = i+1;
	array_of_rooms.room_status[i] = 0;
	array_of_rooms.start_date[i] = "2009-09-19";
	array_of_rooms.start_date[i] = "2009-09-29";
}
<... END OF CLASS CONSTRUCTOR ...>

But elements filling don't work. How I can fill & access the elements of array of my own type, which is sth like struct in c++.

Thanks.

I don't understand what <...CLASS CONSTRUCTOR...> and <... END OF CLASS CONSTRUCTOR...> refer to. You have a room constructor inside of a static function that returns an array of room ?

array_of_rooms.room_status = 0;

should be

array_of_rooms.room_status = 0;

ie you index the array of rooms, not the status, which is not an array!

Edited 7 Years Ago by JamesCherrill: n/a

Hmm, I changed to non-static, as smb said, but this aint help me.

File(class) room.java (my new 'struct'):

public class room {
	public int status;
	public String start_date;
	public String end_date;
}

Here is my hotel class, which has a problem with an elements filling

public class Hotel {
	// Dinamic size(array)
	private room[] array_of_rooms;

	// Class constructor
	public Hotel(int size)
	{
		// Here is all OK    
		array_of_rooms = new room[size];
		// Fill an room array with
		for(int i = 1; i < size; i++)
		{
                        <------------ PROBLEM START ------------>
                        // Here is the problem (NULL) ?????
			array_of_rooms[i].status = 0;
			int to = i*2+1; int up = i*2+3;
			array_of_rooms[i].start_date = "to";
			array_of_rooms[i].end_date = "up";
			<------------ PROBLEM END ------------>
               }
	}
}

My main class("Manage_hotel.java"), where I initialize the process.

public class Manage_hotel {
	// Create 10 residents for a hotel
	private Resident Residents = new Resident(10,1,2,3);

	// Create 100 rooms for a hotel
	private Hotel Rooms = new Hotel(100);
       <............. MORE ................>
}

Any ideas how to fix this problem? To possible fill and access all the elements ???


----------------------------------------------------
----------------------------------------------------
The second question:

This is my implementation on Resident push_back() function, where I want to add more elements to array - ex. if it was created 10 elements array with "new", it would be 11,12,... etc.

public void push_back(int element)
	{
		int[] temp_arrayOfInts;
		int elements_in_array = arrayOfInts.length;
		temp_arrayOfInts = new int[elements_in_array+1];

		//  Fill the new array
		for (int i = 0; i < elements_in_array; i++) {
			temp_arrayOfInts[i] = arrayOfInts[i];
        }
		temp_arrayOfInts[elements_in_array] = elements_in_array*2;
		arrayOfInts = temp_arrayOfInts;
		int dydis = arrayOfInts.length;
		System.out.println("DYDIS: " + dydis);
	}

It works, but as I understand it isn't the best solution.
As I know, Java dosn't all as C++ did, put the new element with "new NEXT_ELEMENT" and set pointers to the new one as PTR *NEXT, so I'm not able to solve this in C++ way.

Edited 7 Years Ago by Xamas: n/a

public class Hotel {
	// Dinamic size(array)
	private room[] array_of_rooms;

	// Class constructor
	public Hotel(int size)
	{
		// Here is all OK    
		array_of_rooms = new room[size];
		// Fill an room array with
		for(int i = 1; i < size; i++)
		{
                        <------------ PROBLEM START ------------>
                        // Here is the problem (NULL) ?????
			array_of_rooms[i].status = 0;
			int to = i*2+1; int up = i*2+3;
			array_of_rooms[i].start_date = "to";
			array_of_rooms[i].end_date = "up";
			<------------ PROBLEM END ------------>
               }
	}
}

I have no clue what you are trying to do on lines 16 - 18. Line 15 looks fine to me. Do you have a room constructor anywhere? Keep in mind that line 9 does not create any room objects. It creates an array, but no room objects. You need to create those as well before trying to use them. Add this line before line 13 at the top of your loop and see if the NULL error goes away:

room[i] = new room ();

I have no clue what you are trying to do on lines 16 - 18. Line 15 looks fine to me. Do you have a room constructor anywhere? Keep in mind that line 9 does not create any room objects. It creates an array, but no room objects. You need to create those as well before trying to use them. Add this line before line 13 at the top of your loop and see if the NULL error goes away:

room[i] = new room ();

I added a constructor to my room class:

public class room {
	public int status;
	public String start_date;
	public String end_date;

	// Class constructor
	public room()
	{
		status = 0;
		start_date = "";
		end_date = "";
	}
}

But I don't see the point of it's iniatialization for now. If I use "room" constructor, should it allocate memory?

I tried to add line "room = new room();" as you said:

public class Hotel {
	// Dinamic size(array)
	private room[] array_of_rooms; // "room" is my new type

	// KlasÄ—s konstruktorius
	public Hotel(int size)
	{
		// All ok
		array_of_rooms = new room[size];
		// Fill room's array
		for(int i = 1; i < size; i++)
		{
			<... ERROR: cannot find symbol 'room[i]' ...>
			room[i] = new room();
			array_of_rooms[i].status = 0;
			<.... ALL OTHER SOURCE ...>
		}
	}
}

But it just gives me an uncompilable source error:

Uncompilable source code - cannot find symbol
symbol: variable room
location: class javaapplication1.Hotel

As I understand I need to define it anywhere, but the define should be the same size as my rooms array size...

Ahh, the problem was that I should instead of:

room[i] = new room();

Add the line:

//room[i] = new room();
array_of_rooms[i] = new room();

I'll make tread as solved.

Ahh, the problem was that I should instead of:

room[i] = new room();

Add the line:

//room[i] = new room();
array_of_rooms[i] = new room();

I'll make tread as solved.

Yes. My bad. I was not paying attention to the variable name and posted incorrectly. The correction you made is what I intended. Glad you got it to work.

This question has already been answered. Start a new discussion instead.