Java Arrays
Java Arrays
Now we can define an array as a variable that is a collection of elements of the same data
type and stored at contiguous memory locations. The size of an array is specified at the time
of declaration and hence the size is fixed.
The arrays that we are going to discuss in this tutorial are Fixed-size or Static arrays.
Java also supports dynamic arrays called “ArrayList” whose size can be altered on the fly. We
will discuss ArrayList in detail later.
In this program, we have declared an int array and then instantiated it using new. Then
we have initialized elements at indices 0 and 1 with values 1 and 3 respectively.
Finally, we print all the elements of the array. Note that the elements we did not
initialize are having default values as 0 since the type of array is int.
JAVA PROGRAM TO ILLUSTRATE STORE AND
PRINT MARKS OF 5 STUDENTS
JAVA ARRAYSINDEXOUTOFBOUNDSEXCEPTION
A common mistake of all programmers while using arrays is they try to
access indexes which are outside the limit. For example, if an array is of
length 6 then the program can use any index of the array in between 0 and
5. But sometimes the program tries to access elements outside this range.
That is when the compiler throws a ArraysIndexOutOfBoundsException
error.
•
The ArrayIndexOutOfBounds e
xception is thrown if a program
tries to access an array index that
is negative, greater than, or equal
to the length of the array.
JAVA PROGRAM TO ILLUSTRATE THE USE OF ARRAYS IN A
PROGRAM:
As you can see, we are
converting the int value
into double. This is called
type casting in Java.
PASSING AN ARRAY TO A METHOD OR FUNCTION
We can also pass the Java array to methods or functions, just like normal
variables. Passing an array to method helps us to reuse the same logic on any
array.
When we pass an array as an argument to a method, it is actually the address of
the first element of the array (base address) which is passed as a reference.
Therefore, if we make any changes to this array in the method, it will affect the
array.
Syntax:
When we pass an array to a method, we specify the name of the array without
any square brackets within the method call.
MULTI-DIMENSIONAL ARRAYS
Multi-dimensional arrays are arrays of arrays in which each element of the
array holds the reference of other arrays. We can also call them Jagged Arrays.
The most commonly used multi-dimensional array is a two-dimensional
array.
A two-dimensional array is an array in which each element is a one-
dimensional array.
For example, a two-dimensional array myArray [ M ][ N ] is an M by N table
with M rows and N columns containing M N elements.
By multiplying the number of rows with the number of columns we can
determine the number of elements in a two-dimensional array. For example,
the number of elements in an array arr [7][9] is calculated as 7 9 = 63.
SYNTAX OF DECLARING TWO-DIMENSIONAL
ARRAYS IS:
PROGRAM TO UNDERSTAND THE TWO
DIMENSIONAL ARRAY
ARRAY OF OBJECTS IN JAVA
Java is an object-oriented programming language. Most of the work done
with the help of objects. We know that an array is a collection of the same
data type that dynamically creates objects and can have elements of primitive
types. Java allows us to store objects in an array. In Java, the class is also a
user-defined data type. An array that conations class type elements are
known as an array of objects. It stores the reference variable of the object.