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 6788185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:27:18+00:00 2026-05-26T17:27:18+00:00

I would like to represent matrix-like data in a suitable data-structure in Java. The

  • 0

I would like to represent matrix-like data in a suitable data-structure in Java. The dimensions of this matrix depend on user-input. One way would probably be to use a “magic” max-constant, and use a simple multidimensional array. But operations on the data depend heavily on the dimension, and I would like to avoid fixed arrays, since I’d always need to keep track of the used vs. maximal dimension. A more dynamic approach of course is to define something like

private ArrayList<ArrayList<ArrayList<Point>>> arr3d = new ArrayList<ArrayList<ArrayList<Element>>>();

I think this is unacceptably ugly. It might be better to define a custom data-structure, i.e. a one-dimensional ArrayList (or Vector), and then somehow wrap this i.e. map an access specified as [i,j,k] to some element of that single list. Is it possible to overwrite the operator []? How to do this efficiently? Is there some existing library or code I could lean on?

PS: I thought this might be a common problem, but despite best efforts I have not found an existing question. Apologies in advance if I missed some answer.

  • 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-26T17:27:19+00:00Added an answer on May 26, 2026 at 5:27 pm

    Why don’t you create your own class to wrap that? It could contain your 3d list internally, but you expose only “user-friendly” methods to access and set elements.

    And no, in Java it’s not possible to overload []

    A quick helper for that:

    public class Cube<T> {
        private final List<List<List<T>>> elements = new ArrayList<List<List<T>>>();
    
        public T get(final int x, final int y, final int z) {
            if (elements.size() > x) {
                final List<List<T>> rowx = elements.get(x);
                if (rowx.size() > y) {
                    final List<T> rowy = rowx.get(y);
                    if (rowy.size() > z) {
                        return rowy.get(z);
                    }
                }
            }
            return null;
        }
    } 
    

    You can even do it in one line if you’re not worried about readability:

    public T get(final int x, final int y, final int z) {
        return (elements.size() > x && elements.get(x).size() > y && elements.get(x).get(y).size() > z ? elements.get(x).get(y).get(z) : null);
    }
    

    And you need put methods, that creates the rows (as ArrayList) when needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data represented in a 1300x1341 matrix. I would like to split
I have data being imported from a database and I would like to represent
I have a text file full of stopwords. I would like to represent this
I have around 3500 flood control facilities that I would like to represent as
I have an array of integers which represent a RGB image and would like
Would like to make anapplication in Java that will not automatically parse parameters used
I would like to sort a list of strings which represent paths. The sort
I would like to represent a value as a 64bit signed long , such
I would like to represent a datatype as a single column in my model,
I would like to have error codes in my Java project, but they should

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.