考试题:
定义泛型类Table,记住基于java.utils.List的类,定义类属性。然后定义使用' null'创建和填充表的构造函数表(int rows,int columns)。之后定义两种方法:
List<E> getRow(int row) returns list of elements in row;
List<E> gerColumn(int column) returns list of elements in columns
注意:应该可以使用返回值修改inicial表。
我的实施:
public class Table<E> {
private List<List<E>> table;
public Table(int r, int c) {
this.table=new ArrayList<>();
for (int i=0; i<r; i++){
table.add(new ArrayList<>());
for (int j=0; j<c; j++){
table.get(i).add(null);
}
}
}
}
现在我需要的是两种方法:
列出getRow(int i),它返回一个数字为i的行;
列出getColumn(int j),它返回一个数字为j的列;
这些方法应该返回引用,或者使用示例:
//rows() and columns() methods are returning int number
//of rows and colums of this table, respectively
Table<String> t=new Table<>(2, 2);
for (int i=0; i<t.rows(); i++){
System.out.println(t.getRow(i));
}
System.out.println();
t.getRow(0).set(1,"Lorem ipsum");
for (int i=0; i<t.rows(); i++){
System.out.println(t.getRow(i));
}
应打印:
[null, null]
[null, null]
[null, Lorem ipsum]
[null, null]
- &GT;初始表&#39; t&#39;已经改变了。
因此,在getRow()的情况下非常简单:
public List<E> getRow(int i) {
return this.table.get(i);
}
但我不能对getColumn这样做
我现在拥有的,返回一行,但它没有引用该表,
只针对位置:
public List<E> getColumn(int j) {
List<E> temp=new ArrayList<>();
for (int i=0; i<this.rows(); i++){
temp.add(this.table.get(i).get(j));
}
return temp;
}
我理解为什么会这样,但我真的不知道如何实现它。 我现在还不确定类属性和构造函数是否为这样的功能做了。
答案 0 :(得分:0)
这个问题可能存在更好的解决方案,但最简单的解决方法是:
列表是一个界面,为什么不实现使用列表列表的列表?内部类看起来像这样:
public class ColumnList extends AbstractList<E> {
private Integer column;
public ColumnList(Integer column) {
this.column = column;
}
@Override
public E get(int index) {
return table.get(index).get(column);
}
@Override
public int size() {
return table.size();
}
@Override
public E set(int index, E element) {
return table.get(index).set(column, element);
}
}
然后方法getRow
看起来像这样:
public List<E> getColumn(int index) {
return new ColumnList(index);
}
JavaDoc指出的地方:
要实现不可修改的列表,程序员只需要扩展此类并提供get(int)和size()方法的实现。
要实现可修改的列表,程序员必须另外覆盖set(int,E)方法(否则会抛出UnsupportedOperationException)。
如果列表是可变大小的,程序员必须另外覆盖add(int,E)和remove(int)方法。
重要的事情:
我建议也要实行行列表,因为不禁止添加ArrayList
。
您可以使用数组而不是使用列表列表。
答案 1 :(得分:0)
这就是我认为对您有用的方式,
public class Table<E> {
private List<Row<E>> table;
public Table(int r, int c) {
this.table = new ArrayList<>();
for (int i = 0; i < r; i++) {
table.add(new Row<>(new ArrayList<>()));
for (int j = 0; j < c; j++) {
table.get(i).add(null);
}
}
}
public int rows() {
return table.size();
}
public List<Row<E>> getTable() {
return table;
}
public void setTable(List<Row<E>> table) {
this.table = table;
}
public Row<E> getRow(int rowIndex) {
if(rowIndex >= 0 && rowIndex < table.size())
return table.get(rowIndex);
throw new IndexOutOfBoundsException();
}
@Override
public String toString() {
return "Table{" + "table=" + table + '}';
}
}
块引用
public class Row<E> {
List<E> columnList;
public Row() {
}
public Row(List<E> columnList) {
this.columnList = columnList;
}
public void add(E colValue) {
columnList.add(colValue);
}
public void set(int cIndex, E cValue) {
columnList.set(cIndex, cValue);
}
public E getColumn(int colIndex) {
if(colIndex >= 0 && colIndex < columnList.size())
return columnList.get(colIndex);
throw new IndexOutOfBoundsException();
}
public int columns() {
return columnList.size();
}
@Override
public String toString() {
return "Row{" + "columnList=" + columnList + '}';
}
}
块引用
public static void main(String[] args) {
Table<String> t = new Table<>(2, 2);
for (int i = 0; i < t.rows(); i++) {
System.out.println(t.getRow(i));
}
System.out.println();
t.getRow(0).set(1, "Lorem ipsum");
for (int i = 0; i < t.rows(); i++) {
System.out.println(t.getRow(i));
}
}
希望这个帮助