gpt4 book ai didi

java - 一个参数的方法,其数据类型为 Passenger 的 ArrayList

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

所以,我有这个方法,printStats,其参数 pList 的数据类型为 Passenger 的 ArrayList:

public static void printStats(ArrayList<Passenger> pList) {
Scanner file = new Scanner( new File( "titanic.csv" ) );
DecimalFormat percent = new DecimalFormat("#.0%");
String header = file.nextLine();
while (file.hasNextLine()){
String gender;
String survived;
int count = 0;
int males = 0;
int females = 0;
int survivedMales = 0;
int survivedFemales = 0;
for(int i=0; i<pList.length;i++) {
if (gender.equals ("M"))
{ males++;
if (survived.equals("yes"))
survivedMales ++;}
else
{
females++;
}
if (survived.equals("yes"))
survivedFemales ++;}
count++
System.out.println("Total number of passengers: " + count);
System.out.println("Total number of male passengers: " + males);
System.out.println("Total number of female passengers: " + females);
System.out.println("Total number of male passengers survived: " + survivedMales);
System.out.println("Total number of female passengers survived: " + survivedFemales);
System.out.println("Survival rate of male passengers: " + percent.format(survivedMales/males));
System.out.println("Survival rate of female passengers: " + percent.format(survivedFemales/females));
}

我在 for 循环中不断遇到语法错误,特别是在“i < pList.length”中:

for(int i=0; i<pList.length;i++)

任何帮助将不胜感激。

最佳答案

我建议你看看the documentation for ArrayList 。向下滚动时,您将看到没有名为 length 的公共(public)字段,因此 pList.length 无效。这就是 Eclipse 所提示的。相反,您会发现有一个名为 size() 的方法。 。因此,您需要将 pList.length 替换为 pList.size()

关于java - 一个参数的方法,其数据类型为 Passenger 的 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61190802/

24 4 0