我从文件中读取单词并将其置于双链表中我使用.equals()方法检查单词是否在双链表中并且方法工作正常但是当我使用compareto()方法时对它显示这些例外的词进行排序: -
Exception in thread "main" java.lang.NullPointerException
at DoubleLinkedList.insert(DoubleLinkedList.java:49)
at FileSystemIndexer.indexFile(FileSystemIndexer.java:88)
at FileSystemIndexer.IndexDirectory(FileSystemIndexer.java:18)
at TestPhase1.main(TestPhase1.java:15)
方法: -
import java.io.*;
import java.util.*;
public class FileSystemIndexer {
private DoubleLinkedList<Index> invertedIndex;
public FileSystemIndexer (){
invertedIndex=new DoubleLinkedList<Index>();
}
public void IndexDirectory(File dir){
File[] files =dir.listFiles();
for(int i=0 ; i<files.length; i++){
indexFile(files[i]); //FileSystemIndexer.java:18
}
}
public void indexFile(File file ){
Scanner input = null;
try{
input = new Scanner(file);
}
catch(Exception e) {
System.out.println(e);
}
String c;
input.useDelimiter(",|\\.|:|\\s+");
while(input.hasNext()) {
c=input.next();
if(c.equals("")){
c=input.next();
}else{
//System.out.println("here: " + c);
if(invertedIndex.empty()){
invertedIndex.insert( new Index(c,file));
}else{
invertedIndex.FindFirst();
while(!invertedIndex.last()){
if(invertedIndex.retrieve().getWord().compareTo(c)==-1){
System.out.println("here: " + c);
invertedIndex.FindPrevious();
invertedIndex.insert(new Index(c,file));
}else if(invertedIndex.retrieve().getWord().compareTo(c)==1){
System.out.println("here: " + c);
invertedIndex.findNext();
invertedIndex.insert(new Index(c,file)); //FileSystemIndexer.java:88
}
else if(invertedIndex.retrieve().getWord().compareTo(c)==0){
System.out.println("here: " + c);
invertedIndex.retrieve().getFiles().insert(file);
}
/*
if(invertedIndex.retrieve().getWord().equals(c)){
invertedIndex.retrieve().getFiles().insert(file);
}
invertedIndex.findNext();
}
if(invertedIndex.retrieve().getWord().equals(c)){
invertedIndex.retrieve().getFiles().insert(file);
}else {
Index i=new Index(c,file);
invertedIndex.insert(i);
}
}/**/
} if(invertedIndex.retrieve().getWord().compareTo(c)==-1){
invertedIndex.FindPrevious();
invertedIndex.insert(new Index(c,file));
}else if(invertedIndex.retrieve().getWord().compareTo(c)==1){
invertedIndex.findNext();
invertedIndex.insert(new Index(c,file));
}
else if(invertedIndex.retrieve().getWord().compareTo(c)==0){
invertedIndex.retrieve().getFiles().insert(file);
}
}
}
}
input.close();
//}
//catch (Exception e){
// System.out.println(e);
//}
}
双链表中的插入方法: -
public void insert(T val ){
Node<T> tmp= new Node<T> (val);
if(empty()){
current=head=tmp;
}else{
tmp.next=current.next; //DoubleLinkedList.java:49
tmp.previous=current;
if(current.next !=null)
current.next.previous=tmp;
current.next=tmp;
current=tmp;
}
}
我对双链表的实现:
public class DoubleLinkedList<T> {
private Node<T> head;
private Node<T> current ;
public DoubleLinkedList(){
head=current=null;
}
public boolean empty(){
return head==null;
}
public boolean last(){
return current.next==null;
}
public boolean first(){
return current.previous==null;
}
public boolean full(){
return false ;
}
public void FindFirst(){
current=head;
}
public void findNext(){
current=current.next;
}
public void FindPrevious(){
current=current.previous;
}
public T retrieve(){
return current.data;
}
public void update(T val ){
current.data=val;
}
public void insert(T val ){
Node<T> tmp= new Node<T> (val);
if(empty()){
current=head=tmp;
}else{
tmp.next=current.next;
tmp.previous=current;
if(current.next !=null)
current.next.previous=tmp;
current.next=tmp;
current=tmp;
}/*
if(empty()){
current=head=tmp;
}else{
tmp.previous = current;
tmp.next = current.next;
if(current.next != null)
current.next.previous = tmp;
current.next = tmp;
}*/
}
public void remove(){
if(current==head){
head=head.next;
if(head!=null)
head.previous=null;
}else{
current.previous.next=current.next;
if(current.next !=null)
current.next.previous=current.previous;
}
if(current.next==null)
current=head;
else
current=current.next ;
}
public void remove2(){
if(current==head)
head=head.next;
else
current.previous.next=current.next;
if(current.next !=null)
current.next.previous=current.previous;
if(current.next==null)
current=head;
else
current=current.next;
}
}
答案 0 :(得分:0)
如果您的插入意图是在列表末尾添加一个新节点,那么插入代码必须更改如下...
public void insert(T val ){
Node<T> tmp= new Node<T> (val);
if(empty()){
current=head=tmp;
}else{
tmp.previous=current;
current.next=tmp;
current=tmp;
}
}
但是如果你想在中间某处插入Val,那么你的代码就可以了,但是你无法将当前设置为tmp。因此必须从代码中删除最后一行。
public void insert(T val ){
Node<T> tmp= new Node<T> (val);
if(empty()){
current=head=tmp;
}else{
tmp.previous = current;
tmp.next = current.next;
if(current.next != null)
current.next.previous = tmp;
current.next = tmp
}
}
如果它不起作用,那么你必须提供DoubleLinkedList的实现,否则就不可能提供任何解决方案。