从文件输入(查找链接列表的第n个元素) [英] Input from a file (Find the nth element for the Linked List)
问题描述
但是我必须从文件中读取一个输入并从列表中输出第n个元素
例如
输入看起来像这样
abcd 4
efgh 2
输出会像
a
g
据说第一个参数是一个路径包含一系列空格分隔字符的文件名,后跟一个表示列表中索引的整数(1为基础),每行一个
我不知道该怎么走对这个?我会首先读取文件并将每行存储在列表中?
是的,您应该逐行读取文件,并将每行存储在 List<串GT;
。
然后,只要这样做:
String line; //使用列表中的迭代器来获取每行
String [] elements = line.trim()。split(\\s);
char [] chars = new char [elements.length - 1];
int index = Integer.parseInt(elements [elements.length - 1]);
for(i = 0; i< elements.length - 1; i ++)
char [i] = elements [i] .charAt(0);
现在你有一个数组中的字符了......而且你说你已经把剩下的东西弄清楚了。
The problem is to find nth element in a linked list, i have the problem figured out on finding the element.
But i have to read an input from a file and output the nth element from the list
For example
Input would look like this
a b c d 4
e f g h 2
Output would like
a
g
It is stated "The first argument will be a path to a filename containing a series of space delimited characters followed by an integer representing a index into the list (1 based), one per line"
I am not sure how i would go about this? Would i first the read the file and store each line in a List?
Yes, you should read the file line by line, and store each line in a List<String>
.
Then, simply do this:
String line; // use an iterator on the list to get each line
String[] elements = line.trim().split("\\s");
char[] chars = new char[elements.length - 1];
int index = Integer.parseInt(elements[elements.length - 1]);
for (i = 0; i < elements.length - 1; i++)
char[i] = elements[i].charAt(0);
Now you have the characters in an array ... and you said you already have the rest figured out.
这篇关于从文件输入(查找链接列表的第n个元素)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!