N
nastya2018
Ребята как можно преобразовать матрицу в двухмерный массив
Код:
package matrix;
public class Main extends Exception {
public static void main(String[] args) throws MatrixIndexException {
Matrix m1 = new Matrix(1000, 1000);
m1.put(1000, 1000, -100);
System.out.println(m1.get(1000, 1000));
Код:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package matrix;
public class Matrix {
private int rowCount;
private int colCount;
private int[][] data;
Matrix(int[][] a) {
}
Matrix(int row, int col) throws MatrixIndexException {
if (row <= 0 || col <= 0) {
throw new MatrixIndexException("Недопустимый размер матрицы.");
}
this.rowCount = row;
this.colCount = col;
data = new int[row][col];
}
Matrix(Matrix matrix) {
this.rowCount = matrix.getRowCount();
this.colCount = matrix.getColCount();
data = new int[rowCount][colCount];
for (int i = 1; i < rowCount; i++) {
for (int j = 1; j < colCount; j++) {
data[i][j] = matrix.data[i][j];
}
}
}
public int get(int i, int j) throws MatrixIndexException {
if (i < 1 || i > rowCount) {
throw new MatrixIndexException("Недопустимое число строк: " + i);
}
if (j < 1 || j > colCount) {
throw new MatrixIndexException("Недопустимое число столбцов: " + j);
}
return data [i][j];
}
void put(int i, int j, int value) throws MatrixIndexException {
if (i < 1 || i > rowCount) {
throw new MatrixIndexException("Недопустимое число строк: " + i);
}
if (j < 1 || j > colCount) {
throw new MatrixIndexException("Недопустимое число столбцов: " + j);
}
data[i][j] = value;
}
public int getRowCount() {
return rowCount;
}
public int getColCount() {
return colCount;
}
@Override
public boolean equals(Object obj) {
Matrix m = (Matrix) obj;
if (m.getRowCount() != rowCount || m.getColCount() != colCount) {
return false;
}
for (int i = 1; i < rowCount; i++) {
for (int j = 1; j < colCount; j++) {
if (data[i][j] != m.data[i][j]) {
return false;
}
}
}
return true;
}
@Override
public String toString() {
StringBuilder out = new StringBuilder();
out.append("Matrix:\n[ ");
for (int i = 0; i < rowCount; i++) {
if (i != 0) {
out.append("\n");
out.append(" ");
}
for (int j = 0; j < colCount; j++) {
out.append(data[i][j]);
if (j == colCount - 1) {
continue;
}
for (int k = 0; k < getMaxLength() - getIntLength(data[i][j]) + 2; k++) {
out.append(" ");
}
}
}
out.append(" ]");
return out.toString();
}
private int getMaxLength() {
int max = Integer.MIN_VALUE;
for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < colCount; j++) {
int k = data[i][j];
if (k > max) {
max = k;
}
}
}
return getIntLength(max);
}
private int getIntLength(int i) {
return String.valueOf(i).length();
}
}
Код:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package matrix;
/**
*
* @author Adil
*/
public class MatrixIndexException extends Exception {
public MatrixIndexException (String s){
super(s);
}
public MatrixIndexException(){
}
}
Код:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package matrix;
import java.util.Random;
public class MatrixUtils {
public static void fillByRandom(Matrix m) {
int rowCount = m.getRowCount();
int colCount = m.getColCount();
Random random = new Random();
for (int i = 1; i <= rowCount; i++) {
for (int j = 1; j <= colCount; j++) {
try {
m.put(i, j, random.nextInt(50));
} catch (MatrixIndexException e) {
System.err.println(e.getMessage());
}
}
}
}
public static void fillByNumber(Matrix m, int number) {
int rowCount = m.getRowCount();
int colCount = m.getColCount();
for (int i = 1; i <= colCount; i++) {
for (int j = 1; j <= rowCount; j++) {
try {
m.put(i,j, number);
} catch (MatrixIndexException e){
System.err.println(e.getMessage());
}
}
}
}
}