java - 每次通过后如何打印剩余的数据?

标签 java binary-search

我正在为学校解决这个问题

"Write a binary search program that looks for the following 3 values 74, 35, 62 (Yes, I know...) )in the supplied data set. Your application should print out the remaining data after each pass. Data set: 23 27 29 31 35 39 40 41 52 66 71 74 75 90 99"

目前,我已编写了找到查找值位置的部分。我只是不知道如何在每次通过后打印出剩余的数据。请帮忙。

这是我的代码:

import java.util.Scanner;

class BinarySearchTracing{
  public static void main(String args[]){
    Scanner scan = new Scanner (System.in);
    int[] d = {23, 27, 29, 31, 35, 39, 40, 41, 52, 66, 71, 74, 75, 90, 99};
    int start = 0;
    int middle = 0;
    int end = d.length - 1;
    int location = -1;
    boolean found = false;

    System.out.println("Enter the look for value: ");
    int lookfor = scan.nextInt();
    scan.close();

    while(start <= end && found == false) {
        middle = (start + end) / 2;
        if(d[middle] == lookfor) {
            found = true;
            location = middle;
        }
        else if(d[middle] < lookfor) {
            start = middle + 1;
        }
        else {
            end = middle - 1;
        }
    }
    System.out.println(location);
  }
}

最佳答案

要打印两个索引之间给定数组的子数组,您可以使用 following methods 之一:

  • Arrays.copyOfRange
  • System.arraycopy等

关于java - 每次通过后如何打印剩余的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61194740/

相关文章:

c++ - 使用 CPU 缓存行击败二进制搜索

java - 使用 iText 将 Swing 组件导出为 PDF

java - 在 Java 中检查字符串是否是 ISO 语言的 ISO 国家/地区的更简洁方法

c - c中的递归二进制搜索

javascript - 为什么我的算法无法在我的数组中找到索引?

c++ - 如何使用二进制搜索将所有重复的字符串打印在排序数组中?

java - 在 Java 中模拟 C++-typedef

java - 从可更改的 TextView 获取整数以在倒计时器中使用

java - Java 中的 C# 日期时间

algorithm - BinarySearch 坐标之间的距离