How to expand an array in Java without changing its name

I wonder if it is possible to expand the array in Java, but without changing its name, since I have several methods associated with this array. I thought about creating a new array with the same name, but twice as many, and then copied all the elements from the first array to the second. Is it possible? Basically I want to create an array with bank accounts, and if the client creates so many statements that there are not enough elements in the array, it should expand.
Thanks for any answers!

+7
source share
5 answers

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

But you should not do that.

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

+9
source

Even if using an ArrayList is probably good advice in many cases, there are perfectly legitimate uses of plain old arrays.

In this case, if you need to resize the array, you can study one of the java.utils.Arrays.copyOf methods. Please note that this will not change the size of your array. They will simply create a new array and copy the common elements.

If the new array is larger than the old one, the new elements will be initialized with some default value (i.e. false for boolean[] , null for T[] - see the documentation for the Details). You should use the following functions:

 myArray = copyOf(myArray, myNewSize); 

Remember, however, that this method will always return a new array. Even if the requested size matches the requested size. If this is not desired, you will have to write something like this:

 myArray = (myNewSize > myArray.length) ? copyOf(myArray, myNewSize) : myArray; 
+7
source

Here is the method 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; } 

This is a fairly independent explanation of how this works, although along the path that he used it should have an array assigned to the method with the name of the array passed as an argument, and the data type of the method should always be the type of the passed array.

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 he explained horribly, and I'm sure there is a better way to do this with VIA vectors or ArrayLists. But if you want to expand a primitive array, this is one way to do this.

I hope I helped = D

+4
source

Use an ArrayList , it does it all for you. Just call add() or addAll() to add stuff.

+3
source
 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); } } 
0
source

All Articles