当前位置: 首页 > 面试题库 >

比较字符串与数组字符串和二进制搜索

微生景胜
2023-03-14
问题内容

程序从经过排序的字符串的txt文件中读取,并使用顺序的,迭代的二进制和递归的二进制存储在数组中,然后在数组中搜索位置以及查找该单词所需的迭代次数。当我尝试将数组中的单词与用户输入的单词进行比较时出现错误。不知道为什么。2)希望有人可以解释迭代二进制和递归二进制之间的区别。3)为什么需要这样做…

SearchString si = new SearchString();

程序在下面…

import ...

public class SearchString
{
public static final String TO_STOP = "-1";
public static final int NOT_FOUND = -1;
public static final int MAX_SIZE = 15000;

  public static int count1;
  public static int count2;
  public static int count3;


  public int sequentialSearch(String[] wordsArray, String word2Search)
  {
    int low = 0;
    int high = wordsArray.length - 1;
    for (int i = low; i <= high; i++)
    {
        count1++;
        if (wordsArray[i] == word2Search)
            return i;
    }
    return NOT_FOUND;
  } // end of sequentialSearch()

  public int binarySearch(String[] wordsArray, String word2Search)
  {
    int low = 0;
    int high = wordsArray.length - 1;
    while (low <= high)
    {
        int mid = (low + high)/2;
        count2++;
        if (wordsArray[mid] > word2Search){
            high = mid - 1;
        } else if (wordsArray[mid] < word2Search){
            low = mid + 1;
        } else
            return mid;
    }
    return NOT_FOUND;
  } /


  public int binarySearch2(String[] wordsArray, int low, int high, String word2Search){
    if (low > high)
        return NOT_FOUND;
    int mid = (low + high)/2;
    count3++;
    if (wordsArray[mid] > word2Search){
        return binarySearch2(wordsArray, low, mid-1, word2Search);
    } else if (wordsArray[mid] < word2Search){
        return binarySearch2(wordsArray, mid+1, high, word2Search);
    } else
        return mid;
  }



public static void main(String[] args) throws IOException
{
    Scanner keyboard = new Scanner(System.in);

    boolean wantToContinue = true;

    Scanner stringsFile = new Scanner (new File("sortedStrings.txt"));//.useDelimiter(",\\s*"); 
    List<String> words = new ArrayList<String>();

    String token1 = "";

  (stringsFile.hasNext())                           
    {     token1 = stringsFile.next();
          words.add(token1);
    }
    stringsFile.close();                                    
    String[] wordsArray = words.toArray(new String[0]);

    System.out.println(Arrays.toString(wordsArray));

    SearchString si = new SearchString();

    do {
        System.out.print("Type a word to search or -1 to stop: ");
        String word2Search = keyboard.nextLine();
        if (word2Search.equals(TO_STOP)){
            wantToContinue = false;
        } else {
            count1 = count2 = count3 = 0;
            int  index;

            index = si.sequentialSearch(wordsArray, word2Search);
            if (index == NOT_FOUND)
                System.out.println("sequentialSearch()      : " + word2Search + " is not found (comparison=" + count1 + ").");
            else
                System.out.println("sequentialSearch()      : " + word2Search + " is found in [" + index + "] (comparison=" + count1 + ").");

            index = si.binarySearch(wordsArray, word2Search);
            if (index == NOT_FOUND)
                System.out.println("iterative binarySearch(): " + word2Search + " is not found (comparison=" + count2 + ").");
            else
                System.out.println("iterative binarySearch(): " + word2Search + " is found in [" + index + "] (comparison=" + count2 + ").");

            index = si.binarySearch2(wordsArray, 0, wordsArray.length-1, word2Search);
            if (index == NOT_FOUND)
                System.out.println("recursive binarySearch(): " + word2Search + " is not found (comparison=" + count3 + ").");
            else
                System.out.println("recursive binarySearch(): " + word2Search + " is found in [" + index + "] (comparison=" + count3 + ").");
        }
    } while (wantToContinue);

}

}


问题答案:

您无法String==(或><)进行比较。您需要使用
String.compareTo



 类似资料:
  • 如何比较字符串二进制(而不是字母数字)?? Torrent规格: 键必须是字符串,并且以排序顺序出现(排序为原始字符串,而不是字母数字)。应该使用二进制比较,而不是特定于区域性的“自然”比较来比较字符串。 所以我需要按键对口供进行排序...但我没有这个规格。解释..有人吗? 更新:http://docs.oracle.com/cd/b19306_01/server.102/b14225/ch5li

  • 我已经声明了一个实现可比较接口和compareTo方法的类,使用employee ID比较两个员工。创建的类对象插入数组列表。现在,当我使用collections.sort(arrayList对象)时,它工作得很好。我对collective和comparator接口之间的比较有何不同感到困惑。我想知道如何在纯粹由数字组成的employee id字符串和其他字符串employee id之间进行比较,

  • 在“基本类型”一章中,介绍了字符串,以及使用is_binary/1函数检查它: iex> string = "hello" "hello" iex> is_binary string true 本章将学习理解:二进制串(binaries)是个啥,它怎么和字符串(strings)扯上关系的; 以及用单引号包裹的值'like this'是啥意思。 UTF-8和Unicode 字符串是UTF-8编码的二

  • 想改进这个问题吗?通过编辑这篇文章添加细节并澄清问题。 我想知道是否可以将字符串数组值与字符串进行比较,例如;我有一个字符串数组,上面有5个值我想知道哪个索引值具有a或b。我想到了一个主意,但没有成功。 基本上,如果索引0是a,则应打印“Apple”,如果是b,则应打印“ball”,依此类推。有没有办法将值或字符串数组与字符串进行比较?

  • 如何比较两个列表是否相等验证数据来自Excel工作表。我需要验证两个列表是否相同,并且列表中没有附加元素或缺少元素。我不需要对列表进行排序。打印输出CAGID Excel data=CAGID Web列表

  • 问题内容: 我听说散列(即将字符串或对象转换为数字)用于字符串等,因为比较数字比字符串更容易。如果为真,这是什么原因? 问题答案: 不一定是这种情况,但大多数时候可能是这样。 请考虑以下情况: 我想比较字符串“ apples”和“ oranges”。如果我只想确定“ apples” ==“ oranges”,我只需要比较每个字符串的第一个字符:’a’!=’o’=>“ apples”!=“ oran