Results 1 to 4 of 4
  1. #1
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default Changing size of Array

    I have a String array of size 5. Is it possible to increase its size on the fly and retaining the data of the array.

    Java Code:
    String[] array = new String[5];
    
    array = new String[10];
    Thanks.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Here's an idea (pseudocode):
    Java Code:
        String[] array = new String[5];
        ...
        array = expand(array, 10);
    
    private String[] expand(String[] array, int size) {
        String[] temp = new String[size];
        System.arraycopy(array, 0, temp, 0, array.length);
        for(int j = array.length; j < size; j++)
            temp[j] = "";
        return temp;
    }

  3. #3
    Join Date
    Jun 2012
    Posts
    1
    Rep Power
    0

    Default Re: Changing size of Array

    Quote Originally Posted by hardwired View Post
    Here's an idea (pseudocode):
    Java Code:
        String[] array = new String[5];
        ...
        array = expand(array, 10);
    
    private String[] expand(String[] array, int size) {
        String[] temp = new String[size];
        System.arraycopy(array, 0, temp, 0, array.length);
        for(int j = array.length; j < size; j++)
            temp[j] = "";
        return temp;
    }
    The above code fails if you pass a null....
    here is a fix.
    Moderator edit: removed unformatted, irrelevant code that isn't a fix for anything.
    Last edited by DarrylBurke; 06-05-2012 at 08:19 PM. Reason: Removed crappy code

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Changing size of Array

    Quote Originally Posted by satyajeetahuja View Post
    The above code fails if you pass a null....
    Do you even understand the meaning of pseudocode? And the question was asked 4 years ago.

    Don't post to old dead threads, and go through these links before posting anything more here:
    Forum Rules
    Guide For New Members
    BB Code List - Java Programming Forum

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Doubling the size of an array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:42 PM
  2. Array size declaration
    By JT4NK3D in forum New To Java
    Replies: 3
    Last Post: 01-18-2008, 10:37 PM
  3. Changing icon of JOptionPane
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-21-2007, 07:01 AM
  4. changing the secondary ID of a view
    By schuetzejanett in forum SWT / JFace
    Replies: 0
    Last Post: 08-08-2007, 07:28 PM
  5. Maximum size of an array
    By Hasan in forum New To Java
    Replies: 1
    Last Post: 05-20-2007, 11:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •