我想要做的是搜索带有关键字的哈希集..
我有3个班级......
在库中,我正在尝试搜索hashsets中的项目。
在Item类中我有getKeywords函数..
这是Items类......
import java.io.PrintStream;
import java.util.Collection;
import java.util.*;
class Item
{
private String title;
private String [] keywords;
public String toString()
{
String line1 = "title: " + title + "\n" + "keywords: " + Arrays.toString(keywords);
return line1;
}
public void print()
{
System.out.println(toString());
}
public Item()
{
}
public Item(String theTitle, String... theKeyword)
{
this.title = theTitle;
this.keywords = theKeyword;
}
public String getTitle()
{
return title;
}
public String [] getKeywords()
{
return keywords;
}
}
class CD extends Item
{
private String artist;
private String [] members;
// private String [] keywords;
private int number;
public CD(String theTitle, String theBand, int Snumber, String... keywords)
{
super(theTitle, keywords);
this.artist = theBand;
this.number = Snumber;
// this.keywords = keywords;
}
public void addband(String... member)
{
this.members = member;
}
public String getArtist()
{
return artist;
}
public String [] getMembers()
{
return members;
}
// public String [] getKeywords()
// {
// return keywords;
//}
public String toString()
{
return "-Music-" + "\n"
+ "band: " + artist + "\n"
+ "# songs: " + number + "\n"
+ "members: " + Arrays.toString(members)
+ "\n" + super.toString()
// + "keywords: " + Arrays.toString(keywords)
+ "\n" + "\n" ;
}
public void print()
{
System.out.println(toString());
}
}
class DVD extends Item
{
private String director;
private String [] cast;
private int scenes;
// private String [] keywords;
public DVD(String theTitle, String theDirector, int nScenes, String... keywords)
{
super(theTitle, keywords);
this.director = theDirector;
this.scenes = nScenes;
// this.keywords = keywords;
}
public void addmoviecast(String... members)
{
this.cast = members;
}
public String [] getCast()
{
return cast;
}
public String getDirector()
{
return director;
}
// public String [] getKeywords()
// {
// return keywords;
// }
public String toString()
{
return "-Movie-" + "\n"
+ "director: " + director + "\n"
+ "# scenes: " + scenes + "\n"
+ "cast: " + Arrays.toString(cast) + "\n"
+ super.toString()
// + "keywords: " + Arrays.toString(keywords)
+ "\n" + "\n" ;
}
public void print()
{
System.out.println(toString());
}
}
class Book extends Item
{
private String author;
private int pages;
public Book(String theTitle, String theAuthor, int nPages, String... keywords)
{
super(theTitle, keywords);
this.author = theAuthor;
this.pages = nPages;
// this.keywords = keywords;
}
public String getAuthor()
{
return author;
}
//public String [] getKeywords()
// {
// return keywords;
//}
public void print()
{
System.out.println(toString());
}
public String toString()
{
return "-Book-" + "\n"
+ "Author: " + author + "\n"
+ "# pages " + pages + "\n"
+ super.toString()
// + "keywords: " + Arrays.toString(keywords)
+ "\n" + "\n" ;
}
}
我希望我没有迷惑你?我需要有关itemsForKeyword(String keyword)函数的帮助..
传入的第一个关键字是“科幻小说”,我想搜索集合中的关键字并返回匹配项。
我做错了什么? 谢谢
答案 0 :(得分:1)
您需要添加以下内容:
String[] keywords = s.getKeywords();
for( String word : keywords ) {
if( word.equals( keyword ) ) {
key.add(s);
break;
}
}
您无法将字符串与字符串数组进行比较。如果您的关键字位于Set中会更好。然后你可以这样做:
if( s.getKeywords.contains( keyword ) ) {
key.add(s);
}
答案 1 :(得分:0)
getKeywords()将返回一个String数组,并进行比较以查看该数组是否等于单个String。该比较将始终返回false。