已知表格总行,总列,排列方式,根据当前位置,计算行列坐标问题

目录

1. pom引入依赖

2. 代码示例 

 3. 测试


 

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 列

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值