我有一个评估要做,而且我不擅长编程。我可以搜索算法并查看它们是如何完成的,但在我的情况下,我已经订购了字符串集合,不知怎的,我必须使用get方法。 我有两个不能改变的类:
public class SearchTest {
/**
* Test program for the Search class.
* Put whatever tests you like in the body of the method.
* @param args the command line arguments
* @throws java.io.IOException of error reading the input
*/
public static void main(String[] args) throws IOException {
// Don't change this line
final Search search = new Search();
// You can set this to any of the text files in the data folder
final FileStrings strings = new FileStrings("data/small.txt");
// add your tests here
System.out.println(search.longestWord(strings));
}
}
和
public class FileStrings implements StringList {
/** Underlying list of elements */
private final ArrayList<String> elements;
/** Number of calls to get() since the last call to resetCount() */
private int count;
/**
* Create a list containing the lines of a text file.
* @param fileName name of a text file of strings, in order
* @throws java.io.IOException on input error
*/
public FileStrings(String fileName) throws IOException {
elements = new ArrayList<>();
try (BufferedReader input = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = input.readLine()) != null) {
elements.add(line);
}
}
count = 0;
}
/**
* Returns the number of elements in this list.
* This method takes constant time.
* @return the number of elements in this list
*/
@Override
public int size() {
return elements.size();
}
/**
* Returns the element at the specified position in this list.
* This method takes constant time.
* @param i position in the list, between 0 and size()-1
* @return the element at the position i
*/
@Override
public String get(int i) {
count++;
return elements.get(i);
}
/**
* Reset the count field.
*/
public void resetCount() {
count = 0;
}
/**
* Getter for count.
* @return number of calls to get() since the last resetCount()
*/
public int getCount() {
return count;
}
}
我的第一个任务是找到给定文件列表中最长的单词。 这是我的尝试(我知道它错了,但不能遵循示例,因为我看到的解决方案直接使用数组):
public class Search {
/**
* Returns the index of the longest string in the list.
* If there are several string of this length, the
* indexed returned is the that of the first.
* @param a list of strings, in ascending order
* @return position of an entry with the longest string in the list
*/
public int longestWord(StringList a) {
int i=0;
int longestWord=0;
String nextWord=a.get(i+1);
String previousWord=a.get(i);
while (i < a.size() ) {
if (nextWord.length()>previousWord.length()){
longestWord = i;
}
i = i + 1;
}
return longestWord;
}
结果应该是&#34; 14&#34;,世界&#34;因为&#34;是第15个字,是最长的。我希望你能帮助我! list of words
答案 0 :(得分:0)
public class Search {
/**
* Returns the index of the longest string in the list.
* If there are several string of this length, the
* indexed returned is the that of the first.
* @param a list of strings, in ascending order
* @return position of an entry with the longest string in the list
*/
public int longestWord(StringList a) {
int length=a.get(0).length();
int i=0;
int longestWord=0;
while (i<a.size()){
if (a.get(i).length()>length){
length=a.get(i).length();
longestWord=i;
}
i = i + 1;
}
return longestWord;
}
设法做到了:P