⚡0 XP🔥0

Arrays

Lesson 6 of 15 ⏱ 6 min read ✎ 2 exercises ❓ 5-question quiz ▶ Runs Java

Create and use 1D and 2D arrays, array length, default values, the Arrays utility class (sort, toString, fill, binarySearch, copyOf), and common array algorithms.

An array stores a fixed number of values of the same type. In Java, arrays are objects: they live on the heap, know their own length, and every index is checked at run time.

Creating arrays#

int[] marks = new int[5];                 // 5 zeros
int[] primes = {2, 3, 5, 7, 11};          // with values
String[] names = new String[3];           // 3 nulls
double[] prices = new double[]{9.5, 12.0};
▶ Try it YourselfJava
Ctrl + Enter to run
Output
First: 78, last: 71
marks[0] = 78
marks[1] = 92
marks[2] = 70
marks[3] = 88
marks[4] = 71
Average: 79.8
false null
हिंदी में समझें

Java में array एक object है - उसका size length से मिलता है (बिना brackets)। Index 0 से शुरू होता है। अगर आप गलत index (जैसे marks[5]) इस्तेमाल करें तो C की तरह चुपचाप गलत value नहीं मिलती, बल्कि ArrayIndexOutOfBoundsException आती है - इससे bugs जल्दी पकड़ में आते हैं।

▶ Try it YourselfJava
Ctrl + Enter to run
Output
Error: Index 3 out of bounds for length 3

The Arrays class#

java.util.Arrays has ready-made helpers:

▶ Try it YourselfJava
Ctrl + Enter to run
Output
6 elements: [64, 25, 12, 22, 11, 90]
Sorted: [11, 12, 22, 25, 64, 90]
Index of 22: 2
Copy: [11, 12, 22, 25, 64, 90, 0, 0]
Part: [12, 22, 25]
Filled: [7, 7, 7, 7]
Equal? false
[Bhopal, Indore, Ujjain]

Arrays are references#

Assigning an array variable copies the reference, not the elements:

▶ Try it YourselfJava
Ctrl + Enter to run
Output
a = [99, 2, 3]
a = [99, 2, 3], c = [99, 55, 3]

This also means a method that receives an array can change the caller's elements.

Common algorithms#

▶ Try it YourselfJava
Ctrl + Enter to run
Output
Reversed: [42, 23, 16, 15, 8, 4]
15 at index 3
Rotated: [16, 15, 8, 4, 42, 23]
1:2 3:3 9:1 

2D arrays#

A 2D array is an array of arrays. Each row can even have a different length (a jagged array):

▶ Try it YourselfJava
Ctrl + Enter to run
Output
Student 0 total 253, average 84.3
Student 1 total 207, average 69.0
Student 2 total 275, average 91.7
1 
1 1 
1 2 1 

Matrix multiplication#

▶ Try it YourselfJava
Ctrl + Enter to run
Output
[[19, 22], [43, 50]]

Summary#

  • Arrays are fixed-size objects; use .length; indexes are checked (exceptions for bad indexes).
  • Default values: 0, false, or null.
  • Arrays.toString, sort, binarySearch, copyOf, fill, equals, deepToString save time.
  • Array variables hold references - use clone() or Arrays.copyOf to copy.
  • 2D arrays are arrays of rows; rows can have different lengths.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Second largest element

Read n and n integers, then print the second largest distinct value as Second largest: 78.

Test input:
7
45 78 12 92 78 33 92
Expected output:
Second largest: 78
2

Row and column sums

For the 3×3 matrix in the starter, print each row sum and each column sum:

Row sums: 6 15 24
Column sums: 12 15 18
Expected output:
Row sums: 6 15 24
Column sums: 12 15 18

Quick Quiz

Q1.

What is the default value of elements in new int[5]?

Q2.

How do you get the number of elements of array a?

Q3.

What happens when you access a[10] in a 5-element array?

Q4.

What does Arrays.toString(new int[]{3, 1, 2}) return?

Q5.

int[] b = a; then b[0] = 99; - what is a[0]?