【问题标题】:How to make a Linear Search OOP approach that uses Input Scanner如何制作使用输入扫描器的线性搜索 OOP 方法
【发布时间】:2022-10-15 05:23:22
【问题描述】:

所以我重构了一个只使用 main 方法的线性搜索代码。我的目标是将其转换为 OOP 方法。但我无法保存输入的整数集。

// LinearSearchDriver.java
import java.util.Scanner;

public class LinearSearchDriver {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        LinearSearch linearSearch = new LinearSearch();
        System.out.println("Enter number of elements");
        int numElements = in.nextInt();
        linearSearch.setNumberOfElements(numElements);

        System.out.println("Enter " + numElements + " integers");

        for (int count = 0; count < numElements; count++){
            int setIntegers = in.nextInt();
            linearSearch.setNumberOfIntegers(setIntegers);
        }
        
        System.out.println("Enter value to find");
        int search = in.nextInt();
        linearSearch.findValue(search);

    }
}

//LinearSearch.java
public class LinearSearch {
    private int c;
    private int n;
    private int array[];

    public void setNumberOfElements(int n) {
        this.n = n;
        this.array = new int[n];
    }

    public void setNumberOfIntegers(int y) {
        for (c=0; c < n; c++)
            array[c] = y;
    }

    public void findValue(int search) {
        for (c = 0; c < n; c++) {
            if (array[c] == search) {    /* Searching element is present */
                System.out.println(search + " is present at location " + (c + 1) + ".");
                break;
            }
        }

        if (c == n) { /* Searching element is absent */
            System.out.println(search + " is not present in array.");
        }
    }
}

示例输出:

但是当我输入数字 1 时,这是输出:

该程序只读取我认为的 2 号输出,最后一个数字只是保存到数组中的那个。

【问题讨论】:

标签: java oop refactoring linear-search


【解决方案1】:
for (c = 0; c < n; c++) {
    array[c] = y;
}

是出问题的地方。您正在为数组中的每个索引设置传递给该函数的最后一个值。

您可以通过多种方式解决此问题:

  1. 将数组而不是单个参数传递给函数。
  2. 您可以确定数组中的当前元素数量,然后“手动”附加最新值。见this post
  3. 或者您可以简单地使用动态结构,例如 List 并将元素附加到该结构。

    以下是第 3 个选项的粗略大纲:

    public class LinearSearch {
        private List<Integer> intList;
    
        public LinearSearch() {
        }
    
        public void setNumberOfElements(int n) {
            intList = new ArrayList<>(n); //Set the capacity here like before.
        }
    
        public void setNumberOfIntegers(int y) {
            //If you want your List to always only contain the initially allowed number of elements, you could implement
            // this logic here, by adding the new value and removing the "oldest" one.
            intList.add(y);
        }
    
        public void findValue(int search) {
            if (!intList.contains(search)) { //You can put this up here and potentially skip the looping.
                System.out.println(search + " is not present in array.");
                return;
            }
            for (int n : intList) {
                if (n == search) {
                    System.out.println(search + " is present at location " + intList.indexOf(search) + ".");
                    return; //Use return to exit the method, break only exits the loop in your example, and you could print both lines.
                }
            }
        }
    }
    

【讨论】:

  • 是的,我也认为,这种方法是错误的。好的,我会试试的。谢谢
  • 太感谢了!我正在尝试数字 3 选项。并感谢您的简短说明和评论。 '会实践这个方法!
猜你喜欢
  • 2012-08-22
  • 2021-05-23
  • 1970-01-01
  • 1970-01-01
  • 2014-06-05
  • 1970-01-01
  • 1970-01-01
  • 2012-09-02
  • 1970-01-01
相关资源
最近更新 更多