Skip to main content

Introduction to Java Programming

  What is Java? Java is a programming language and a platform. Java is a high-level, robust, object-oriented and secure programming language. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Firstly, it was called  Greentalk  and the file extension was  .gt . After that, it was called  Oak . Initially Java was designed for small, embedded systems in electronic appliances like set-top boxes. Platform:  Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API(Application Programming Interface), it is called a platform. The principles for creating Java programming were "Simple, Robust, Portable, Platform-independent, Secured, High performance, Multithreaded, Architecture neutral, Object-oriented, Interpreted, and Dynamic". Currently, Java is used in internet programming, mobile devices, games, e-business so

12. Arrays in Java

 

Arrays

  • An array is a collection of similar types of data having contiguous memory allocation.
  • The indexing of the array starts from 0, i.e. 1st element will be stored at the 0th index, 3rd at 2nd index and so on.
  • The size of the array can not be increased at run time therefore we can store only a fixed size of elements in array.
  • If the array is initialized at the time of declaration, then the dimension of the array is optional.
  • An array can also be initialized by taking input of values from the keyboard with the help of loops.
  • If the array elements are not given any specific values, then they contain garbage values.
  • The arrays are categorized into two types as follows:
    1. One-dimensional array
    2. Multi-dimensional array

One Dimensional Array

  • It is the simplest type of array.
  • It is a finite set of homogeneous elements stored in contiguous memory locations.
  • Declaring an array means specifying the data type and size of array.
  • After arrays are declared, it can be initialized.
import java.util.Scanner;

class ArrayInJava {
    public static void main(String[] arr) {
        int[] marks1 = new int[3]; // Declaration and Memory allocation of array
        marks1[0] = 25; // Initialization of array
        marks1[1] = 16;
        marks1[2] = 21;

        int[] marks2; // Declaration of array
        marks2 = new int[3]; // Memory allocation of array
        marks2[0] = 25; // Initialization of array
        marks2[1] = 16;
        marks2[2] = 21;

        int[] marks3 = { 25, 16, 21 }; // Declaration, Memory allocation and Initialization of array

        String name[] = { "Deepak", "Chandni", "Neetu", "Sheela", "Gaurav", "Deependra", "Diksha", "Prajjwal",
                "Garvit" };

        int[] array;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the number of elements: ");
        int size = input.nextInt();
        array = new int[size];
        for (int i = 0; i < size; i++) {
            System.out.print("Enter element " + (i + 1) + ": ");
            array[i] = input.nextInt();
        }

        System.out.println("The length of your array is " + array.length);
        System.out.println("Element at index '0' in your array = " + array[0]);
input.close();
    }
}

Output :

Enter the number of elements: 5
Enter element 1: 1
Enter element 2: 2
Enter element 3: 3
Enter element 4: 4
Enter element 5: 5
The length of your array is 5
Element at index '0' in your array = 1

Accessing Array Elements

class ArrayTraversing {
    public static void main(String[] arr) {
        double[] array = { 10, 12, 14 };

        System.out.println("Printing the array using NAIVE method...");
        System.out.print(array[0] + " ");
        System.out.print(array[1] + " ");
        System.out.print(array[2] + " ");

        System.out.println("\nPrinting the array using FOR loop...");
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }

        System.out.println("\nPrinting the array using FOR-EACH loop...");
        for (double element : array) {
            System.out.print(element + " ");
        }

        System.out.println("\nPrinting the array using FOR loop in reverse order...");
        for (int i = array.length - 1; i >= 0; i--) {
            System.out.print(array[i] + " ");
        }
    }
}

Output :

Printing the array using NAIVE method...
10.0 12.0 14.0
Printing the array using FOR loop...
10.0 12.0 14.0
Printing the array using FOR-EACH loop...
10.0 12.0 14.0
Printing the array using FOR loop in reverse order...
14.0 12.0 10.0

For Each Loop in Java

  • For each loop is an enhanced version of for loop and starts with the keyword for.
  • Instead of declaring and initializing a loop counter variable, we declare a variable that is the same type as the base type of the array, followed by a colon, which is then followed by the array name.
  • In the loop body, we can use the loop variable we created rather than using an indexed array element.
  • It's commonly used to iterate over an array or a collection elements.
  • The drawback of the for-each loop is that it cannot traverse the elements in reverse order.
  • Moreover, we cannot skip any element while traversing.

Multi-dimensional Arrays

  • It is an array of arrays' having similar data types.
  • Each elements of a multi-dimensional array is an array itself.

2-D Array

class TwoDimensionalArray {
    public static void main(String[] arr) {
        int[][] flats = new int[2][3];
        flats[0][0] = 001;
        flats[0][1] = 002;
        flats[0][2] = 003;
        flats[1][0] = 101;
        flats[1][1] = 102;
        flats[1][2] = 103;
        System.out.println("Printing the 2-D array...");
        for (int i = 0; i < flats.length; i++) {
            for (int j = 0; j < flats[i].length; j++) {
                System.out.print(flats[i][j] + "\t");
            }
            System.out.print("\n");
        }
    }
}

Output :

Printing the 2-D array...
1            2          3
101       102      103


Previous

Next

Comments