目录
1. pom引入依赖
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>1.0.0-alpha-4</version>
</dependency>
2. 代码示例
/**
* @param rows: 行数据
* @param cols: 列数据
* @param position: 要计算的位置
* @param isTransverse: 是否横向排列 是否横向 1横向,0纵向
* @return io.vavr.Tuple2<java.lang.Integer, java.lang.Integer> <行,列>
* @描述: 计算坐标方法
* @Date: 2023/7/13 10:43
* @Author: Duys
**/
public static Tuple2<Integer, Integer> coordinateCalculation(int rows, int cols, int position, Boolean isTransverse) {
int row = 0;
int col = 0;
if (isTransverse) {
// // 横向排列 计算行号和列号
row = (position) / cols + 1;
col = (position) % cols + 1;
} else {
// 纵向排列 计算行号和列号
row = (position) % rows + 1;
col = (position) / rows + 1;
}
//System.out.println("横向排列 第 " + (position + 1) + " 位的坐标为 " + row + " , " + col);
return new Tuple2<>(row, col);
}
3. 测试
public static void main(String[] args) {
// -------------------------------------------------------------------------------------
//横向排列
int rows = 3; // 表格的行数
int cols = 3; // 表格的列数
int position = 0; // 要计算的位置
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
for (int i = 0; i < list.size(); i++) {
Tuple2<Integer, Integer> coordinate = coordinateCalculation(rows, cols, position, true);
System.out.println("横向排列 第 " + (position + 1) + " 位的坐标为: " + coordinate._1() + " 行 , " + coordinate._2()+" 列");
position++;
}
// -------------------------------------------------------------------------------------
}
// 输出内容
横向排列 第 1 位的坐标为: 1 行 , 1 列
横向排列 第 2 位的坐标为: 1 行 , 2 列
横向排列 第 3 位的坐标为: 1 行 , 3 列
横向排列 第 4 位的坐标为: 2 行 , 1 列
横向排列 第 5 位的坐标为: 2 行 , 2 列