从文件输入(查找链接列表的第n个元素)

编程入门 行业动态 更新时间:2024-10-19 16:24:08
本文介绍了从文件输入(查找链接列表的第n个元素)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 问题是在链表中找到第n个元素,我找到了找到元素的问题。 但是我必须从文件中读取一个输入并从列表中输出第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个元素)

本文发布于:2023-11-30 04:33:47,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   链接   文件   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!