Java 组合数学。使用生成的数据创建对象
Java combinatorics. Creating an object with generated data
这是我第一次 post 来这里。今天我开始使用 Java.
的组合库
这个:https://github.com/dpaukov/combinatoricslib3
我在 Excel 中得到的三角形边长超过 10k。比我把它们拉到二维整数数组中。
比我创建的class三角形:
public class Triangle {
private int a;
private int b;
private int c;
public Triangle(int a, int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
public boolean isCorrect()
{
if(this.a + this.b > this.c)
return true;
return false;
}
}
我的问题是我可以生成所有可能的三角形组合,但不知道如何创建对象三角形。只知道如何打印结果。
public static void main(String[] args) throws IOException {
Generator.combination(sides).simple(3).stream().forEach(System.out::println);
}
提前谢谢你。干杯!
编辑:
这是侧面的例子:
static final int[][] sides = new int[][]{
{71, 100, 1231, 832, 127},
{336, 447, 815, 658, 373},
{126, 444, 556, 221, 1322},
{1226, 662, 985, 87, 991},
{555, 512, 111, 339, 22},
};
我想用这些数据生成所有可能的三角形。
应该看起来像这样:
Generator
.combination(sides)
.simple(3)
.stream()
.forEach(
sides -> new Triangle(sides[0],sides[1],sides[2])
);
请注意,这意味着边是 整数,如果不是(例如字符串),您可能需要另外将它们转换(映射)为适当的类型。
现在,如果您想将它们全部收集起来列出,您可以这样做:
List<Triangle> triangles = Generator
.combination(sides)
.simple(3)
.stream()
.map(sides -> new Triangle(sides[0],sides[1],sides[2]))
.collect(Collectors.toList())
您可以遍历二维 int[][] 数组,并找到每个 line 的所有组合,例如那:
Arrays.stream(sides)
.forEach(
line -> {
Generator.combination(Arrays.stream(line).boxed().collect(Collectors.toList()))
.simple(3)
.stream()
.forEach(System.out::println);
}
);
这是我第一次 post 来这里。今天我开始使用 Java.
的组合库这个:https://github.com/dpaukov/combinatoricslib3
我在 Excel 中得到的三角形边长超过 10k。比我把它们拉到二维整数数组中。
比我创建的class三角形:
public class Triangle {
private int a;
private int b;
private int c;
public Triangle(int a, int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
public boolean isCorrect()
{
if(this.a + this.b > this.c)
return true;
return false;
}
}
我的问题是我可以生成所有可能的三角形组合,但不知道如何创建对象三角形。只知道如何打印结果。
public static void main(String[] args) throws IOException {
Generator.combination(sides).simple(3).stream().forEach(System.out::println);
}
提前谢谢你。干杯!
编辑:
这是侧面的例子:
static final int[][] sides = new int[][]{
{71, 100, 1231, 832, 127},
{336, 447, 815, 658, 373},
{126, 444, 556, 221, 1322},
{1226, 662, 985, 87, 991},
{555, 512, 111, 339, 22},
};
我想用这些数据生成所有可能的三角形。
应该看起来像这样:
Generator
.combination(sides)
.simple(3)
.stream()
.forEach(
sides -> new Triangle(sides[0],sides[1],sides[2])
);
请注意,这意味着边是 整数,如果不是(例如字符串),您可能需要另外将它们转换(映射)为适当的类型。
现在,如果您想将它们全部收集起来列出,您可以这样做:
List<Triangle> triangles = Generator
.combination(sides)
.simple(3)
.stream()
.map(sides -> new Triangle(sides[0],sides[1],sides[2]))
.collect(Collectors.toList())
您可以遍历二维 int[][] 数组,并找到每个 line 的所有组合,例如那:
Arrays.stream(sides)
.forEach(
line -> {
Generator.combination(Arrays.stream(line).boxed().collect(Collectors.toList()))
.simple(3)
.stream()
.forEach(System.out::println);
}
);