【问题标题】:Can anyone tell me the import statement for Java's Combinations class?谁能告诉我 Java 的 Combinations 类的导入语句?
【发布时间】:2018-05-01 18:33:27
【问题描述】:

我尝试通过

导入组合
import java.lang.org.apache.commons.math3.util.Combinations;

但是当我在源代码中使用组合时,我不断收到错误。

import java.util.*;
import java.org.apache.commons.math3.util.Combinations;

public class PowerSet{   //gets power set for a set containing first n integers

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = Integer.parseInt(args[0]);

        for(int i=0; i<=n; i++){
            Combinations c = new Combinations(n,i);
            Iterator iter = c.iterator();
            while(iter.hasNext()){
                int[] iarr = (int[])iter.next();
                System.out.print("{" + iarr[0]);
                for(int a=1; a<iarr.length; a++){
                    System.out.println(", " + iarr[a]);
                }
                System.out.print("}, ");
            }
        }
    }
}

我得到的错误清楚地表明该类不存在。是我弄错了层次结构还是我应该导入类的方式有误?

package java.org.apache.commons.math3.util does not exist
import java.org.apache.commons.math3.util.Combinations;
                                     ^
PowerSet.java:11: error: cannot find symbol
        Combinations c = new Combinations(n,i);
        ^
symbol:   class Combinations
location: class PowerSet

【问题讨论】:

  • 您是否将包添加到您的项目中(Combinations 不是 Java 类,而是通过 Apache Project 库添加的)?您使用的是什么 IDE?
  • 包的开头应该没有java.。不过,您应该将导入留在您的 IDE 中。
  • 放弃java.lang 位。
  • 我假设您已经仔细检查了 Apache Commons Math 是否在您的类路径中?
  • 您下载了 JAR 文件吗?您是否已将 JAR 添加到您的类路径中?您是否将导入更改为 import org.apache.commons.math3.util.Combinations;

标签: java import path package combinations


【解决方案1】:

从导入语句中可以看出

java.lang.org.apache.commons.math3.util.Combinations;

不在核心 java / jee 包中,而是指第 3 方包存储库。因此您需要下载该包并将其放入您的类路径或项目 lib 目录中,或者使用您的 IDE 或编译命令将该包指向系统中的某个位置。但是,现在开发人员正在使用各种构建工具(例如 maven 或 gradle)来管理此类项目依赖项开销。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    相关资源
    最近更新 更多