将单个元素添加到java结构数组时出错

将单个元素添加到java结构数组时出错,java,arrays,Java,Arrays,我是java新手,在尝试向结构类型数组中添加单个元素时遇到了问题。我的阵列设置如下:公共公寓可用eRoom[]=新公寓[1]My main调用一个方法,该方法在应用程序启动时立即对此进行初始化: availableRoom[0] = new apartment(150, 2, 200.00,null); //this sets default values for room#, beds, price, and guest 我的构造函数像这样获取信息 public apartment(int

我是java新手,在尝试向结构类型数组中添加单个元素时遇到了问题。我的阵列设置如下:
公共公寓可用eRoom[]=新公寓[1]
My main调用一个方法,该方法在应用程序启动时立即对此进行初始化:

availableRoom[0] = new apartment(150, 2, 200.00,null);
//this sets default values for room#, beds, price, and guest
我的构造函数像这样获取信息

public apartment(int roomNum, int beds, double price, String guest )
    { 
        this.roomNumber = roomNum;
        this.roomBeds = beds;
        this.nightlyFee = price;
        this.roomGuest = guest;
    }
我遇到问题的地方是当我试图将客人分配到房间时。我正在使用
AvailableeRoom[I]进行尝试。roomGuest=name
name由用户输入,I设置为0(已选中)。没有错误,但当我打印房间的信息时,它会将每个值返回为0,将来宾返回为null。有人能看出我做错了什么吗?(仅供参考的公寓是与主楼分开的一个等级)

主要

java公寓

public void createApt()
    {
       availableRoom[0] = new apartment(150, 2, 200.00,null);
}

public void addGuest()
{

    name = input.next();
    availableRoom[i].roomGuest = name;

}
嗯,正如你所说

没有错误,但当我打印房间的信息时,它会将每个值返回为0,将来宾返回为null

我认为,您正在设置不同对象中的值并打印不同的值。如果您只是粘贴如何打印其值,可能会有很大帮助

需要考虑的事情

  • 由于您获得的是默认值(如果您不分配任何值,则实例变量将获得该值),这意味着数组中存在一个实际对象,该对象已实例化但未初始化
  • 仔细查看正在打印的对象和正在设置值的对象
  • 很可能在第一个索引处,插入了一个新实例化的对象,该对象以某种方式替换了原始对象
  • 您正在打印与原始对象不同的对象…有可能
  • 您可能希望去掉“无参数构造函数”,以便更好地了解问题的实际情况。试试看……它的价值

  • 您的程序未完成。我举一个小例子,你可以从中猜出你的错误

    public class Demo
    {
       int x;
       public static void main(String args[])
       {
           Demo d[] = new Demo[2];
           d[0] = new Demo();
           d[1] = new Demo();
    
           d[0].x = 100;
           d[1].x = 200;
    
           System.out.println(d[0].x); 
           System.out.println(d[1].x); 
       }
    }
    Many people get wrong concept in the following code.
           Demo d[] = new Demo[2];
    
    You think a Demo array of 2 elements (with two Demo objects) with object d is created.  
    It is wrong.  Infact, two reference variables of type Demo are created.  The two    
    reference variables are to be converted int objects before they are used as follows.
           d[0] = new Demo();
           d[1] = new Demo();
    
    With the above code, d[0] and d[1] becomes objects.  Now check your code in these  
    lines.  
    

    您可以从

    中找到更多详细信息。您可能需要粘贴整个代码。不,我想没有人可以。这是因为你限制了你发布的内容。我们看不到循环,看不到变量的声明,我们不知道您是如何“检查”索引值的。考虑到您的问题,您应该能够构建一个完整的、可执行的“main”类,该类用不到二十行代码来演示您遇到的问题。在创建过程中,您可能会自己发现答案,如果没有,您可以形成一个更好的问题。。。它不应该是
    可用的eRoom[i].roomGuest=name
    ?我如何删除它并能够使用主参数引用构造函数?在尝试添加来宾之前,它打印得很好。第二个我调用addguest方法并再次尝试打印的是当它返回所有0和null时,我是否尝试删除无参数构造函数。。?另外,您可能想显示代码..当您再次打印时..很抱歉,您能解释一下无参数构造函数是什么意思吗?像这样..新公寓()…不带参数的构造函数..因此您只有一个将值作为参数的构造函数..问题是..您正在打印一个由此无参数构造函数创建的对象..如果删除它,您将了解问题所在。如果删除它,我的主类上会出现一个错误,抱怨它没有参数参数。
    public class Demo
    {
       int x;
       public static void main(String args[])
       {
           Demo d[] = new Demo[2];
           d[0] = new Demo();
           d[1] = new Demo();
    
           d[0].x = 100;
           d[1].x = 200;
    
           System.out.println(d[0].x); 
           System.out.println(d[1].x); 
       }
    }
    Many people get wrong concept in the following code.
           Demo d[] = new Demo[2];
    
    You think a Demo array of 2 elements (with two Demo objects) with object d is created.  
    It is wrong.  Infact, two reference variables of type Demo are created.  The two    
    reference variables are to be converted int objects before they are used as follows.
           d[0] = new Demo();
           d[1] = new Demo();
    
    With the above code, d[0] and d[1] becomes objects.  Now check your code in these  
    lines.