ARRAYS
Java Programming
GRADING SYSTEM EVALUATION
A school administrator is interested in assessing the performance of 'n' students in a class to provide constructive feedback and identify areas of
improvement. The school has records of the scores for each student in various subjects. Develop an algorithm and write a Java program to determine the
average score and, for each student, print the difference between their score and the average score.
ARRAYS
• A data structure that can store a fixed-size sequential collection of elements of the same (homogenous) type.
• An array as a collection of variables of the same type.
• The lowest index corresponds to the first element; the highest to the last element.
• Array indices start with zero.
• The elements have indices from 0 to n-1.
The amount of storage required to hold an array is directly related to its type and size.
For a single dimension array:
Total bytes = sizeof(base type) × length of array
WHY ARRAYS
Suppose we have to write a program that can accept salary of 50 employees. If we solve this problem using variables, we need 50 variables to store each
employee's salary. Remembering and managing these 50 variables is not easy and it will make the program complex and lengthy. This problem can be
solved by declaring 1 array having 50 elements; one for each employee's salary. Now we only have to remember the name of that 1 array.
ARRAYS – MEMORY LAYOUT
In Java, arrays are objects stored on the heap.
Each element is accessed by its index starting from 0.
Index 0 1 2 3
Name a[0] a[1] a[2] a[3]
Value 7 8 6 5
Starting address = Base address + index × data type size
ARRAY DECLARATION IN JAVA
Syntax:
dataType[] arrayName;
// or
dataType[] arrayName = new dataType[size];
Examples:
int[] a = new int[4];
float[] marks = new float[10];
// Using constant
final int SIZE = 4;
int[] a = new int[SIZE];
ARRAY INITIALIZATION
Integer array:
int[] a = {7, 8, 6, 5};
// or
int[] a = new int[]{7, 8, 6, 5};
// a[0]=7, a[1]=8, a[2]=6, a[3]=5
Character/String array:
char[] a = {'H','a','p','p','y','M','o','r','n'};
String str = "HappyMorn"; // Java strings are objects
ARRAYS – INITIALIZATION RULES
Declaration Result
int[] rank = {2,1,4,5,3}; 2, 1, 4, 5, 3
int[] rank = new int[6]; rank[0]=2; rank[1]=1; rank[2]=4; 2, 1, 4, 0, 0, 0 (rest default to 0)
int[] rank = {2,1,4,3,4}; 2, 1, 4, 3, 4
int[] arr = new int[5]; 0, 0, 0, 0, 0 (Java defaults to 0)
Note: In Java, all array elements are automatically initialized to their default values (0 for int, 0.0 for double, false for boolean, null for objects).
READING AND DISPLAYING AN ELEMENT IN AN ARRAY
int[] a = new int[4];
// Read third element
Scanner sc = new Scanner([Link]);
a[2] = [Link]();
// Display third element
[Link]("third element = " + a[2]);
ARRAY – SAMPLE CODE
import [Link]; Output:
public class ArrayExample { Enter 5 integers: 5
public static void main(String[] args) { 8 6 9 4
int[] a = new int[5]; Displaying integers:
Scanner sc = new Scanner([Link]); 5
[Link]("Enter 5 integers: "); 8
for (int i = 0; i < 5; i++) 6
a[i] = [Link](); 9
[Link]("Displaying integers:"); 4
for (int i = 0; i < 5; i++)
[Link](a[i]);
}
ARRAY – SAMPLE CODE (FIBONACCI SERIES)
public class FibonacciArray {
public static void main(String[] args) {
int[] fibonacci = new int[20];
fibonacci[0] = 0;
fibonacci[1] = 1;
for (int i = 2; i < 20; i++)
fibonacci[i] = fibonacci[i-2] + fibonacci[i-1];
for (int i = 0; i < 20; i++)
[Link]("fibonacci[" + i + "] = " + fibonacci[i]);
}
ARRAY – SCORE GREATER THAN AVERAGE SCORE
import [Link]; Output:
public class ScoreAverage {
Enter n: 5
public static void main(String[] args) {
Enter the marks: 60
Scanner sc = new Scanner([Link]);
70 50 80 90
[Link]("Enter n: ");
int n = [Link](); Average: 70.00
int[] a = new int[n];
Numbers > average: 80 90
int s = 0;
[Link]("Enter the marks: ");
for (int i = 0; i < n; i++) {
a[i] = [Link]();
s += a[i];
double avg = (double) s / n;
[Link]("Average: %.2f%n", avg);
[Link]("Numbers greater than average: ");
for (int i = 0; i < n; i++)
if (a[i] > avg)
[Link](a[i] + " ");
}
MCQ
What is the effect of executing the statement in Java?
int[] arr = {1, 2, 0, 0, 0}; // equivalent to partial init
Or equivalently:
int[] arr = new int[5];
arr[0]=1; arr[1]=2;
a) The first two elements are set to 1 and 2, rest are uninitialized.
b) The first two elements are set to 1 and 2, rest are set to 0. ← Ans: B
c) All elements are set to 1 and 2 alternatively.
d) The compiler generates an error.
MCQ
What are the elements present in the array of the following Java code?
int[] array = new int[5];
array[0] = 5;
In Java, all array elements default to 0 when created with new.
a) 5, 5, 5, 5, 5
b) 5, 0, 0, 0, 0 ← Ans: B
c) 5, (garbage), (garbage), (garbage), (garbage)
d) (garbage), (garbage), (garbage), (garbage), 5
PRACTICE QUESTIONS
• Print elements of array in reverse order
• Print sum of elements of an array
• Print maximum and minimum element in an array
• Program to search an element in an array (Linear & Binary search)
• Copying an array to another
• Print elements of an array in ascending order