我正在尝试从TreeMap中的单个键检索多个值.我们的想法是每个密钥都链接到多个值,并且应该是可搜索的.现在我遇到的麻烦是,当我只能从密钥中获取一个值时.
键是一个String,值是一个名为Song的自定义对象.歌曲包含多个元素.目标是从每首歌曲中逐字逐句提取歌词,并将每个单词用作关键词.然后键将链接到包含键的每个乐曲(值).
我一直在搜索StackOverFlow和网络上的提示,我已经看过一些,但没有任何直接解决我的绊脚石.我看到的一个想法是将值更改为某种数组或列表.当我的大脑恢复精神时,明天我可能会尝试.
无论如何,提前感谢任何提示和建议.是的,这是家庭作业.不,我没有标记是因为我被告知家庭作业标签不再常用.
码:
public class SearchByLyricsWords {
private static Song[] songs;
private static TreeMap<String, Song> lyricsTreeMap = new TreeMap<String, Song>();
private static TreeSet<String> wordsToIgnoreTree = new TreeSet<String>();
private static File wordsToIgnoreInput = new File("ignore.txt");
private static String wordsToIgnoreString;
private static String[] wordsToIgnoreArray;
private Song[] searchResults; // holds the results of the search
private ArrayList<Song> searchList = new ArrayList<Song>();
public SearchByLyricsWords(SongCollection sc) throws FileNotFoundException {
// Create a string out of the ignore.txt file
Scanner scanInputFile = new Scanner(wordsToIgnoreInput); …Run Code Online (Sandbox Code Playgroud) 我在编程时仍然非常环保,并且遇到了Java中数组上二进制搜索语法的问题.我试图调用存在于从我使用二进制搜索类单独的一类比较方法(重载"比较"方法).本质上我的目标是搜索数组存储在只有一个变量构成数组的对象.没有比较器,我没有成功这样做,因为我创建了一个"虚拟"对象,只保留搜索所需的标准作为密钥.
这是我的二进制搜索代码:
Song searchSong = new Song(artistInput, artistInput, artistInput);
int search = Arrays.binarySearch(songs, searchSong, new compare<Song>());
Run Code Online (Sandbox Code Playgroud)
这是我的重载比较器的代码,同样在一个单独的类中:
public int compare (Song firstSong, Song secondSong) {
return firstSong.getArtist().compareTo(secondSong.getArtist());
}
Run Code Online (Sandbox Code Playgroud)
我确信这只是一件我想念的简单事,但我还没有找到答案.我感谢任何帮助,如果需要更多细节,请告诉我.我知道二进制搜索的代码不能用它的当前形式.
可能重复:
Java字符串比较?
我对Java很新,并对我的if/else语句无法执行的原因有疑问.该方法的目标是基于从文本文件读入的符号加载2D阵列.符号将转换为数字以填充数组.据我所知,用于确定if或if/else语句是否应该执行的布尔逻辑是合理的,但它们都没有.谢谢您的帮助.
码:
public void loadText(Scanner sc) {
for (int row = 1; row <= grid.length; row++) {
for (int col = 1; col <= grid[row].length; col++) {
String x = sc.next();
if ((x == " ") && (row <= 10)) {
grid[row][col] = 0;
} else if ((x == "*") && (row <= 10)) {
grid[row][col] = 1;
} else if ((x == "$") && (row <= 10)) {
Arrays.fill(grid[row], 0);
} else if (row >= 11) { …Run Code Online (Sandbox Code Playgroud)