gpt4 book ai didi

java - 访问数组列表中的元素时出错

转载 作者:行者123 更新时间:2023-12-01 19:07:42 25 4
gpt4 key购买 nike

public  void dataAnalytics()
{
double sum=0;
double i=0;
double minage=20;
double maxage=20;

System.out.println("dataAnalytics for the Rural region");

for (Record ee :RList)
{
sum=sum+Double.valueOf(ee.getIncome());
i++;
}

ageComparator sc = new ageComparator();
Collections.sort(RList, sc);

maxage = (RList[0]).getAge();
minage = (RList[ records.length - 1]).getAge();


System.out.println("Average income for the Rural region is : $"+sum/i);
System.out.println("Min age for the Rural region is : "+minage);
System.out.println("Max age for the Rural region is : "+maxage);

}

我正在尝试访问数组的第一个对象和 arrayList 的最后一个对象中的变量,这将为我提供 arraylist 中的最小年龄和最大年龄。这行给了我错误::

maxage = (RList[0]).getAge();
minage = (RList[ records.length - 1]).getAge();

任何想法/

最佳答案

数组列表不是使用方括号而是使用 getter 进行索引,尝试:

RList.get(0).getAge();
Rlist.get(RList.size()-1).getAge();

请参阅此处了解详细信息。 http://docs.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html

要使用数组语法,您可以使用

Rlist.toArray()[0];

关于java - 访问数组列表中的元素时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9450426/

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