任何人都可以告诉我Java组合类的导入语句吗?
问题描述:
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
答
你可以从你的import语句
java.lang.org.apache.commons.math3.util.Combinations;
看是不是在核心Java/JEE包,而指第三方包库。因此您需要下载该软件包并将其放入您的类路径或项目lib目录中,或使用IDE或编译命令将该软件包指向系统中的某个位置。然而,现在开发人员正在使用诸如maven或gradle等各种构建工具来管理这些项目依赖性开销。
您是否已将该包添加到项目中(“Combinations”不是Java类,而是通过Apache Project库添加的)?你在用什么IDE? – AntonH
软件包开始时不应该有'java.'。尽管如此,您应该将导入保留到IDE中。 – bcsb1001
抛弃'java.lang'位。 –