Home Java Programs Oops String Exception Multithreading
Home Java Programs Oops String Exception Multithreading
Java Arrays
Normally, an array is a collection of similar type of elements which has contiguous memory
location.
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. It is a data structure where
we store similar elements. We can store only a fixed set of elements in a Java array.
Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd
element is stored on 1st index and so on.
Unlike C/C++, we can get the length of the array using the length member. In C/C++, we need to
use the sizeof operator.
In Java, array is an object of a dynamically generated class. Java array inherits the Object
class, and implements the Serializable as well as Cloneable interfaces. We can store primitive
values or objects in an array in Java. Like C/C++, we can also create single dimentional or
multidimentional arrays in Java.
Moreover, Java provides the feature of anonymous arrays which is not available in C/C++.
Advantages
Code Optimization: It makes the code optimized, we can retrieve or sort the data
efficiently.
Disadvantages
Size Limit: We can store only the fixed size of elements in the array. It doesn't grow its
size at runtime. To solve this problem, collection framework is used in Java which grows
automatically.
arrayRefVar=new
datatype[size];
Let's see the simple example of java array, where we are going to declare, instantiate, initialize
and traverse an array.
//Java Program to
illustrate how to
d l i i
Test it Now
Output:
10
20
70
40
50
int a[]=
{33,3,4,5};//declarat
i i i i
//Java Program to
illustrate the use of
d l i
Test it Now
Output:
33
3
4
5
for(data_type
variable:array){
//b d f h l
Let us see the example of print the elements of Java array using the for-each loop.
//Java Program to
print the array
l i f
Output:
33
3
4
5
Let's see the simple example to get the minimum number of an array using a method.
//Java Program to
demonstrate the way
f i
Test it Now
Output:
//Java Program to
demonstrate the way
f i
Test it Now
Output:
10
22
44
66
//Java Program to
return an array from
h h d
Test it Now
Output:
10
30
50
90
60
ArrayIndexOutOfBoundsException
The Java Virtual Machine (JVM) throws an ArrayIndexOutOfBoundsException if length of the
array in negative, equal to the array size or greater than the array size while traversing the
array.
//Java Program to
demonstrate the case
f
Test it Now
Output:
dataType[][]
arrayRefVar; (or)
d T []
int[][] arr=new
int[3][3];//3 row and
3 l
arr[0][0]=1;
arr[0][1]=2;
[0][2] 3
Let's see the simple example to declare, instantiate, initialize and print the 2Dimensional array.
//Java Program to
illustrate the use of
l idi i l
Test it Now
Output:
1 2 3
2 4 5
4 4 5
//Java Program to
illustrate the jagged
Test it Now
Output:
0 1 2
3 4 5 6
7 8
Test it Now
Output:
I
Copying a Java Array
We can copy an array to another by the arraycopy() method of System class.
Test it Now
Output:
caffein
//Java Program to
clone the array
l T 1{
Output:
//Java Program to
demonstrate the
ddi i f
Test it Now
Output:
2 6 8
6 8 10
//Java Program to
multiply two matrices
bli l
Test it Now
Output:
6 6 6
12 12 12
18 18 18
Related Topics
1) Java Program to copy all elements of one array into another array
12) Java Program to print the sum of all the items of the array
32) Java Program to find the frequency of odd & even numbers in the given matrix
← Prev Next →
Feedback
Preparation
Company
Interview
Questions
Company
Questions
Trending Technologies