我想搜索鏈接列表的匹配,但我總是變得錯誤。這裏是我的代碼:搜索鏈接列表匹配
名單充滿INFILE是
try
{
BufferedReader infile =
new BufferedReader(new FileReader("C:\\videoDat.txt")); .....
public static void createVideoList(BufferedReader infile,
VideoList videoList)
throws IOException
{
String title;
String star1;
String star2;
String producer;
String director;
String productionCo;
int InStock;
title = infile.readLine();
//If the title exists then there is the rest
while ((title=infile.readLine()) != null) {
// Fill Linked list
star1=infile.readLine();
star2=infile.readLine();
producer=infile.readLine();
director=infile.readLine();
productionCo=infile.readLine();
InStock=infile.read();
VideoElement MovieObject=new VideoElement();
MovieObject.setVideoInfo(title,star1,star2,producer,
director,productionCo,InStock);
videoList.addToList(MovieObject);
title=infile.readLine();
}//end while
}//end createVideoList
搜索使用equals方法:
public boolean searchVideoList(String title)
{
for(VideoElement x:VideoList)
{
if(x.equals(title))
{
return true;
}
}
return false;
}//end searchVideoList
從main方法調用搜索:
System.out.print("Enter the title: ");
title = in.readLine();
System.out.println();
if(videoList.searchVideoList(title)==true)
System.out.println("Title found.");
else
System.out.println("Video not in store.");
break;
對不起我沒有連意識到它正在使用VideoElement的overided equals方法,它是由老師爲我們提供的:
public boolean equals(DataElement otherElement)
{
VideoElement temp = (VideoElement) otherElement;
return (videoTitle.compareTo(temp.videoTitle) == 0);
}
我總是看不到視頻,因爲searchvideolist總是在最後返回false。但如果我刪除它告訴我,我需要返回的東西..怎麼辦..做什麼..
你應該有在這裏兩個反斜槓: 嘗試{BufferedReader中的infile =新的BufferedReader(新的FileReader( 「C:\ videoDat.txt」)); 。 – JZares 2012-02-13 05:35:46
我其實不知道爲什麼或者我怎麼沒有複製它們.. – Sackling 2012-02-13 05:38:44
在上面的代碼中,您正在使用VideoElement.equals方法。所以問題是:(1)你是否重寫了equals方法? (2.)你爲什麼不在這裏使用比較器? – NiranjanBhat 2012-02-13 05:39:00