输出以前读取的22行-Java 我试图从一个文件中读取,当一个空白行被读取时,它将在空白行之前输出22行。例如,如果程序在第44行的第一个空行上读取所有行,那么它将打印第22行
目前,我让它工作,以便它读取输入并将其存储到arrayList中,然后输出它。我想知道做这件事最有效的方法是什么?我还试图确保一次存储的行数不超过23行。arraylist是否适合此应用程序的数据结构输出以前读取的22行-Java 我试图从一个文件中读取,当一个空白行被读取时,它将在空白行之前输出22行。例如,如果程序在第44行的第一个空行上读取所有行,那么它将打印第22行,java,data-structures,arraylist,Java,Data Structures,Arraylist,目前,我让它工作,以便它读取输入并将其存储到arrayList中,然后输出它。我想知道做这件事最有效的方法是什么?我还试图确保一次存储的行数不超过23行。arraylist是否适合此应用程序的数据结构 public static void test(BufferedReader r, PrintWriter w) throws IOException { ArrayList<String> s = new ArrayList<String>(); St
public static void test(BufferedReader r, PrintWriter w) throws IOException {
ArrayList<String> s = new ArrayList<String>();
String line;
int n = 0;
while ((line = r.readLine()) != null) {
s.add(line);
n++;
}
Iterator<String> i = s.iterator();
while (i.hasNext()) {
w.println(i.next());
}
}
公共静态无效测试(BufferedReader r、PrintWriter w)引发IOException{
ArrayList s=新的ArrayList();
弦线;
int n=0;
而((line=r.readLine())!=null){
s、 添加(行);
n++;
}
迭代器i=s.Iterator();
while(i.hasNext()){
w、 println(i.next());
}
}
感谢您的任何建议/输入您可以使用大小为22的简单的
字符串[]
,并执行插入和“get”模22。您的代码应该如下所示:
public static void test(BufferedReader r, PrintWriter w) throws IOException {
String[] prev22 = new String[22];
String line;
int n = 0;
while ((line = r.readLine()) != null) {
prev22[n % 22] = line;
if(line.trim() == null){
w.println(prev22[n-22 % 22]);
}
else{
w.println(line);
}
n++;
}
}
ArrayList有点过分了。因为您确切地知道行的总数,所以可以使用字符串数组 但是,您必须保留关于当前“开始指针”的信息(如果您希望保持顺序并以有效的循环方式使用它) 示例:使用自定义圆形数组
public class CircularStringArray {
private int currenInsertIndex = 0;
private String[] array = new String[22];
public void addString(String element)
{
array[currenInsertIndex++] = element;
currenInsertIndex = currenInsertIndex % array.length;
}
public String printStrings()
{
String result = "";
for(int i=currenInsertIndex; i<array.length; i++)
result+=i+")"+array[i]+"\n";
for(int i=0; i<currenInsertIndex; i++)
result+=i+")"+array[i]+"\n";
return result;
}
public static void main(String args[])
{
CircularStringArray test = new CircularStringArray();
for (int i=0; i<50; i++)
test.addString(new String(new char[]{(char)i}));
System.out.println(test.printStrings());
}
}
公共类通告{
私有int currenisertindex=0;
私有字符串[]数组=新字符串[22];
公共void addString(字符串元素)
{
数组[CurreniserTindex++]=元素;
CurreniserTindex=CurreniserTindex%array.length;
}
公共字符串printStrings()
{
字符串结果=”;
对于(int i=CurrnReutsType;i),ALAYLIST将存储所有行,直到空白。您可以使用大小为22的字符串的<强>队列< /强>。队列的队列和队列操作保持恒定的O(1)时间,并且内存使用也将是最小的
< P>这应该工作
public static void test(BufferedReader r, PrintWriter w) throws IOException {
String[] s = new String[22];
String line;
int n = 0;
while ((line = r.readLine()) != null) {
s[n] = line;
//This will take n to 0 when n is 21 i.e. last array position
n = (n+1) % 22;
}
int last = n;
//Will print it in the same order in which those lines were read.
do{
w.println(s[n]);
n = (n+1) % 22;
} while(n != last);
}
有没有我可以使用的数据结构?如果现有DS能够有效地解决问题,为什么要使用nes数据结构?有没有我可以使用的数据结构?@choloboy你不需要任何特殊的数据结构,一个长度为22的简单字符串数组就可以了。任何类型的循环数组数据结构都足够了。大多数情况下t答案显示了一个快速的实现。也有在线实现,例如,为什么不为printStrings()
方法使用此%SIZE
逻辑呢?很好的一点。尽管我怀疑在效率方面,这是更有效的,因为使用模将在循环中产生额外的成本。