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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:50:52+00:00 2026-06-18T05:50:52+00:00

Java supports multi-dimensional arrays, which are represented as arrays of arrays. For instance, I

  • 0

Java supports multi-dimensional arrays, which are represented as “arrays of arrays”. For instance, I can create an array of String arrays using the following code:

int rows = ...
int cols = ...
String[][] array2d = new String[rows][cols];

What I’d like to do is have the second dimension be “nulled out”. In other words, a for-loop like for(String[] array : array2d) System.err.println(array); would print out:

null
...
null

The reason I’d like to do this is because I have already allocated a bunch of String[] instances that I want to just drop into array2d. I have a couple of solutions but both seem sub-optimal for the following reasons:

Solution 1

I could just do something like String[][] array2d = new String[rows][0], use a for-loop to null out the first dimension, and populate the rows later, but this seems ugly to me because Java will create a new empty String[] for every row, and I really don’t need it to.

Solution 2

I could also do something like String[][] array2d = new String[][]{null, null, ... null}, but this is even worse because I have to hard code the length of the first dimension of array2d via the bracketed section of code, which is disgusting.

I recognize that this isn’t a huge problem, especially when the dimensionality is small, and any programmer worth his salt would choose some other construct over creating arrays of many dimensions. I’m mostly just curious if there is a way to partially allocate dimensions of a multi-dimensional array at construction.

  • 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-06-18T05:50:53+00:00Added an answer on June 18, 2026 at 5:50 am

    Easiest solution possible:

    int rows = ...
    String[][] array2d = new String[rows][];
    

    If you don’t initialize the second dimension, you array will have null entries in dimension 1.

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

Sidebar

Related Questions

In C# there is DataTable type which can be used to create multi-column structures.
Is there any Java open source library that supports multi-character (i.e., String with length
How should implement multi-thread in single-threaded Operating system using java? Java supports Multi-threading.Is there
Is there any open source CMS in a java/xml platform which supports REST based
Possible Duplicate: Prepend lines to file in Java I have a CSV-file which supports
Is there a Java lib for line-based diff between multi-line text chunks, that supports
I searched Java's internal representation for String, but I've got two materials which look
is it possible to create a function in java that supports any number of
Possible Duplicate: Can we run Java applictions on iPhone? Does IPhone4 supports Java? Can
I want to know if LevelDB supports java ? Where can i get the

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.