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

  • Home
  • SEARCH
  • 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 549733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:12:52+00:00 2026-05-13T11:12:52+00:00

I was wondering what the simplest way would be to implement an array who’s

  • 0

I was wondering what the simplest way would be to implement an array who’s rank is specified at runtime.

The example I am working on stores a array of boolean values for lattice points, and I want the user to be able to chose how many spatial dimensions the model uses at runtime.

I’ve looked at the Array.newInstance() method:

dimensionOfSpace = userInputValue;  // this value comes from GUI or whatever
int latticeLength = 5;  // square lattice for simplicity

int[] dimensions = new int[dimensionOfSpace];
for(int i = 0; i < l.length; i++) l[i] = length; 
Object lattice = Array.newInstance(boolean.class, dimensions);

But accessing these values in any sort of way seems to require horribly slow methods such as recursively using Array.get until the returned value is no longer an array, i.e. using isArray().

Am I missing an obvious solution here? I would love to be able to access the values in a way similar to foo[i][j][k].

  • 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-13T11:12:53+00:00Added an answer on May 13, 2026 at 11:12 am

    Looks like what you are looking for is for some way to declare how many dimensions an array has at runtime. I don’t know how this could be done using a multidimensional ArrayList, or any multidimensional structure where you have to specify the dimensionality at compile time.

    The only answer I see is to use a simple linear array wrapped in a class that converts multidimensional coordinate to and from the its position in the underlying array. This is basically how languages such as C stores multidimensional arrays by using one contiguous chunk of memory.

    The code would look something like this:

    import java.util.*;
    
    class MultiArray<T>{
        private int[] dimensions;
        private Object[] array;
    
        public MultiArray(int ... dimensions){
            this.dimensions=dimensions;
            //Utils.product returns the product of the ints in an array
            array=new Object[Utils.product(dimensions)];
        }
    
        public void set(T value, int ... coords){
            int pos=computePos(coords); 
            array[pos]=value;
        }
    
        public T get(int ... coords){
            int pos=computePos(coords);
            return (T)(array[pos]);
        }
    
        private int computePos(int[] coords){
            int pos=0;
            int factor=1;
            for (int i=0;i<coords.length;i++){
                pos+=factor*coords[i];
                factor*=dimensions[i];
            }
            return pos;
        }
    }
    
    class Main{
        public static void main(String args[]){
            MultiArray<Integer> m=new MultiArray<Integer>(new int[]{5,4,3}); 
            Random r=new Random();
    
            for(int i=0;i<5;i++)
                for(int j=0;j<4;j++)
                    for(int k=0;k<3;k++)
                        m.set(r.nextInt(),i,j,k);
            for(int i=0;i<5;i++){
                for(int j=0;j<4;j++){
                    for(int k=0;k<3;k++)
                        System.out.print(m.get(i,j,k)+" ");     
                    System.out.println("");
                }
                System.out.println("\n");
            }
        }
    }
    
    class Utils{
        public static int product(int...a){
            int ret=1;
            for (int x:a) ret*=x;
            return ret;
        } 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.