So basically I tried to set the lenght of an array with a variable named "a" and I declared it as 3 and int. When I try to see all the elements with for loop, I wouldn't get "null" as an element. So far so good. However, when someone wanted to add an element to that array, I add 1 to mentioned variable "a", hence prolonging the length of that array and allow me to add one more element to that array. But it doesn't work. Here are the codes:
public class Main {
public static void main(String[] args) {
int a=3;
String[] cars = new String [a];
cars[0]= "Camaro";
cars[1]= "corvet";
cars[2]= "tesla";
for(int i = 0; i<cars.length; i ) {
System.out.println(cars[i]);
}
a= a 1;
cars[3]="Honda";
System.out.println(cars[3]);
}
}
CodePudding user response:
Apart from the answers that correctly state "in Java, arrays are fixed length", it seems you misunderstand the exact meanings of declaration and assignment.
int a=3;
String[] cars = new String [a];
Here we have a variable named a
, given the value 3. Then we allocate an array, of a size specified by the current value of a
. As far as the array allocation is concerned, the effect is exactly the same as executing
new String[3];
There is no association between the array and a
.
Therefore, the subsequent execution of
a= a 1;
has no effect on the array, or indeed on any other data except for the value of a
itself. Even if arrays were not fixed length (which they are), changing an independent integer variable would not affect the array.
CodePudding user response:
Arrays are of fixed length in Java. You can't change length of array after it's declared. If you want new array with increased length you should create new array and copy the old contents. However in this particular case using ArrayList will do what you want. Here's the code:
ArrayList<String> cars = new ArrayList<>(); //Note that you don't provide any size
cars.add("camero");
cars.add("corvert");
cars.add("tesla");
cars.add("honda");
Here the array will be sized dynamically and you can add as much as you want.
If you still want extended array:
int a = 3;
//your code
a = 1;
String[] carsExtendedArray = new String[a];
for (int i = 0; i < cars.length; i ) {
carsExtendedArray[i] = cars[i];
}
carsExtendedArray[4] = "honda";
CodePudding user response:
Java arrays have a fixed length1. Said length is required at runtime. You can use a dynamic collection (such as ArrayList
). Or you can copy the array into a new larger array. Like,
String[] cars = { "Camaro", "corvet", "tesla" };
System.out.println(Arrays.toString(cars));
cars = Arrays.copyOf(cars, cars.length 1);
cars[3] = "Honda";
System.out.println(Arrays.toString(cars));
or
Collection<String> cars = new ArrayList<>(
Arrays.asList("Camaro", "corvet", "tesla"));
System.out.println(cars);
cars.add("Honda");
System.out.println(cars);
Both output
[Camaro, corvet, tesla]
[Camaro, corvet, tesla, Honda]
1JLS 10.2. Array Variables says (in part) Once an array object is created, its length never changes.
CodePudding user response:
The declaration of an array implies an expensive memory allocation operation, and as in other languages like C, the length of the array is always fixed. If you want to extend the length of an array you have to create a new one and then copy the contents with some like:
private static String[] arrayPush(String[] array, String element) {
String[] result = new String[array.length 1];
for (int i = 0; i < array.length; i ) {
result[i] = array[i];
}
result[array.length] = element;
return result;
}
Or shorter, with java.util.Arrays
:
cars = Arrays.copyOf(cars, cars.length 1);
cars[cars.length - 1] = "Honda";
However, as said above, the redeclaration of an array implies memory allocation and it's expensive, so you want to reduce as much as possible that operation. For that you use ArrayList
class.
I don't know the exact bytecode implementation, but you can see ArrayList
as if it holds internally an array, but with a length bigger than the number of elements it holds. This allows this class to add and remove elements without the need of declare and copy the entire array each time the size increases. If you add a new element and the underliying array is full, then increases the length of it (usually doubles it to reduce the number of times this operation is performed) in a manner similar to the one seen before.
CodePudding user response:
When you initialize the array, the parameter is sent by value, not reference. Changing the variable after that will have no impact.