为什么这样做:
List<ArrayList<Integer>> input = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> row1 = new ArrayList<Integer>();
ArrayList<Integer> row2 = new ArrayList<Integer>();
input.add(row1);
input.get(0).add(2);
input.get(0).add(3);
input.get(0).add(4);
input.add(row2);
input.get(1).add(5);
input.get(1).add(6);
input.get(1).add(7);
ArrayList<Integer> row1 = new ArrayList<Integer>();
row1.add(2);
row1.add(3);
row1.add(4);
row1.add(5);
ArrayList<Integer> row2 = new ArrayList<Integer>();
row1.add(6);
row1.add(7);
row1.add(8);
row1.add(9);
input.add(row1);
input.add(row2);
第一个创建一个2X3矩阵,第二个创建一个2D数组,其第一个元素包含'row1'和'row2'中的所有数字。
TIA !
您没有在row2中添加任何内容。
改变ArrayList<Integer> row2 = new ArrayList<Integer>();
row1.add(6);
row1.add(7);
row1.add(8);
row1.add(9);
ArrayList<Integer> row2 = new ArrayList<Integer>();
row2.add(6);
row2.add(7);
row2.add(8);
row2.add(9);
您将所有元素添加到row1中,因此输出与预期一致。
。您正在使用
row1.add(6);
不是row2.add(6)
矩阵2D &实施:3 d
import java.util.ArrayList;
import java.util.List;
public class Matrix {
private int rows;
private int cols;
private int depth;
private List<Integer> list;
public Matrix(int rows, int cols, List<Integer> list) {
this.rows = rows;
this.cols = cols;
this.list = list;
}
public Matrix(int rows, int cols, int depth, List<Integer> list) {
this.rows = rows;
this.cols = cols;
this.depth = depth;
this.list = list;
}
public Integer get2D(int row, int col) {
Integer elem = null;
if (row < rows && col < cols) {
int index = row * cols + col;
elem = list.get(index);
}
return elem;
}
public boolean set2D(int row, int col, Integer val) {
boolean flag = false;
if (row < rows && col < cols) {
int index = row * cols + col;
list.set(index, val);
flag = true;
}
return flag;
}
public Integer get3D(int row, int col, int dep) {
Integer elem = null;
if (row < rows && col < cols && dep <depth) {
int index = dep * rows * cols + row * cols + col;
elem = list.get(index);
}
return elem;
}
public boolean set3D(int row, int col, int dep, Integer val) {
boolean flag = false;
if (row < rows && col < cols && dep <depth) {
int index = dep * rows * cols + row * cols + col;
list.set(index, val);
flag = true;
}
return flag;
}
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
list.add(i);
}
Matrix mat2d = new Matrix(2, 3, list);
System.out.println(mat2d.get2D(1, 2));
Matrix mat3d = new Matrix(2, 2, 5, list);
System.out.println(mat3d.get3D(1, 1, 3));
}
}
数组二维实现:
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class Array2D {
private int rowNum;
private Map<Integer, List<Integer>> rows;
public Array2D(int rowNum) {
this.rowNum = rowNum;
rows = new TreeMap<Integer, List<Integer>>();
for (int i = 0; i < rowNum; i++) {
rows.put(i, new ArrayList<Integer>());
}
}
public Integer get(int row, int col) {
Integer elem = null;
if (row < rowNum) {
List<Integer> list = rows.get(row);
if (col < list.size())
elem = list.get(col);
}
return elem;
}
public boolean set(int row, int col, Integer val) {
boolean flag = false;
if (row < rowNum) {
List<Integer> list = rows.get(row);
if (col < list.size())
list.set(col, val);
}
return flag;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}