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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:43:11+00:00 2026-05-26T10:43:11+00:00

If it is possible, how can I create a static multidimensional array in Java

  • 0

If it is possible, how can I create a static multidimensional array in Java with different primitive datatypes per dimension?

By static, I mean the primitive array that is not dynamic like an ArrayList would be.

  • 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-26T10:43:12+00:00Added an answer on May 26, 2026 at 10:43 am

    Dimensions in an Array are always from type int. Think about it!

    int a = 4;
    int b = 5;
    
    Shoe shoe = new Shoe (Color.RED, 42, "Leather");
    Hat hat = new Hat (17, Color.Black);
    
    Foo foo = foos[a][b];
    Zilch pop = bars[shoe][hat]; // no go
    

    If you have a multidimensional array of Foos, the first dimension is an Foo, the second an array of Foos, the third an array of array of Foo. The only variable type is the one at the bottom.

    Edit after update of question:

    Arrays aren’t called static or primitive. Their size is fixed on initialization, and what they have in common with primitives is, that they are a buildin, which is threated special in some cases. They are – in contrast to the so called primitive types, which aren’t that primitive (they have, for example, operators, exclusively for their own, like * / -) but meanwhile they are objects, but not declared in the library.

    Call them build in-types.

    Using Bhesh Gurung’s trick:

    Object[] arr = {new Integer[]{}, new String[]{}, new Double[]{}}; 
    

    is begging for trouble, and it is not made of different datatypes per dimension. Let’s start with the dimensions:

    // One-dimensional object:
    JPanel [] panels = new JPanel [3]; 
    // Two-dimensional object:
    JPanel [][] panels = new JPanel [3][10]; 
    

    You have JPanels on the bottom level, and an Array of JPanel on the next dimension. You can add more dimension, and will always get an additional (Array of …) wrapped around.

    You can not mix different datatypes in an Array like int and char, or JPanel and JFrame, or int and JButton. Only if you abstract over the difference, and use an JComponent for JPanel and JFrame as common parent, but this will not work for the build-in types int, char, boolean and so on, because they aren’t objects.

    But can’t you use autoboxing, and use Integer instead of int, Character instead of char, and then use Object as common parent class? Yes, you could, but then you’re not using the primitives any more, and you’re begging for troubles.

    Dan is talking about a different thing – using differnt types for indexing in the multi-dimensional array:

    byte  b = 120;
    short s = 1000;
    String o [][] = new String[b][s];
    b = 7;
    s = 9;  
    o[b][s] = "foobar";
    String foo = o[b][s];
    

    There is no problem using bytes or shorts, but you can’t restrict the size of an Array by declaring it as byte or short. In most cases the boundaries of a build-in integer type will not fit to a datatype (think 365 days per year), especially, since all types might get negative, so bounds-checking is necessary although and can’t be restricted to compile time.

    But now to the trouble:
    We could declare the array as two-dimensional from the beginning:

    Object[][] ar2 = {
        new Integer [] {4, 5, 6}, 
        new String [] {"me", "and", "you"}, 
        new Character [] {'x', 'y', 'z'}};
    

    That works fine, and makes the inner arrays accessible immediately without casting. But it is only known for the compiler, that the elements are Object arrays – the underlying type is abstracted away, and so we can write:

    ar2[1][1] = 17; // expected: String
    ar2[2][0] = "double you"; // expected: Char
    

    This will compile flawlessly, but you’re shooting yourself in the foot and get a Runtime exception for free.

    Here is the source as a whole:

    public class ArrOfMixedArr
    {
        public static void main (String args[])
        {
            Object[] arr = {
                new Integer [] {1, 2, 3}, 
                new String [] {"you", "and", "me"}, 
                new Character [] {'a', 'b', 'c'}};
            show (arr);
            
            byte b = 7;
            short s = 9;
            String o [][] = new String[200][1000];
            o[b][s] = "foobar";
            String foo = o[b][s];
            
            Object[][] ar2 = {
                new Integer [] {4, 5, 6}, 
                new String [] {"me", "and", "you"}, 
                new Character [] {'x', 'y', 'z'}};
            show (ar2);
    
            // exeptions:
            ar2[1][1] = 17; // expected: String
            ar2[2][0] = "double you"; // expected: Char
        }
    
        public static void show (Object[] arr)
        {
            for (Object o : arr) 
            {
                if (o instanceof Object[])
                    show ((Object[]) o);
                else 
                    System.out.print (o.toString () + "\t");
            }
            System.out.println ();
        }
    }
    

    Now what is the solution?
    If your base-types arrays of (int, byte, char, String, JPanel, …) are of equal length, then you have something like a hidden Object, a database-row. Use a class instead:

    class Shoe {
        byte size;
        String manufactor;
        java.math.BigDecimal price;
        java.awt.Color color;
    }
    
    Shoe [] shoes = new Shoe [7];
    

    If you don’t have different types of the same size, they might be unrelated, and should not be put in a common container.

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

Sidebar

Related Questions

I just start learning WPF, is that possible I can create an IE toolbar
Is it possible to create a multidimensional list in C#? I can create an
Is it possible to create an attribute that can be initialized with a variable
Is it possible to create a list that can be access by either an
Is it possible to create a library that can be consumed from .Net 2.0
It's possible to create digrams in SQL Server 2000 that can be useful to
So I want to create a text box static if possible that when my
I would like to create a static helper method that I can call from
Possible Duplicate: How I can create installer for website. PHP mysql I'm looking to
Can any one tell me if its possible to create a stored procedure in

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.