Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7181437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:33:24+00:00 2026-05-28T17:33:24+00:00

I’m trying to create a table class, who’s rows and columns may expand or

  • 0

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:

enter image description here

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:

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));
        }
    }
}

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:33:25+00:00Added an answer on May 28, 2026 at 5:33 pm

    Using Java 1.7 generics improvements:

    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));
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.