java - java - 读取输入行并输出重复项
问题描述
因此,我正在学习阅读文本文件等,并且我正在尝试制作一个程序,该程序一次读取一行并输出当前行,当且仅当它小于迄今为止读取的任何其他行时。较小的是相对于字符串的通常顺序,由 String.compareTo() 定义。
当我尝试运行我的代码时,我收到错误“列表无法解析为类型”和“ArrayList 无法解析为类型”。我很困惑为什么在我的程序中出现这个错误,并且想知道是否还有其他我应该使用的东西?
package comp;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashSet;
public class Part2 {
public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
List<String> allStrings = new ArrayList<>();
String line;
String shortest = allStrings.get(0);
while((line = r.readLine()) != null) {
for(String s: allStrings) {
if(s.length()>shortest.length()) {
shortest = s;
line = shortest;
}
}
allStrings.add(line);
for (String text: allStrings) {
w.println(text);
}
}
}
public static void main(String[] args) {
try {
BufferedReader r;
PrintWriter w;
if (args.length == 0) {
r = new BufferedReader(new InputStreamReader(System.in));
w = new PrintWriter(System.out);
} else if (args.length == 1) {
r = new BufferedReader(new FileReader(args[0]));
w = new PrintWriter(System.out);
} else {
r = new BufferedReader(new FileReader(args[0]));
w = new PrintWriter(new FileWriter(args[1]));
}
long start = System.nanoTime();
doIt(r, w);
w.flush();
long stop = System.nanoTime();
System.out.println("Execution time: " + 10e-9 * (stop-start));
} catch (IOException e) {
System.err.println(e);
System.exit(-1);
}
}
}
解决方案
您可以只使用整数作为最小长度:
public class Part2 {
public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
List<String> allStrings = new ArrayList<String>();
String line;
int minLength = 0;
while ((line = r.readLine()) != null) {
if (minLength == 0 || minLength > line.length()) {
allStrings.add(line);
minLength = line.length();
}
}
for (String text : allStrings) {
w.println(text);
}
}
public static void main(String[] args) {
try {
BufferedReader r;
PrintWriter w;
if (args.length == 0) {
r = new BufferedReader(new InputStreamReader(System.in));
w = new PrintWriter(System.out);
} else if (args.length == 1) {
r = new BufferedReader(new FileReader(args[0]));
w = new PrintWriter(System.out);
} else {
r = new BufferedReader(new FileReader(args[0]));
w = new PrintWriter(new FileWriter(args[1]));
}
long start = System.nanoTime();
doIt(r, w);
w.flush();
long stop = System.nanoTime();
System.out.println("Execution time: " + 10e-9 * (stop - start));
} catch (IOException e) {
System.err.println(e);
System.exit(-1);
}
}
}
输入文件:
1111111
222222222
3333333333
444
输出:
1111111
444
您的代码也有以下错误:
List<String> allStrings = new ArrayList<>();
// It throws IndexOutOfBoundsException!!!
String shortest = allStrings.get(0);
推荐阅读
- java - 如何将对象列表转换为Java中的整数列表?
- google-cloud-platform - 如何识别 cloudbuild 子任务
- powerbi - Power Query 数据加载:大型数据集导致“未找到表达式.错误列”
- javascript - Reactstrap Collapse 在 React 组件中不起作用
- jquery - 如何防止双击键码9,即在jquery中按tab键
- javascript - 使用Mongo find javascript时如何使用部分匹配?
- sql - Oracle SQL 内连接不返回具有空值的记录
- python - PYTHON - 如何根据分隔符将一列分成两列
- operators - 'true and (true or false) and false' 的答案应该是什么?
- maven - 在 Mac 中为 Catolina 或更高版本设置 maven (mvn) 的环境变量路径