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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:11:13+00:00 2026-06-14T08:11:13+00:00

Basically, I want to create a 2-D array that grows in size at runtime.

  • 0

Basically, I want to create a 2-D array that grows in size at runtime. Could I do this using a vector in Java?

Would these be right then?

int [] [] x = new x [100] [100];

Vector x= new Vector();

To add elements into the array-

for i=0 to 99
   for j=0 to 99
   x.addElement(x[i] [j]);

essentially, it would be just the same like referencing any other object, only here, I’ve to specify the index numbers as well, right?

I’m a novice at Java. So I’d appreciate any help!

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

    Unfortunately, Java has neither 2D arrays nor 2D growing vectors.

    It has array of arrays or vector of vectors.

    In your example, you created 1D vector of Objects:

    Vector x= new Vector();
    

    is similar to growing array of type Object[] x

    So, when you do

    y.addElement(x[i][j]);
    

    Java does “boxing”, i.e. it does

    y.addElement(new Integer(x[i][j]));
    

    Since you added 100×100 elements into 1D array, you will need to calculate location yourself

    y.get(i*numcols+j)
    

    So, to avoid all this, use vector of vectors like example below. The example copies newly fixed size array into vector of vectors:

       // creates fixed size 2D array with zeros
        int [] [] x = new int [50][50]; 
    
        // creates empty vector of vectors of integers
        // y is of type Vector<Vector<Integer>>
        // y.get(row) is of type Vector<Integer>>
        // y.get(row).get(col) is of type Integer
        Vector<Vector<Integer>> y = new Vector<Vector<Integer>>(); 
    
        // set the size of vector of vectors (number of rows)
        // each row will be null for now
        y.setSize(x.length);
    
        // enumerating rows
        for(int row=0; row<x.length; ++row) {
    
            log.info("row {}", row);
    
            // assign empty vector for row
            y.set(row, new Vector<Integer>());
    
            // set row size (number of columns)
            y.get(row).setSize(x[row].length);
    
            // enumerating columns of current row
            for(int col=0; col<x[row].length; ++col) {
    
                // setting the value for a cell
                y.get(row).set(col, x[row][col]);
    
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a dynamic array that will look like this, using years:
So basically I want to create a GUI app using PySide and the Qt
I have a flat array that I'm trying to make multidimensional. Basically, I want
I want to create a javascript form that basically has a bunch of text
Basically I want to create a multidimensional array (array of arrays) with data from
Basically I want to create one large object of many object in JavaScript. Something
I have php generating html and I want to create layers. Basically I want
I want to create own filetype to save objects in my app. Basically, I
I want to create a Google Docs document from within Haskell, so basically I
How to create imaged scrollbars, for example: http://www.openstudio.fr/jquery/index.htm Basically, I want to create my

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.