Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does one access an ArrayList of ArrayLists? (Generics?)
    primarykey
    data
    text
    <p>I'm trying to create a table class, who's rows and columns may expand or shrink, to store ints and strings as a first Java project. The data structure I'm trying to use to represent the table is an ArrayList of ArrayLists, where the initial array's elements all point to a new array list - so the initial array kind of serves as an entrance into rows. This would be a picture of how I have it in my mind, for reference: </p> <p><a href="https://i.stack.imgur.com/wiXUa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wiXUa.png" alt="enter image description here"></a></p> <p>The problem I'm having is accessing the inner ArrayLists. I've been reading a bit of documentation, and I can't seem to understand the big issue with why I'm not able to access the inner lists. Some code here: </p> <pre><code>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 &lt; 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() &gt;= column) { for (int i = 0; i &lt; 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 &lt; this.newTable.size(); i++) { System.out.println(this.newTable.get(i)); } } } </code></pre> <p>I'm suspicious that the problem may rely the lack of use in generics, which I'm not quite as familiar with yet as I would like to be. So my question to you, stackoverflow, is whether this data structure - an ArrayList of ArrayLists - is even possible, and if so, where lays my problem?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload