?
Solved

Is there an append method for java arrays

Posted on 2006-05-19
3
Medium Priority
?
4,919 Views
Last Modified: 2012-06-27
Hi,

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?

Thanks,

waffe
0
Comment
Question by:waffe
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
basicinstinct earned 200 total points
ID: 16723074
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: http://www.hostitwise.com/java/javaarrays_1.html


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
    originalArray.length);
0
 
LVL 23

Expert Comment

by:basicinstinct
ID: 16723078
And the code below from here:
http://www.javaworld.com/javaworld/jw-08-1999/jw-08-cooltools.html

    public int[] method01( SomeContainer rset )
    {
        int[] bigArray = new int[ 1024 ];
        int count = 0;
        while( rset.next() )
        {  
            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;
    }
0
 

Author Comment

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

Thanks,

waffe
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses
Course of the Month15 days, 21 hours left to enroll

850 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