xxxxxxxxxx
// 10 Strings in the array named: arr
String[] arr = new String[10];
xxxxxxxxxx
String[] array = new String[] { "String1", "String2", "String3" }; // Create array
String string3 = array[2]; // Get array
xxxxxxxxxx
datatype[] varname = new datatype[size];
// Always see output after converting to string
some_array = Arrays.toString(varname); // for single dimension array
some_array = Arrays.deepToString(varname); // for multi dimension array
System.out.println(some_array);
xxxxxxxxxx
// datatype var = new datatype[size]
int[] arr = new int[10];
// OR
int arr[] = new int[10];
xxxxxxxxxx
String[] animals ={"Dog", "Cat", "Fish", "Turtle", "Penguin"}; // Array of animals
xxxxxxxxxx
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars[0]);
// Change elements in array
cars[0] = "Opel";
System.out.println(cars[0]);
// Length of array
System.out.println(cars.length);
// Loop through array
for (int i = 0; i < cars.length; i++) {
System.out.println(cars[i]);
}
xxxxxxxxxx
Java array is an object which contains elements of a similar data type.
Additionally, The elements of an array are stored in a contiguous memory
location.
Basically, it is one of the a data structure where we store similar elements.
We can store only a fixed set of elements in a Java array.
following shows the use of the array in java,
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
//after printing array
default value of arrray element is 0;
[10,20,0,0,0]