Java 如何访问ArrayList中的ArrayList?(仿制药?)

Java 如何访问ArrayList中的ArrayList?(仿制药?),java,arrays,oop,generics,Java,Arrays,Oop,Generics,我试图创建一个表类,它的行和列可以扩展或收缩,以存储int和string作为第一个Java项目。我试图用来表示表的数据结构是一个ArrayList of ArrayList,其中初始数组的元素都指向一个新的数组列表-因此初始数组类型充当了行的入口。这是一张我脑海中的照片,供参考: 我遇到的问题是访问内部数组列表。我已经阅读了一些文档,但我似乎无法理解为什么我无法访问内部列表的大问题。这里有一些代码: import java.util.ArrayList; public class Table

我试图创建一个表类,它的行和列可以扩展或收缩,以存储int和string作为第一个Java项目。我试图用来表示表的数据结构是一个ArrayList of ArrayList,其中初始数组的元素都指向一个新的数组列表-因此初始数组类型充当了行的入口。这是一张我脑海中的照片,供参考:

我遇到的问题是访问内部数组列表。我已经阅读了一些文档,但我似乎无法理解为什么我无法访问内部列表的大问题。这里有一些代码:

import java.util.ArrayList;

public class Table {

    private int length, width;
    private ArrayList newTable;

    public Table() {
    this.length = this.width = 0;
    }

    /**
     * Testing a few functions
     */
    public static void main(String[] args) {
        // Just testing a few functions.
        Table list1 = new Table();
        list1.createTable(4, 4);
        list1.displayRow(1);
        list1.displayColumn(1);
        System.out.println("displayColumn done!");
        list1.displayEntireTable();
    }

    public void createTable(int tableLength, int tableWidth) {
        length = tableLength;
        width = tableWidth;

        this.newTable = new ArrayList();
        for (int i = 0; i < tableWidth; i++) {
            this.newTable.add(new ArrayList(tableLength));
        }
    }

    public void displayRow(int row) {
        System.out.println(this.newTable.get(row));
    }

    /**
     * This function displays the column of the table. Still work which
     * needs to be done here.
     * @param column 
     */
    public void displayColumn(int column) {
        if (this.newTable.size() >= column) {
            for (int i = 0; i < this.newTable.size(); i++) {
                // This doesn't work.
                System.out.println(this.newTable.get(i).get(column)); 
            }
        }
    }

    public void displayEntireTable() {

        for (int i = 0; i < this.newTable.size(); i++) {
        System.out.println(this.newTable.get(i));
        }
    }
}
import java.util.ArrayList;
公共类表{
私有整数长度、宽度;
私有数组列表newTable;
公共表(){
this.length=this.width=0;
}
/**
*测试一些函数
*/
公共静态void main(字符串[]args){
//只是测试一些功能。
表list1=新表();
列表1.createTable(4,4);
列表1.显示行(1);
列表1.显示列(1);
System.out.println(“displayColumn done!”);
list1.displayEntireTable();
}
public void createTable(int tableLength,int tableWidth){
长度=表格长度;
宽度=桌子宽度;
this.newTable=newArrayList();
对于(int i=0;i=列){
for(int i=0;i

我怀疑这个问题可能是由于在泛型中缺乏使用,而我对泛型还不太熟悉。因此,我要问你的问题是,stackoverflow,这个数据结构——ArrayList of ArrayList——是否可能,如果可能,我的问题出在哪里?

将不起作用的代码更改为:

System.out.println(((ArrayList) this.newTable.get(i)).get(column)); 
ArrayList中的ArrayList当然是可能的。
当然,禁止您使用的不是泛型。
泛型只是编译时检查错误,与此无关。您可以完全忽略它,不建议这样做,因为由于太多错误导致类强制转换异常的概率要大得多

解释。编译器不知道您的ArrayList包含ArrayList,因此在试图对对象而不是ArrayList调用get方法时,它无法识别该方法。因此,如果您不想在没有泛型的情况下使用它,那么解决方案是强制转换它。然而,我建议使用泛型并像这样定义您的列表

private List<List<String> newTable;

private List当然这是可能的,而且我怀疑你的问题实际上与泛型有关——如果你不使用泛型,你将不得不进行一系列的强制转换,这在你看来似乎根本不起作用

我会这样写

List<List<Object>> table;
列表表;

然后我会通过执行
table.add(new ArrayList())
来添加行,并使用
table.get(I).get(j)

访问元素。我认为问题在于您误解了
new arrarylist(tableLength)
调用的语义:它不会创建
tableLength
元素的数组列表;相反,它创建一个
ArrayList
,其初始容量至少足以容纳
tableLength
元素

我不确定您计划向ArrayList中的ArrayList添加什么类型的元素,但这里有一种方法可以测试创建二维ArrayList的代码:

for (int i = 0; i < tableWidth; i++) {
    ArrayList toAdd = new ArrayList(tableLength);
    for (int j = 0; j != tableLength ; j++) {
        toAdd.add(new Integer(i*tableLength +j));
    }
    this.newTable.add(toAdd);
}
for(int i=0;i
首先,你真正的问题是什么?您没有将任何数据添加到内部ArrayList中,因此它们是空的


另一方面,最好为行创建一个对象,并将这些对象存储在arraylist中

使用Java 1.7泛型改进:

import java.util.ArrayList;
import java.util.List;

public class Table {

    private int length, width;

    private List<List<String>> newTable;

    public Table() {
        this.length = this.width = 0;
    }

    /**
     * Testing a few functions
     */
    public static void main(String[] args) {
        // Just testing a few functions.
        Table list1 = new Table();
        list1.createTable(4, 4);
        list1.displayRow(1);
        System.out.println("displayRow done!");
        list1.displayColumn(1);
        System.out.println("displayColumn done!");
        list1.displayEntireTable();
        System.out.println("displayEntireTable done!");
    }

    public void createTable(int tableLength, int tableWidth) {
        length = tableLength;
        width = tableWidth;

        //by java 1.7 diamond feature, some generics can be hidden
        this.newTable = new ArrayList<>();
        for (int i = 0; i < tableWidth; i++) {
            List<String> columns = new ArrayList<>();
            for (int j = 0; j < tableLength; j++) {
                columns.add(new String("test"));
            } //added here
            this.newTable.add(columns);
        }
    }

    public void displayRow(int row) {
        System.out.println(this.newTable.get(row));
    }

    /**
     * This function displays the column of the table. Still work which
     * needs to be done here.
     * @param column 
     */
    public void displayColumn(int column) {
        for (int i = 0; i < this.newTable.size(); i++) {
            System.out.println("[" + this.newTable.get(i).get(column) + "]");
        }
    }

    public void displayEntireTable() {

        for (int i = 0; i < this.newTable.size(); i++) {
            System.out.println(this.newTable.get(i));
        }
    }
}
import java.util.ArrayList;
导入java.util.List;
公共类表{
私有整数长度、宽度;
私有列表表;
公共表(){
this.length=this.width=0;
}
/**
*测试一些函数
*/
公共静态void main(字符串[]args){
//只是测试一些功能。
表list1=新表();
列表1.createTable(4,4);
列表1.显示行(1);
System.out.println(“displayRow完成!”);
列表1.显示列(1);
System.out.println(“displayColumn done!”);
list1.displayEntireTable();
System.out.println(“displayEntireTable done!”);
}
public void createTable(int tableLength,int tableWidth){
长度=表格长度;
宽度=桌子宽度;
//通过Java1.7Diamond特性,可以隐藏一些泛型
this.newTable=newArrayList();
对于(int i=0;i