Arrays

Agenda

  • Definition and Syntax

  • Input / Output

  • for-each loop

Arrays

Java array is a data structure that stores data of the same type in a sequential manner. An array takes a contiguous section of the memory.

class Main {
    public static void main(String[] args) {
        // Array with initial values (size determined automatically)
        int[] arr1 = {4, 8, 9, 1, 0};
        
        // Empty array with size = 5. Initially all values are 0.
        int[] arr2 = new int[5];
        
        // Alternate declarations
        int []arr3 = new int[5];
        int arr4[] = new int[5];
        int [] arr5 = new int[5];
    }
}

Given the marks of students in a classroom, calculate the average marks.

Classroom Average

class Main {
    public static void main(String[] args) {
        int marks1 = 50;
        int marks2 = 45;
        int marks3 = 82;
        int marks4 = 76;
        int marks5 = 92;
        int marks6 = 83;
    }
}

Given the marks of students in a classroom, calculate the average marks.

Classroom Average

class Main {
    public static void main(String[] args) {
        int marks1 = 50;
        int marks2 = 45;
        int marks3 = 82;
        int marks4 = 76;
        int marks5 = 92;
        int marks6 = 83;
        // ... 
        int marks10 = 91;
    }
}

Given the marks of students in a classroom, calculate the average marks.

Assume class size to be 10.

Classroom Average

class Main {
    public static void main(String[] args) {
        int[] marks = new int[10];
        
    }
}

Given the marks of students in a classroom, calculate the average marks.

Assume class size to be 10.

Classroom Average

class Main {
    public static void main(String[] args) {
        int[] marks = new int[10];
        
        marks[0] = 10;
        marks[1] = 20;

    }
}

Given the marks of students in a classroom, calculate the average marks.

Assume class size to be 10.

Classroom Average

class Main {
    public static void main(String[] args) {
        int[] marks = new int[10];

        marks[0] = 10;
        marks[1] = 20;

        System.out.println(marks[0]);   // 10
        System.out.println(marks[1]);   // 20

    }
}

Given the marks of students in a classroom, calculate the average marks.

Assume class size to be 10.

Classroom Average

class Main {
    public static void main(String[] args) {
        int[] marks = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

        System.out.println(marks[0]);   // 1
        System.out.println(marks[1]);   // 2

    }
}

Given the marks of students in a classroom, calculate the average marks.

Assume class size to be 10.

Classroom Average

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr= new int[n];

//        Input loop
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

//        Output loop
        for (int i = 0; i < n; i++) {
            System.out.println(arr[i]);
        }
    }
}

Array - Input / Output

Linear Search

Given an array and a key value as input, find the index of the key in the array.

Return -1  if it is not present.

 

Sample Input

5

10 20 30 40 50

40

 

Sample Output

3

Linear Search

Given an array and a key value as input, find the index of the key in the array.

Return -1  if it is not present.

 

Sample Input

5

10 20 30 40 50

40

 

Sample Output

3

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];

//        Input loop
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        
        int key = sc.nextInt();
        
        
    }
}

Linear Search

Given an array and a key value as input, find the index of the key in the array.

Return -1  if it is not present.

 

Sample Input

5

10 20 30 40 50

40

 

Sample Output

3

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];

//        Input loop
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        int key = sc.nextInt();

        int ans = -1;

//        Iterate over the array and update ans if key is found
        for (int i = 0; i < n; i++) {
            
        }

        System.out.println(ans);
    }
}

Linear Search

Given an array and a key value as input, find the index of the key in the array.

Return -1  if it is not present.

 

Sample Input

5

10 20 30 40 50

40

 

Sample Output

3

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];

//        Input loop
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        int key = sc.nextInt();

        int ans = -1;

//        Iterate over the array and update ans if key is found
        for (int i = 0; i < n; i++) {
            if (arr[i] == key) {
                ans = i;
                break;
            }
        }

        System.out.println(ans);
    }
}

The for each loop in Java, also called as 'enhanced for loop', was introduced in Java 5. It is one of the alternative approaches that are used for traversing the iterable. As the name suggests, it is mainly used to iterate over each of the iterable elements one by one.

for-each loop

class Main {
    public static void main(String[] args) {
        int[] arr = {5, 3, 2, 4, 1};
        for (int num : arr) {
            System.out.println(num);
        }
    }
}

Homework

1. Given an integer N, generate & print an array containing squares of all numbers from 1 to N.

 

2. Read two arrays from the user and generate a third array containing all the common elements from the first two.

 

3. Given an array, find the maximum and the minimum elements in it.

 

4. Given an array, generate a new array that is reverse of it.

Arrays

By Tarun Luthra

Arrays

  • 46