Array is an object the contains elements of similar data type. It is a data structure where we store similar elements. We can store only fixed set of elements in a java array. Array in java is index based, first element of the array is stored at 0 index.
Advantage of array in Java
- We can access any element randomly by using indexes provided by arrays.
- Primitive type to wrapper classes object conversion will not happen so it is fast.
- Array can store many number of elements at a time.
Declaring Array variables
The first step to creating an array is creating a variable that will hold the array, just as you would any other variable. Array variables indicate the type of object the array will hold (just as they do for any variable) and the name of the array, followed by empty brackets ([]).
An alternate method of defining an array variable is to put the brackets after the type instead of after the variable. They are equivalent, but this latter form is often much more readable.
Creating an Array Objects
The second step is to create an array object and assign it to that variable. There are two ways to do this:
- Using new
- Directly initializing the contents of that array
The first way is to use the new operator to create a new instance of an array:
String[] names = new String[10];
That line creates a new array of Strings with ten slots, or elements. When you create the new array object using new, you must indicate how many elements that array will hold. Array objects can contain primitive types such as integers or booleans, just as they can contain
objects:
int[] temps = new int[99];
When you create an array object using new, all its elements are initialized for you (0 for numeric arrays, false for boolean, ‘\0’ for character arrays, and null for everything else). You can also create and initialize an array at the same time. Instead of using new to create the new array object, enclose the elements of the array inside braces, separated by commas:
String[] person = { “Ram”, “Arjun”, “Lakhan”, “Karan”, “Ganesh” };
Accessing Array Elements
Once you have an array with initial values, you can test and change the values in each slot of that array. To get at a value stored within an array, use the array subscript expression:
myArray[subscript];
The myArray part of this expression is a variable holding an array object, although it can also be an expression that results in an array. The subscript is the slot within the array to access, which can also be an expression. Array subscripts start with 0.
Changing Array Elements
To assign a value to a particular array slot, merely put an assignment statement after the array access expression:
myarray[1] = 15;
sentence[0] = “The”;
sentence[10] = sentence[0];
An important thing to note is that an array of objects in Java is an array of references to those objects When you assign a value to a slot in an array, you’re creating a reference to that object, just as you do for a plain variable. When you move values around inside arrays (as in that last line), you just reassign the reference; you don’t copy the value from one slot to another. Arrays of primitive types such asints or floats do copy the values from one slot to another.