栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Java中获取子矩阵的参考

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Java中获取子矩阵的参考

我将为

int[][]
数据编写一个包装器,并将其称为Matrix类。然后写一个方法
getSubMatrix(x, y, rows,cols)
。这是一个简单的Matrix类:

static class Matrix {    int[][] data;    int x, y, columns, rows;    public Matrix(int[][] data) {        this(data, 0, 0, data.length, data[0].length);    }    private Matrix(int[][] data, int x, int y, int columns, int rows) {        this.data = data;        this.x = x;        this.y = y;        this.columns = columns;        this.rows = rows;    }    public Matrix getSubMatrix(int x, int y, int columns, int rows) {        return new Matrix(data, this.x + x , this.y + y, columns, rows);    }    public String toString() {        StringBuffer sb = new StringBuffer();        for (int i = y; i < x + rows; i++) { for (int j = x; j < x + columns; j++)     sb.append(data[i][j]).append(" "); sb.append("n");        }        sb.setLength(sb.length() - 1);        return sb.toString();    }}

这个测试程序…:

public static void main(String[] args) throws IOException {    int[][] testData = new int[10][10];    for (int i = 0; i < testData.length; i++)         for (int j = 0; j < testData[i].length; j++) testData[i][j] = 100 + i + j;    Matrix full = new Matrix(testData);    System.out.println("Full test matrix:");    System.out.println(full);    System.out.println();    System.out.println("Part of the matrix:");    System.out.println(full.getSubMatrix(3, 3, 3, 3));}

…打印:

Full test matrix:100 101 102 103 104 105 106 107 108 109 101 102 103 104 105 106 107 108 109 110 102 103 104 105 106 107 108 109 110 111 103 104 105 106 107 108 109 110 111 112 104 105 106 107 108 109 110 111 112 113 105 106 107 108 109 110 111 112 113 114 106 107 108 109 110 111 112 113 114 115 107 108 109 110 111 112 113 114 115 116 108 109 110 111 112 113 114 115 116 117 109 110 111 112 113 114 115 116 117 118Part of the matrix:106 107 108 107 108 109 108 109 110


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/498476.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号