Arrays
Been so long since I posted, Happy that i'm able to be back with basic concepts again.
Let's see what is Arrays:
As we know Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. It is a fixed-size sequential collection of elements of the same data type.
To declare an array, define the variable type with square brackets:
String[] fruits;
To place the values in like below we can use a comma-separated list, inside curly braces:
String[] fruit={"Apple",Orange","Strawberry"};
we can also directly declare and add values with just above line.
Access the Elements of an Array
You can access an array element by referring to the index number.
This statement accesses the value of the first element in fruits:
String[] fruits={"Apple",Orange","Strawberry"};
System.out.println(fruits[0]);
// Output Apple
Multidimensional Arrays in Java
A multidimensional array is an array of arrays. In Java, the most common type is the 2D array, but you can also have arrays with more than two dimensions.
An example of 2 dimensional is below:
Let's see few common operations we do with Arrays:
1.Arrays.sort() method for sorting.
2.Arrays.copyOf() Copying an Array:
Using System.arraycopy()
Hope we were able to understand Arrays today, let's see collections tomorrow. Happy Learning.