, поэтому у меня есть этот список ссылок, полный имен. Пользователь будет искать первую букву имени и будет распечатывать узлы, имена которых начинаются с букв. Когда я бегу, я думал, что не получаю ответа. Хотя если я вставляю некоторые строки печати в циклы, я их возвращаю.Распечатайте раздел связанного списка
Здесь:
public String printSection(){
LinkedListNode current = front;
String searchedLetter;
int i = 0;
String retSec = "The nodes in the list are:\n";
//Get the input of the name being removed
Scanner s = new Scanner(System.in);
System.out.println("Enter the first letter of the section you would like to print out");
searchedLetter = s.nextLine();
//while current is not null
while(current != null){
//if the data in current starts with the letter entered for searchedLetter
if(current.getData().startsWith(searchedLetter)){
//if(current.getData().substring(0,1) == searchedLetter){
//Increment the number of the node
i++;
//Print the node(s)
retSec += "Node " + i + " is: " + current.getData() + "\n";
//Traverse the list
current = current.getNext();
System.out.println("You made it here");
}
}
return retSec;
}
}
Вот оно: (Новый метод работы)
public void printSection(){
LinkedListNode current = front;
String searchedLetter;
int i = 0;
//Get the input of the name being removed
Scanner s = new Scanner(System.in);
System.out.println("Enter the first letter of the section you would like to print out");
searchedLetter = s.nextLine();
//while current is not null
while(current != null){
//if the data in current starts with the letter entered for searchedLetter
if(current.getData().startsWith(searchedLetter)){
//Increment the number of the node
i++;
//Print the node
System.out.println("Node " + i + " is: " + current.getData());
}
//Traverse the list
current = current.getNext();
}
}
Проблема может быть вашей декларации о текущем состоянии 'front', поэтому сначала убедитесь, что все в порядке. Затем посмотрите и посмотрите, действительно ли этот цикл while while. Похоже, вам нужен только первый, чтобы проверить, не дошли ли вы до конца связанного списка. –
Вы можете преобразовать свой список в Set, а затем использовать метод подмножества. Проверьте этот ответ: http://stackoverflow.com/questions/7228350/searching-for-a-record-in-a-treeset-on-the-fly –