gpt4 book ai didi

java - 如何打印增强型 for 循环的前 10 行

转载 作者:行者123 更新时间:2023-12-01 22:40:54 26 4
gpt4 key购买 nike

我有一个包含 1000 多个名字的文件,其中还包括性别以及有多少人拥有该名字。

示例

莎拉 F 2000

我试图打印从 for 循环创建的前 10 行,但由于某种原因,我尝试只打印最后一行 10 次。

import java.util.*;
import java.io.*;
import java.util.Collections;
public class NameYear
{
private String year;
ArrayList<OneName> oneName = new ArrayList<OneName>();

public NameYear(String year)
{
String line = "";
String Top = "";
Scanner sc = null;
try
{
sc = new Scanner(new File
("/home/mathcs/courses/cs225/koch/names/yob"+year+".txt"));
}
catch (Exception e)
{
System.out.println("Error Year should be between 1880 and 2013 not "+ year);
System.exit(1);
}

while(sc.hasNextLine())
{

// read a line from the input file via sc into line
line = sc.nextLine();



StringTokenizer stk = new StringTokenizer(line, ",");
String name = stk.nextToken();
char sex = stk.nextToken().charAt(0);
int count = Integer.parseInt(stk.nextToken());


OneName list = new OneName(name, sex, count);

oneName.add(list);
}
for (int i = 0 ; i < 10; i++)
{
System.out.println(descending());
}

public String descending()
{
String x = "";
Collections.sort(oneName, new OneNameCountCompare());
for(OneName b: oneName)
{
x = b.toString();
}
return x;

OneName 文件

public class OneName
{
private String Name;
private char Sex;
private int Count;

public OneName(String name, char sex, int count)
{
Name = name;
Sex = sex;
Count = count;

}
public String getName()
{
return Name;
}
public char getSex()
{
return Sex;
}
public int getCount()
{
return Count;
}
public void setName(String name)
{
if (name.length() < 1)
{
throw new NullPointerException("Baby name is missing");
}

Name = name;

}
private char M;
private char F;
public void setSex(char sex)
{
if( sex != M)
{
if(sex != F)
{
throw new IllegalArgumentException("Sex has to be M or F");
}
}
Sex = sex;

}
public void setCount(int count)
{
if(count < 0)
{
throw new IllegalArgumentException("Count cant be negative");
}

Count = count;

}
public String toString()
{
return String.format("%s %c %d", Name, Sex, Count);

}
}

OneNameCount

import java.util.Comparator;
import java.util.Collections;

public class OneNameCountCompare implements Comparator<OneName>
{
public int compare(OneName b1, OneName b2)
{
if(b1.getCount() <b2.getCount())
{
return 1;
}
else
{
return -1;
}
}
}

主程序

import java.io.*;
import java.util.*;

public class TopNames
{
public static void main(String args[])
{
String line = ""; // string var to hold entire line

if (args.length < 1)
{
System.out.println("\nYou forgot to put a Year on the command line.");
System.exit(1);
};
String inFile = args[0]; // file name off command line
String year = inFile;
NameYear list = new NameYear(year);

}


}

最佳答案

您的descending函数返回一个字符串,并且始终是相同的字符串(对集合进行排序后顺序中的最后一个)。无论您调用它的频率如何,如果数据没有更改,您总是会返回相同的最后一个字符串。

如果您想要排序后的前 10 个,descending需要返回 List<String>包含这 10 个:

public List<String> descending()
{
List<String> x = new ArrayList<String>(10);
Collections.sort(oneName, new OneNameCountCompare());
for(OneName b: oneName)
{
x.add(b.toString());
if (x.size() == 10) // Or don't use enhanced for, use an index instead
{
break;
}
}
return x;
}

然后在打印时,替换你的 for (int i = 0 ; i < 10; i++)循环:

for (String s : descending())
{
System.out.println(s);
}

关于java - 如何打印增强型 for 循环的前 10 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26160725/

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