gpt4 book ai didi

java - 如何从java中的Array类类型对象中检索值?

转载 作者:行者123 更新时间:2023-12-02 07:07:36 24 4
gpt4 key购买 nike

我有通过传递参数 ipadd 来调用 getSystem() 的方法,我有两个类,如下所述

SystemReport Class

public class SystemReport
{
protected System[] system;

public ComputerSystemResponse ()
{
}

public void setSystems( System[] system )
{
this.system = system;
}

public System[] getSystem()
{
return system;
}

}

系统类,其中包含我感兴趣的网站详细信息,

public class System 
{

protected String site;

public System()
{
}

public System(String site)
{
this.site=site;
}

public void setSite(String site)
{
this.site = site;
}

public String getSite()
{
return site;
}

}

不同类中的方法并尝试通过循环检索站点的值

SystemReport rep = classInstance.getNames(ipadd);
System[] test = rep.getSystem();

getNames 的返回类型

protected SystemReport getNames (ipadd)
{
SystemReport rep = new SystemReport();

return rep;
}

回答问题:
1. classInstance.getNames(ipadd) 返回类型为SystemReport
2. 重复次数 > 0

现在我想从代表那里获取网站,我尝试通过执行test.length is 0来检查长度。我缺少什么?

最佳答案

您的getNames函数返回一个新的、空白的SystemReport实例。您调用 new SystemReport() 但默认构造函数不会向列表添加任何内容,因此长度将为 0。

您需要在创建的报告上调用 setSystems 并传入您获取 System[] system 数组的位置。

关于java - 如何从java中的Array类类型对象中检索值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15913230/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com