我正在学习数据结构类(class),我在本章末尾有一个无法解决的问题。
class WeatherRecType {
int AvgHiTemp;
int AvgLoTemp;
float ActualRain;
float RecordRain;
}
问题:
Declare a one-dimensional array,
WeatherListType
ofWeatherRecType
components, contains ten elements.
答案:
WeatherRecType WeatherListType[] = new WeatherRecType[10];
<小时/>
问题:
Assign the value 1.05 to the
ActualRain
field of the seventh record inWeatherListType
.
答案:???
如何将值分配给数组 WeatherListType
字段 7 中的 ActualRain
?
我尝试了这样的方法,但是不起作用:
<小时/>问题已经解决了,感谢所有与我分享知识的人。 这是正确的答案:
<小时/>我认为我可以将值初始化为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/