JAVA TUTORIAL

Java Intro Java Features Java vs C++ Java Simple Program Java Applet vs Application JVM,JDK or JRE Java Keywords Java Variables Java Literals Java Separators Java Datatypes Java Operators Java Statements Java Array Java String

CONTROL STATEMENTS

Java If-else Java While Loop Java Do-While Loop Java For Loop Java For-each Loop Java Switch Java Break/Continue

JAVA METHODS

Java Method Java Overloading Java Overriding

JAVA CLASSES

Java OOPs Java Classes/Objects Java Inheritance Java Polymorphism Java Encapsulation Java Abstraction Java Modifiers Java Constructors Java Interface Java static keyword Java this keyword

Java Arrays

Java Array is a very common type of data structure which contains all the data values of the same data type. The data items put in the array are called elements and the first element in the array starts with index zero. Arrays inherit the object class and implement the serializable and cloneable interfaces. We can store primitive values or objects in an array.

In simple words an array is a collection of similar types of data.

For example, if we want to store the names of 50 people then we can create an array of the string type that can store 50 names.

String[] array = new String[50];

In the above example array cannot store more than 50 names.


Array Variables

3 step process for using an array in your program:

1 Declaring your Array

2 Constructing your Array

3 Initialize your Array

1) Declaring your Array

Syntax

dataType[] arrayName;

or

dataType arrayName[];

dataType -> it can be primitive data types like int, char, double etc. or Java objects.

arrayName -> it is an identifier.

For example

int[] data;

or

int data[];

In this data is an array that can hold values of type int.

2) Constructing an Array

arrayName = new dataType[];

For example

intArray[] = new int[50];

In this, intArray will store 50 integer values

In Java, we can declare and allocate the memory of an array in one single statement.

For example

int intArray[] = new int[50];

3) Initialize an Array

In Java, we can initialize arrays during declaration.

For example

int age[] = {20,15,34,23,25,35}; //declare and initialize and array

In this, we have created an array named age and initialized it with the values inside the curly brackets.

Note -> we have not provided the size of the array. In this case, the Java compiler automatically specifies the size by counting the number of elements in the array (i.e. 6).

In Java array, each memory location is associated with a number. The number is known as an array index. We can also initialize arrays in Java, using the index number.

For example

int age[] = new int[5]; //declare an array

//initialize an array
age[0] = 23;
age[1] = 21;
age[2] = 22;
...

Note:

* Array indices always start from 0. That is, the first element of an array is at index 0.

* If the size of an array is n, then the last element of the array will be at index n-1.

Access Elements of an Array in Java

Syntax

age[index] //access array elements

First Array Program

class Simple{
    public static void main(String args[]){
    int age[] = {20,15,34,23,25,35}; //creating an array
        System.out.println("Printing an ages");
        System.out.println("age1: "+age[0]);
        System.out.println("age2: "+age[1]);
        System.out.println("age3: "+age[2]);
        System.out.println("age4: "+age[3]);
        System.out.println("age5: "+age[4]);
        System.out.println("age6: "+age[5]);
    }
}

Output:

age1: 20

age2: 15

age3: 34

age4: 23

age5: 25

age6: 35

In the above example, we are using the index number to access each element of the array.

With the help of loops to access all the elements of the array at once.

Example: Using For Loop

class Simple{
    public static void main(String args[]){
    int age[] = {20,15,34,23,25,35}; //creating an array
        System.out.println("Using for loop to printing an ages:");
    //using for loop
        for(int i = 0; i < age.length; i++){
        System.out.println(age[i]);
        }
    }
}

Output:

20

15

34

23

25

35

age.length

In the above example, we are using the length property of the array to get the size of the array.

We can also use the for-each loop to iterate through the elements of an array.

Example: Using the for-each Loop

class Simple{
    public static void main(String args[]){
    int age[] = {20,15,34,23,25,35}; //creating an array
        System.out.println("Using for-each loop to printing an ages:");
    //using for-each loop
        for(int i : age){
        System.out.println(i);
        }
    }
}

Output:

20

15

34

23

25

35

Java Multidimensional Arrays

A multidimensional array is an array of arrays.

To create a 2 -dimensional array, add each array within its own set of curly braces.

Example

int age[][] = new int[3][3];

age is now an array with two arrays as its elements.

Initialize a 2d array in Java:

int age[][] = { {23,12,43}, {34,23,53} };

Example: 2-dimensional Array

class Simple{
    public static void main(String args[]){
    int age[][] = { {20,15,34}, {23,25,35}, {12,25,14} };
        System.out.println("Using for-each loop to printing an ages:");
    //using for loop
        for(int i = 0; i < 3; i++){
            for(int i = 0; i < 3; i++){
                    System.out.print(age[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Output:

20 15 34
23 25 35
12 25 14