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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:18:35+00:00 2026-05-27T09:18:35+00:00

I know you can’t dynamically expand a normal array but is this a valid

  • 0

I know you can’t dynamically expand a normal array but is this a valid way of doing it?

public int size = 0;    
public String[] OrigArray = new String[size+1]; 

public void expand(){


            String[] tempArray = new String[size+1];    

            tempArray = (String[])OrigArray.clone();

            OrigArray = new String[size+1];

            OrigArray = (String[])tempArray.clone();    

            size++;         

    }

I’m aware of much better methods than trying to use a normal array but I’d like to figure this for just using a normal array first.

My desire is that it starts off with OrigArray being 0+1 (so 1) and when expand() is called the new tempArray is made that is the same size as the OrigArray and this then holds OrigArray while OrigArray is declared again with size+1 then the tempArray is copied back to the newly sized OrigArray. This makes sense to me, but I keep getting out of bound exception?

  • 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-27T09:18:36+00:00Added an answer on May 27, 2026 at 9:18 am

    The method does not change the value of OrigArray; all it does is store a clone of a clone in it, so in effect the value isn’t changed.

    I think what you want is this:

    public void expand() {
        String[] newArray = new String[OrigArray.length + 1];
        System.arraycopy(OrigArray, 0, newArray, 0, OrigArray.length);
    
        //an alternative to using System.arraycopy would be a for-loop:
        // for(int i = 0; i < OrigArray.length; i++)
        //     newArray[i] = OrigArray[i];
        OrigArray = newArray;
    }
    

    This creates an array that has a size 1 greater than OrigArray, copies the content of OrigArray into it and assigns that array to OrigArray. Unless you want to remember how many times expand() has been called, there shouldn’t be a reason to have the variable size.

    EDIT: If what you really want is to know a way to sensibly implement the functionality you asked for, you can go with what @Óscar López said and use ArrayList.

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

Sidebar

Related Questions

This is something I know can be done somehow , because I've done it
I have a query that I know can be done using a subselect, but
You can know if the event stack is empty calling the gtk.events_pending() method, but
I know I can do most of this by hacking Trac and using Git
I know you can look at the row.count or tables.count, but are there other
I know can use string.find() to find a substring in a string. But what
i just want to know can we pass nsdata or array datatype from one
Does anyone know how can I replace this 2 symbol below from the string
I know you can do redirection based on the domain or path to rewrite
I know I can programatically make the taskbar item for a particular window start

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.