Dreamer
0
Q:

set array length java

int[] array1 = new int[5]; //int array length 5
String[] array2 = new String[5] //String array length 5
double[] array3 = new double[5] // Double array length 5
19
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }
20
int[] theNumbers = new int[5];

arr[0] = 4;
arr[1] = 8;
arr[2] = 15;
arr[3] = 16;
arr[4] = 23;
arr[5] = 42;
8
/**
* An Example to get the Array Length is Java
*/
public class ArrayLengthJava {
public static void main(String[] args) {
String[] myArray = { "I", "Love", "Music" };
int arrayLength = myArray.length; //array length attribute
System.out.println("The length of the array is: " + arrayLength);
}
}
6
The size of an array cannot be changed after instantiation
If you need to change the size, create a new array and copy the contents
or use an ArrayList which does require a set size
1
int[] age = new int[5];
2
int[] array;

// is equivalent to

int array[];
1
int[] num = new int[5];
0

New to Communities?

Join the community