Is there an append method for java arrays

Posted on 2006-05-19
Last Modified: 2012-06-27

I have several arrays in java, some ints, some strings etc... I would like to add (append) info to the end of them. How is this done?


Question by:waffe
    LVL 23

    Accepted Solution

    Arrays in Java can't grow.  If you can't use an Arraylist or Vector then you will have to write code to make the array "grow".

    Here's some code from this site:

    Changing an Array’s Size in Array
    An array’s length is a fixed length; to grow an array size you need to create a new array of the required size, and copy the existing elements in the old array to the new array, and fill in the rest with your new values.  Two ways to make a copy of an array:
    1. Use loops (for, while, etc) to copy individual elements

        // Create the original array
        int[] originalArray = { 1,5, 14, 33 };
        // Define 2nd array
        int[] arrayCopy = new int[originalArray.length];
        // Copy individual elements
        for (int element = 0; element < originalArray.length; element++)
        arrayCopy[element] = originalArray[element];

    2. Use System.arraycopy() method

        int[] originalArray = { 7, 4, 5, 2 };
        int[]arrayCopy = new int[originalArray.length];
        // Copy using arrayCopy()
        System.arraycopy(originalArray, // Source array
        0, // Source array position
        arrayCopy, // Target array
        0, // Target array position
    LVL 23

    Expert Comment

    And the code below from here:

        public int[] method01( SomeContainer rset )
            int[] bigArray = new int[ 1024 ];
            int count = 0;
            while( )
                if( count >= bigArray.length ) // time to grow!
                    int[] tmpArray = new int[ bigArray.length + 1024 ];
                    System.arraycopy( bigArray, 0, tmpArray,
                      0, bigArray.length );
                    bigArray = tmpArray;
                bigArray[ count ] = rset.getInt();
                count += 1;
            int[] smallArray = new int[ count ];
            System.arraycopy( bigArray, 0, smallArray, 0, smallArray.length );
            return smallArray;

    Author Comment

    Thanks I have been looking into Arraylist and Vectors - running into a few problems but I'll post if I need some help.



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now