DEV Community

Indumathy
Indumathy

Posted on

Array

What is an Array?
An array is a collection of similar data types stored in a continuous memory location.
Arrays are used when we need to store multiple values in a single variable instead of creating many separate variables.
Example
Instead of writing:
int a = 10;
int b = 20;
int c = 30;
We can store them in one array:
int[] arr = {10,20,30};

Array Declaration in Java
Array declaration means creating a reference variable for the array.
Two ways to declare an array:

int[] arr;
or
int arr[];
Both are valid in Java.

**Array Creation
**After declaration, we need to allocate memory for the array.

arr = new int[5];

This creates an array that can store 5 integer values.

Declaration + Creation (Shortcut)
We can also do both together:

int[] arr = new int[5];

Array Initialization

Initialization means assigning values to the array elements.

Method 1: Using Index

arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;

Here, the numbers inside [ ] are called index values.
Array index always starts from 0.

Method 2: Shortcut Initialization

int[] arr = {10,20,30,40,50};

This is the easiest way to initialize an array.

Printing All Elements of an Array:

We can use a for loop to print all elements.

public class Main
{
public static void main(String[] args)
{
int[] arr = {10,20,30,40,50};

    for(int i = 0; i < arr.length; i++)
    {
        System.out.println(arr[i]);
    }
}
Enter fullscreen mode Exit fullscreen mode

}

Output

10
20
30
40
50
arr.length returns the size of the array.

Accessing Required Index

We can access any specific element using its index number.

public class School
{
public static void main(String[] args)
{
int[] arr = {10,20,30,40,50};

    System.out.println(arr[3]);
}
Enter fullscreen mode Exit fullscreen mode

}

Output

40

Explanation:

Index : 0 1 2 3 4
Value : 10 20 30 40 50
So arr[3] returns 40.

Array Index Out of Bounds Exception
If we try to access an index that does not exist, Java will throw an exception.

System.out.println(arr[10]);

ArrayIndexOutOfBoundsException
Because the array size is only 5, and index 10 does not exist.

Iteration in array

Iteration means repeating a block of code multiple times until the condition become false. In Array we can do iteration using for loop.

int[] marks = {10,20,30,40};
for (int i= 0; i<marks.length; i++)
{
System.out.println(marks[i]);
}

Output:
10
20
30
40

from the above value if we want only 3 values than we need to do changes in for loop.

for(int i =; i<marks.length-1; i++)

How to finde length of array

int[] marks = {10,20,30,40};
System.out.println(marks.lenghth);

length is predefined intance variable for array.

Reversing the values

int[] marks = {10,20,30,40};
for (int i= marks.length-1; i>=0; i--)
{
System.out.println(marks[i]);
}
Output:
40
30
20
10

String data type in array:

Example for printing first letter in each name.

String[] names= {"indu", "kavin", "nethra"};
for(int i =; i<marks.length-1; i++)
{
String a= names[i];
System.out.println(a.charAt(i)); // charAt is predefined method in String class
}
Output:
i
k
n

Example for changing first letter to capital for all values.

String[] names= {"indu", "kavin", "nethra"};
for(int i =; i<marks.length-1; i++)
{
String a= names[i];
Character b = a.charAt(i);
System.out.println(b.toUpperCase(b));
}

Output:
I
K
N

Top comments (0)