简体   繁体   English

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

[英]Getting String input from the user and searching it in an array

I need to get String input from the user and search it in some array and then i need to order the results like 我需要从用户那里获取String输入并在某个数组中搜索它,然后我需要对结果进行排序

  1. The most similar thing 最相似的东西
  2. .... ....
  3. ...... ......

like user's input will be like "The mos" and it will show this as result. 用户输入的内容将类似于“ The mos”,并将其显示为结果。

This one was the method i wrote for it but i couldn't complete it: (That is some homework and i need to use self-written data structures and i need to use a hashmap to create a phonebook. If you need my other classes i can update the question.) 这是我为此编写的方法,但我无法完成它:(这是一些作业,我需要使用自行编写的数据结构,并且需要使用哈希图来创建电话簿。如果需要其他课程我可以更新问题。)

  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

You have to think about how you are going to tell which elements are "similar" to the user's input. 您必须考虑如何判断哪些元素与用户的输入“相似”。

Coming to my mind : 我想到:

  • Elements containing 1 or all words from input. 输入中包含1个或所有单词的元素。
  • Elements containing whole chain. 元素包含整个链。
  • Elements containing all letters, no care about order. 包含所有字母的元素,无需关心顺序。

When you know what algorithm you want, implementing it should then be easy, but you first need to ask yourself those questions before writting any code . 当您知道想要哪种算法时,实现起来应该很容易,但是在编写任何代码之前 ,您首先需要问自己那些问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM