从用户获取字符串输入并在数组中搜索它

时间:2014-11-30 22:22:13

标签: java data-structures hash hashmap

我需要从用户那里获取String输入并在某个数组中搜索它然后我需要订购结果,如

  1. 最相似的事情
  2. ....
  3. ......
  4. 用户的输入就像“the mos”一样,它会显示结果。

    这是我为它写的方法,但我无法完成它: (这是一些功课,我需要使用自编的数据结构,我需要使用hashmap来创建电话簿。如果你需要我的其他课程,我可以更新问题。)

      public static void searchByName()
            {
                System.out.println("Name: ");
                String nameSearch = input.next();
                for(int i=0; i < contactList.bucket.length; i++)
                {
                    Iterator itr = (LinkedListIterator<HashEntry<Integer,Person>>) contactList.bucket[i].iterator(); 
                    while(itr.hasNext()){
                    HashEntry<Integer,Person> he = (HashEntry<Integer, Person>) itr.next();
                    int similarity = nameSearch.compareTo(he.getValue().getName());
                    HashEntry<Integer,String>[] similarArray = null;
                    for(int x=0; x < contactList.bucket[i].size; x++)
                    {
                        similarArray[x] = new HashEntry(similarity, he.getValue().getName()); 
                    }//end of for loop
    
                    }//end of while loop
    
                }//end of for loop
    
            }//end of searchByName
    

1 个答案:

答案 0 :(得分:0)

你必须考虑如何判断哪些元素是相似的&#34;类似的&#34;用户的输入。

来到我的脑海:

  • 包含输入中的1个或所有单词的元素。
  • 包含整条链的元素。
  • 包含所有字母的元素,不关心订单。

当你知道你想要什么算法时,实现它应该很容易,但你首先需要在编写任何代码之前问自己这些问题

相关问题