How to extend an array in Java without changing its name
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!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;
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.
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
Use an ArrayList, it does all of that for you. Simply call add()
or addAll()
to append stuff.
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);
}
}
精彩评论