您的程序应该读取输入文件(程序的第一个参数).第一行包含数字'N'的值,后跟多行.您可以假设输入文件格式正确,第一行的数字即'N'是有效的正整数.eg
这是我的输入:
2
Hello WorldCodeEval
快速福克斯
一个
旧金山
期望的输出应该是:
旧金山
你好世界
这是我的代码:
class Longest
{
public static void main(String args[]) throws FileNotFoundException
{
BufferedReader in = null;
List<String> myList = new ArrayList<String>();
try
{
in = new BufferedReader(new FileReader("C:\\filename.txt"));
String str;
while ((str = in.readLine()) != null)
{
if (str.length() == 0) continue;
myList.add(str);
}
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println("Contents of the ArrayList : "+myList);
System.out.println("Size of the ArrayList : "+myList.size());
String s = myList.remove(0);
System.out.println(System.getProperty("line.separator")); …Run Code Online (Sandbox Code Playgroud)