Java,将数据写入文件并拆分为多行
Java, write data in file and split on multiple lines
我有一个双向链表,在该列表中我必须生成 100 个随机值。我已经这样做了。然后,我需要将双向链表的值存储到一个文本文件中。我也这样做了。
最后,我必须格式化我的文档,例如在线有 5 个值:
提示:我会将这些行写成随机值,顺序无关紧要,我使用冒泡排序对它们进行排序,然后将它们反转,但我只需要知道如何像这样放置这些值:
1 14 23 4 55
6 39 91 1 4
etc.
我也尝试覆盖 toString 并在那里添加了 "for" 和 "if",但结果失败了。这是我的代码:
DLL ran = new DLL(); //this is my class named DLL
for(int i=0; i<100; i++)
{
Integer n = new Integer((int) (Math.random()*100));
ran.startValue(n); //this is my add function, to add elements in list
System.out.print(n+" ");
}
BufferedWriter out = new BufferedWriter(new FileWriter("out.txt"));
out.write(ran.toString());
out.flush();
out.close();
如果有get(int i)
函数,我会这样写toString
:
public String toString(){
String ans = "";
for(int i = 0; i < this.length; i++){
ans += this.get(i) + " ";
if(i % 5 == 0)
ans += "\n";
}
return ans;
}
这就是你写 get(int i)
:
的方式
public int get(int index){
Node head = this.head;
for(int i = 0; i < index; i++){
head = head.next;
}
return head.getData();
}
如果只是格式问题,就用这个。
for(int i=0; i<100; i++)
{
Integer n = new Integer((int) (Math.random()*100));
ran.startValue(n); //this is my add function, to add elements in list
System.out.print(n+" ");
if(i%5==0)System.out.println("");
}
但我同意@Hovercraft 的观点,你应该使用PrintWriter,它还默认提供换行打印方法。无需在此处重写 toString()
使用您提供的 Node 和 DLL 函数,您可能可以在 DLL 中执行类似的操作 class:
public String toString(){
String ans = "";
Node head = this.head;
for(int i = 0; i < this.size; i++){
ans += head.getData() + " ";
if(i % 5 == 0)
ans += "\n";
head = head.next;
}
return ans;
}
这在 while 循环中同样容易编写如下:
public String toString(){
String ans = "";
Node head = this.head;
int i = 1;
while(head != null){
ans += head.getData() + " ";
if(i % 5 == 0)
ans += "\n";
head = head.next;
i++;
}
return ans;
}
我有一个双向链表,在该列表中我必须生成 100 个随机值。我已经这样做了。然后,我需要将双向链表的值存储到一个文本文件中。我也这样做了。 最后,我必须格式化我的文档,例如在线有 5 个值:
提示:我会将这些行写成随机值,顺序无关紧要,我使用冒泡排序对它们进行排序,然后将它们反转,但我只需要知道如何像这样放置这些值:
1 14 23 4 55
6 39 91 1 4
etc.
我也尝试覆盖 toString 并在那里添加了 "for" 和 "if",但结果失败了。这是我的代码:
DLL ran = new DLL(); //this is my class named DLL
for(int i=0; i<100; i++)
{
Integer n = new Integer((int) (Math.random()*100));
ran.startValue(n); //this is my add function, to add elements in list
System.out.print(n+" ");
}
BufferedWriter out = new BufferedWriter(new FileWriter("out.txt"));
out.write(ran.toString());
out.flush();
out.close();
如果有get(int i)
函数,我会这样写toString
:
public String toString(){
String ans = "";
for(int i = 0; i < this.length; i++){
ans += this.get(i) + " ";
if(i % 5 == 0)
ans += "\n";
}
return ans;
}
这就是你写 get(int i)
:
public int get(int index){
Node head = this.head;
for(int i = 0; i < index; i++){
head = head.next;
}
return head.getData();
}
如果只是格式问题,就用这个。
for(int i=0; i<100; i++)
{
Integer n = new Integer((int) (Math.random()*100));
ran.startValue(n); //this is my add function, to add elements in list
System.out.print(n+" ");
if(i%5==0)System.out.println("");
}
但我同意@Hovercraft 的观点,你应该使用PrintWriter,它还默认提供换行打印方法。无需在此处重写 toString()
使用您提供的 Node 和 DLL 函数,您可能可以在 DLL 中执行类似的操作 class:
public String toString(){
String ans = "";
Node head = this.head;
for(int i = 0; i < this.size; i++){
ans += head.getData() + " ";
if(i % 5 == 0)
ans += "\n";
head = head.next;
}
return ans;
}
这在 while 循环中同样容易编写如下:
public String toString(){
String ans = "";
Node head = this.head;
int i = 1;
while(head != null){
ans += head.getData() + " ";
if(i % 5 == 0)
ans += "\n";
head = head.next;
i++;
}
return ans;
}