java - 如何声明数组类型类

标签 java arrays initialization

我正在学习数据结构类(class),我在本章末尾有一个无法解决的问题。

class WeatherRecType {
    int AvgHiTemp;
    int AvgLoTemp;
    float ActualRain;
    float RecordRain;
}

问题:

Declare a one-dimensional array, WeatherListType of WeatherRecType components, contains ten elements.

答案:

WeatherRecType WeatherListType[] = new WeatherRecType[10];
<小时/>

问题:

Assign the value 1.05 to the ActualRain field of the seventh record in WeatherListType.

答案:???

如何将值分配给数组 WeatherListType 字段 7 中的 ActualRain

我尝试了这样的方法,但是不起作用: Problem Image

<小时/>

问题已经解决了,感谢所有与我分享知识的人。 这是正确的答案: Correct Answer Image

<小时/>

我认为我可以将值初始化为ActualRain,而无需再次声明第7个单元格,换句话说,无需编写:WeatherListType[6] = new WeatherRecType();

WeatherRecType[] WeatherListType = new WeatherRecType[10];
WeatherListType[6].ActualRain = 10;

最佳答案

在大多数基本编程语言(C、Java 等)中,数组的第七条记录会转换为索引 6,因为计数从索引 0 开始。因此您需要:

WeatherListType[6] = new WeatherRecType();
WeatherListType[6].ActualRain = 1.05f;

但是由于这是您所指的数据结构类(class),因此如果您谈论的是伪代码或其他内容,则 7 可能会起作用。

关于java - 如何声明数组类型类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38479347/

相关文章:

java - 如何在不依赖的情况下从不同项目中的包中获取所有类?

java - 如何提高pdf中嵌入的签名图像的质量

java - 重新创建数组

javascript - 如何按副标题和标题(而不是仅按其中之一)进行筛选?

arrays - 使用 go 中的范围选择 2D slice 的 2D 子 slice

java - 最终变量首先初始化

java - 同一个生成的.JAR文件在不同的电脑上是不同的

将图像显示到 JFrame 时出现 java.lang.NullPointerException

scala - Scala 中类的初始化是如何工作的?

ios - 创建 UIViewController 时什么时候使用 init()?