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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:43:00+00:00 2026-06-17T08:43:00+00:00

I have a problem related to storing data dynamically in two Dimensional String Array

  • 0

I have a problem related to storing data dynamically in two Dimensional String Array, String[][].
I am dynamically storing data in the String[i][j] array. Here the first index’s value is fixed i.e. i=3, but second index’s value is different for all rows.

for example, I am getting values like this,

String arrElements[][] = {
        {"1"},
        {"abc", "xyz", "lkm", "pwd", "srt", "qwert"},
        {"1234", "3456"}
        };

I am getting values something like this. i.e there is only 1 value in the first row, any number of values in the second row and in the third row.

If I go like this,

    int i = 0, j = 0;
    String arrElements[][] = {};
    arrElements= new String[3][25];
            //What size should I define here.
    arrElements[0][0] = "Sahil";
    if (a == 0) { //Its just a logical representation of what I might be doing.
        // Store the value in second row
        arrElements[1][i] = a;
        i++;
    }
    if (a == 1) {
        // Store the value in third row
        arrElements[2][j] = a;
        j++;
    }

Now, I am setting these values in the expandable list View. If the number of values in any of the rows exceeds the size specified, it gives ArrayOutOfBoundException. and if the size is less than 25, it shows empty rows.

Now, I dont want to give hard coded size limit for the array indexes. Is there any better way to deal with it.

  • 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-17T08:43:01+00:00Added an answer on June 17, 2026 at 8:43 am

    As a first remark: Are you sure a String[][] is the right data structure for what you want to achieve? There is a whole bunch of Collection classes that might be more suitable (ArrayList to name the most obvious).

    If you really want to proceed with String[][] you can’t define the length of the sub-arrays upfront but have to declare it per row:

    String[][] foo = new String[4][];
    foo[0] = new String[1];
    foo[1] = new String[2];
    // .... 
    

    But as I said you might be much happier with a nested ArrayList which resizes dynamically:

    ArrayList<ArrayList<String>> foo = new ArrayList<ArrayList<String>>();
    // Do the following for each row
    foo.add(new ArrayList<String>>());
    // Do the following to append data in row i
    foo.get(i).add("new string");
    // Do the following to retrieve column j in row i
    foo.get(i).get(j);
    

    Depending on what you actually want to store, other data structures might be much more suited.

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

Sidebar

Related Questions

I have a problem related to an array data which fetched from an external
I have fixed the problem of it not listing the data which was related
I have a performance problem related to string comparison (in Java). I'm working on
I have read a lot of related topics here regarding this problem but I
This problem is related to this question. Here is the data type I wish
I'm using EJB3 and I have a problem related to refreshing my EntityManager .
I have problem with my widget related to performance and memory: Issue : My
hello I am having problem related to https:// . I have used FB.getLoginStatus(function(response) function
This is a problem is related to worker role hosted VM. I have a
I have seen some related questions but none focusing on the specific problem I

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.