15

I wonder if it's possible to extend an array in Java but without changing its name, since I have multiple methods linked to this array. I was thinking of creating a new array with the same name but twice as big, and then copy all elements from the first array to the second. Is this possible?
Basically I want to make an array with accounts at a bank, and if the customer creates so many accounds that the array doesn't have enough elements, it should extend itself.
Thank you for any replies!

2
  • not a Java dev, but if you need an array with a dynamic size isn't a typed Vector a good option ? Commented Dec 16, 2010 at 16:52
  • So at the moment my array looks like this: SimpleAccount [] AccountArray = new SimpleAccount[10]; then how do I change it to an ArrayList? Commented Dec 16, 2010 at 17:28

5 Answers 5

16

Even if using an ArrayList is probably a good advice in many circumstances, there are perfectly legitimate occasions for using plain old arrays.

In that case, if you need to resize your array, you might want to investigate one of the java.utils.Arrays.copyOf methods. Please note however those won't really resize your array. They will merely create a new array and copy common items.

If the new array has a size greater than the old one, the new items will be initialized to some default value (i.e.: false for boolean[], null for T[] -- see the documentation for details). You have to use those function like that:

myArray = copyOf(myArray, myNewSize); 

Remember however that this method will always return a new array. Even if the requested size is the same as the original one. If this in not desirable, you will have to write something like that:

myArray = (myNewSize > myArray.length) ? copyOf(myArray, myNewSize) : myArray;
Sign up to request clarification or add additional context in comments.

Comments

8

You cannot change the array you have, but can create a new array with the desired type and size, copy data from the original array to the newly created, and then assign it to the original array variable.

But you shouldn't do this.

Instead of an array use any implementation of java.util.List. ArrayList is a good choice for this case, I think.

1 Comment

Sometimes there is a perfectly legitimate reason why you don't want to use an ArrayList. For example if the source is an array which was created by some other component you cannot change.
6

Here is a method that i wrote for my Java Programming Class

private static double[] extendArraySize(double [] array){
    double [] temp = array.clone();
    array = new double[array.length + 1];
    System.arraycopy(temp, 0, array, 0, temp.length);
    return array;
}

It's pretty self explanitory on how it works, although on the way it's used it must have the array assigned to the method with the arrays name passing as the argument, and the data type of the method must always be the type of array passed in.

example:

double [] z = {1,2,3};  //size of z = 3; [0,1,2]   VALUES: [1,2,3]
z = extendArraySize(z); //size of z = 4; [0,1,2,3] VALUES: [1,2,3,0];

Sorry if it's explained terribly and i'm sure there's a better way of doing it VIA vectors or ArrayLists. But if you're going to want to extend a primitive array, this is one way of doing it.

Hope I helped =D

1 Comment

This solution is pretty light on memory, since it allocates only the array what it needs. On the other hand: you don't need to clone the array into the temp if you return the extended array anyway.
5

Use an ArrayList, it does all of that for you. Simply call add() or addAll() to append stuff.

2 Comments

Great! So at the moment my array looks like this: SimpleAccount [] AccountArray = new SimpleAccount[10]; then how do I change it to an ArrayList?
@Kajsa: ArrayList<SimpleAccount> accounts = new ArrayList<SimpleAccount>();
0
public static void extendarrayafterinitialization(int [] x){
        Scanner input =new Scanner(System.in);
        System.out.print("How many values do you need to insert to the Array : ");
        int how=input.nextInt();
        int length=x.length+how;
        int [] all=new int[length];
        int value=0;
        System.out.println(length);
        for(int i=0;i<x.length;i++){
            all[i]=x[i];
            }
            for(int j=x.length;j<length;j++){
                System.out.print("Enter new value : ");
                int index=input.nextInt();
                all[j]=index;
                }
        for(int print : all){
            System.out.println(print);
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.