mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix test
This commit is contained in:
@@ -20,6 +20,7 @@
|
|||||||
|
|
||||||
### Bug修复
|
### Bug修复
|
||||||
* 【core】 修复NetUtil.getUsableLocalPort问题(pr#69@Gitee)
|
* 【core】 修复NetUtil.getUsableLocalPort问题(pr#69@Gitee)
|
||||||
|
* 【core】 修复MathUtil.arrangementSelect重复元素导致无结果问题(issue#529@Gitee)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@@ -5,6 +5,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
import cn.hutool.core.util.NumberUtil;
|
import cn.hutool.core.util.NumberUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -14,7 +15,7 @@ import cn.hutool.core.util.NumberUtil;
|
|||||||
* @author looly
|
* @author looly
|
||||||
* @since 4.0.7
|
* @since 4.0.7
|
||||||
*/
|
*/
|
||||||
public class Arrangement implements Serializable{
|
public class Arrangement implements Serializable {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private String[] datas;
|
private String[] datas;
|
||||||
@@ -27,7 +28,7 @@ public class Arrangement implements Serializable{
|
|||||||
public Arrangement(String[] datas) {
|
public Arrangement(String[] datas) {
|
||||||
this.datas = datas;
|
this.datas = datas;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 计算排列数,即A(n, n) = n!
|
* 计算排列数,即A(n, n) = n!
|
||||||
*
|
*
|
||||||
@@ -46,12 +47,12 @@ public class Arrangement implements Serializable{
|
|||||||
* @return 排列数
|
* @return 排列数
|
||||||
*/
|
*/
|
||||||
public static long count(int n, int m) {
|
public static long count(int n, int m) {
|
||||||
if(n == m) {
|
if (n == m) {
|
||||||
return NumberUtil.factorial(n);
|
return NumberUtil.factorial(n);
|
||||||
}
|
}
|
||||||
return (n > m) ? NumberUtil.factorial(n, n - m) : 0;
|
return (n > m) ? NumberUtil.factorial(n, n - m) : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 计算排列总数,即A(n, 1) + A(n, 2) + A(n, 3)...
|
* 计算排列总数,即A(n, 1) + A(n, 2) + A(n, 3)...
|
||||||
*
|
*
|
||||||
@@ -60,14 +61,15 @@ public class Arrangement implements Serializable{
|
|||||||
*/
|
*/
|
||||||
public static long countAll(int n) {
|
public static long countAll(int n) {
|
||||||
long total = 0;
|
long total = 0;
|
||||||
for(int i = 1; i <= n; i++) {
|
for (int i = 1; i <= n; i++) {
|
||||||
total += count(n, i);
|
total += count(n, i);
|
||||||
}
|
}
|
||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 全排列选择(列表全部参与排列)
|
* 全排列选择(列表全部参与排列)
|
||||||
|
*
|
||||||
* @return 所有排列列表
|
* @return 所有排列列表
|
||||||
*/
|
*/
|
||||||
public List<String[]> select() {
|
public List<String[]> select() {
|
||||||
@@ -82,52 +84,45 @@ public class Arrangement implements Serializable{
|
|||||||
*/
|
*/
|
||||||
public List<String[]> select(int m) {
|
public List<String[]> select(int m) {
|
||||||
final List<String[]> result = new ArrayList<>((int) count(this.datas.length, m));
|
final List<String[]> result = new ArrayList<>((int) count(this.datas.length, m));
|
||||||
select(new String[m], 0, result);
|
select(this.datas, new String[m], 0, result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 排列所有组合,即A(n, 1) + A(n, 2) + A(n, 3)...
|
* 排列所有组合,即A(n, 1) + A(n, 2) + A(n, 3)...
|
||||||
*
|
*
|
||||||
* @return 全排列结果
|
* @return 全排列结果
|
||||||
*/
|
*/
|
||||||
public List<String[]> selectAll(){
|
public List<String[]> selectAll() {
|
||||||
final List<String[]> result = new ArrayList<>((int)countAll(this.datas.length));
|
final List<String[]> result = new ArrayList<>((int) countAll(this.datas.length));
|
||||||
for(int i = 1; i <= this.datas.length; i++) {
|
for (int i = 1; i <= this.datas.length; i++) {
|
||||||
result.addAll(select(i));
|
result.addAll(select(i));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 排列选择
|
* 排列选择<br>
|
||||||
|
* 排列方式为先从数据数组中取出一个元素,再把剩余的元素作为新的基数,依次列推,直到选择到足够的元素
|
||||||
*
|
*
|
||||||
|
* @param datas 选择的基数
|
||||||
* @param dataList 待选列表
|
* @param dataList 待选列表
|
||||||
* @param resultList 前面(resultIndex-1)个的排列结果
|
* @param resultList 前面(resultIndex-1)个的排列结果
|
||||||
* @param resultIndex 选择索引,从0开始
|
* @param resultIndex 选择索引,从0开始
|
||||||
* @param result 最终结果
|
* @param result 最终结果
|
||||||
*/
|
*/
|
||||||
private void select(String[] resultList, int resultIndex, List<String[]> result) {
|
private void select(String[] datas, String[] resultList, int resultIndex, List<String[]> result) {
|
||||||
int resultLen = resultList.length;
|
if (resultIndex >= resultList.length) { // 全部选择完时,输出排列结果
|
||||||
if (resultIndex >= resultLen) { // 全部选择完时,输出排列结果
|
if (false == result.contains(resultList)) {
|
||||||
result.add(Arrays.copyOf(resultList, resultList.length));
|
result.add(Arrays.copyOf(resultList, resultList.length));
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 递归选择下一个
|
// 递归选择下一个
|
||||||
for (int i = 0; i < datas.length; i++) {
|
for (int i = 0; i < datas.length; i++) {
|
||||||
// 判断待选项是否存在于排列结果中
|
resultList[resultIndex] = datas[i];
|
||||||
boolean exists = false;
|
select(ArrayUtil.remove(datas, i), resultList, resultIndex + 1, result);
|
||||||
for (int j = 0; j < resultIndex; j++) {
|
|
||||||
if (datas[i].equals(resultList[j])) {
|
|
||||||
exists = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (false == exists) { // 排列结果不存在该项,才可选择
|
|
||||||
resultList[resultIndex] = datas[i];
|
|
||||||
select(resultList, resultIndex + 1, result);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -3,8 +3,11 @@ package cn.hutool.core.math;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.Console;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 排列单元测试
|
* 排列单元测试
|
||||||
* @author looly
|
* @author looly
|
||||||
@@ -51,4 +54,13 @@ public class ArrangementTest {
|
|||||||
List<String[]> list2 = arrangement.select(0);
|
List<String[]> list2 = arrangement.select(0);
|
||||||
Assert.assertTrue(1 == list2.size());
|
Assert.assertTrue(1 == list2.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Ignore
|
||||||
|
public void selectTest2() {
|
||||||
|
List<String[]> list = MathUtil.arrangementSelect(new String[] { "1", "1", "3", "4" });
|
||||||
|
for (String[] strings : list) {
|
||||||
|
Console.log(strings);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -483,6 +483,17 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
|||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置默认行高,值为一个点的高度
|
||||||
|
*
|
||||||
|
* @param height 高度
|
||||||
|
* @return this
|
||||||
|
* @since 4.6.5
|
||||||
|
*/
|
||||||
|
public ExcelWriter setDefaultRowHeight(int height) {
|
||||||
|
return setRowHeight(-1, height);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置行高,值为一个点的高度
|
* 设置行高,值为一个点的高度
|
||||||
|
@@ -75,7 +75,7 @@ public class ExcelWriteTest {
|
|||||||
rows.add(ObjectUtil.clone(row1));
|
rows.add(ObjectUtil.clone(row1));
|
||||||
}
|
}
|
||||||
|
|
||||||
String filePath = "e:/writeTest.xlsx";
|
String filePath = "f:/test/writeTest.xlsx";
|
||||||
FileUtil.del(filePath);
|
FileUtil.del(filePath);
|
||||||
// 通过工具类创建writer
|
// 通过工具类创建writer
|
||||||
ExcelWriter writer = ExcelUtil.getWriter(filePath);
|
ExcelWriter writer = ExcelUtil.getWriter(filePath);
|
||||||
|
Reference in New Issue
Block a user