亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

java錯誤:無法找到或加載主類BinarySearch

java錯誤:無法找到或加載主類BinarySearch

蝴蝶刀刀 2023-07-13 15:51:37
我正在閱讀 Sedgewick 的《算法》(第四版)。代碼是這樣的:package edu.princeton.cs.algs4;import java.util.Arrays;/** *  The {@code BinarySearch} class provides a static method for binary *  searching for an integer in a sorted array of integers. *  <p> *  The <em>indexOf</em> operations takes logarithmic time in the worst case. *  <p> *  For additional documentation, see <a href="https://algs4.cs.princeton.edu/11model">Section 1.1</a> of *  <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne. * *  @author Robert Sedgewick *  @author Kevin Wayne */public class BinarySearch {    private BinarySearch() { }    public static int indexOf(int[] a, int key) {        int lo = 0;        int hi = a.length - 1;        while (lo <= hi) {            // Key is in a[lo..hi] or not present.            int mid = lo + (hi - lo) / 2;            if      (key < a[mid]) hi = mid - 1;            else if (key > a[mid]) lo = mid + 1;            else return mid;        }        return -1;    }    @Deprecated    public static int rank(int key, int[] a) {        return indexOf(a, key);    }    public static void main(String[] args) {        // read the integers from a file        In in = new In(args[0]);        int[] whitelist = in.readAllInts();        // sort the array        Arrays.sort(whitelist);        // read integer key from standard input; print if not in whitelist        while (!StdIn.isEmpty()) {            int key = StdIn.readInt();            if (BinarySearch.indexOf(whitelist, key) == -1)                StdOut.println(key);        }    }}我已經成功編譯了代碼:javac -cp /Users/user/documents/algorithms/algs4-master/target/algs4-1.0.0.0.jar BinarySearch.java但是當我嘗試運行代碼時,出現錯誤:user$ java -cp /Users/user/documents/algorithms/algs4-master/target/algs4-1.0.0.0.jar BinarySearch 錯誤:無法找到或加載主類 BinarySearch 原因:java.lang.ClassNotFoundException:BinarySearch請幫忙告訴我這里發生了什么事。多謝!
查看完整描述

1 回答

?
泛舟湖上清波郎朗

TA貢獻1818條經驗 獲得超3個贊

提供包含 main 方法和不包含 main 方法的類的整個包,.java因為您正在運行該類,而不是 java 文件:

edu.princeton.cs.algs4

javac -cp /Users/user/documents/algorithms/algs4-master/target/algs4-1.0.0.0.jar edu.princeton.cs.algs4.BinarySearch



查看完整回答
反對 回復 2023-07-13
  • 1 回答
  • 0 關注
  • 182 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號