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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:31:19+00:00 2026-05-15T10:31:19+00:00

Suppose we have the Java code: Object arr = Array.newInstance(Array.class, 5); Would that run?

  • 0

Suppose we have the Java code:

Object arr = Array.newInstance(Array.class, 5);

Would that run? As a further note, what if we were to try something like this:

Object arr1 = Array.newInstance(Array.class, 2);
Object arr2 = Array.newInstance(String.class, 4);
Object arr3 = Array.newInstance(String.class, 4);
Array.set(arr1, 0, arr2);
Array.set(arr1, 1, arr3);

Would arr1 then be a 2D array equivalent to:

String[2][4] arr1;

How about this: what if we don’t know the dimensions of this array until runtime?

Edit: if this helps (I’m sure it would…) we’re trying to parse an array of unknown dimensions from a String of the form

[value1, value2, ...]

or

[ [value11, value12, ...] [value21, value22, ...] ...]

And so on

Edit2: In case someone as stupid as I am tries this junk, here’s a version that at least compiles and runs. Whether or not the logic is sound is another question entirely…

Object arr1 = Array.newInstance(Object.class, x);
Object arr11 = Array.newInstance(Object.class, y);
Object arr12 = Array.newInstance(Object.class, y);
...
Object arr1x = Array.newInstance(Object.class, y);
Array.set(arr1, 0, arr11);
Array.set(arr1, 1, arr12);
...
Array.set(arr1, x-1, arr1x);

And so on. It just has to be a giant nested array of Objects

  • 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-15T10:31:20+00:00Added an answer on May 15, 2026 at 10:31 am

    It is actually possible to do in java. (I’m a bit surprised I must say.)

    Disclaimer; I never ever want to see this code anywhere else than as an answer to this question. I strongly encourage you to use Lists.

    import java.lang.reflect.Array;
    import java.util.*;
    
    public class Test {
    
        public static int[] tail(int[] arr) {
            return Arrays.copyOfRange(arr, 1, arr.length);
        }
    
        public static void setValue(Object array, String value, int... indecies) {
            if (indecies.length == 1)
                ((String[]) array)[indecies[0]] = value;
            else
                setValue(Array.get(array, indecies[0]), value, tail(indecies));
        }
    
        public static void fillWithSomeValues(Object array, String v, int... sizes) {
            for (int i = 0; i < sizes[0]; i++)
                if (sizes.length == 1)
                    ((String[]) array)[i] = v + i;
                else
                    fillWithSomeValues(Array.get(array, i), v + i, tail(sizes));
        }
    
        public static void main(String[] args) {
    
            // Randomly choose number of dimensions (1, 2 or 3) at runtime.
            Random r = new Random();
            int dims = 1 + r.nextInt(3);
    
            // Randomly choose array lengths (1, 2 or 3) at runtime.
            int[] sizes = new int[dims];
            for (int i = 0; i < sizes.length; i++)
                sizes[i] = 1 + r.nextInt(3);
    
            // Create array
            System.out.println("Creating array with dimensions / sizes: " +
                    Arrays.toString(sizes).replaceAll(", ", "]["));
            Object multiDimArray = Array.newInstance(String.class, sizes);
    
            // Fill with some 
            fillWithSomeValues(multiDimArray, "pos ", sizes);
    
            System.out.println(Arrays.deepToString((Object[]) multiDimArray));
    
    
        }
    }
    

    Example Output:

    Creating array with dimensions / sizes: [2][3][2]
    [[[pos 000, pos 001], [pos 010, pos 011], [pos 020, pos 021]],
     [[pos 100, pos 101], [pos 110, pos 111], [pos 120, pos 121]]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 438k
  • Answers 438k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try this if it helps : CGSize itemSize = CGSizeMake(kAppIconWidth,… May 15, 2026 at 4:33 pm
  • Editorial Team
    Editorial Team added an answer This is a possible solution (if you can change MyClass):… May 15, 2026 at 4:33 pm
  • Editorial Team
    Editorial Team added an answer You may force a floating point division like any of… May 15, 2026 at 4:33 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.