Java Arrays

Fixed-size lists of values.

✍️ 2 exercises in this lesson

An array holds a fixed number of values of one type.

  • Create: int[] nums = {10, 20, 30}; or new int[5]
  • Access: nums[0]; length: nums.length
  • Multidimensional: int[][] grid = {{1, 2}, {3, 4}};
  • The Arrays class helps: Arrays.sort(), Arrays.toString(), Arrays.stream()

Key Takeaways: Java Arrays

Mastering Java Arrays helps you write clean and reliable Java code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

Java Arrays Code Example

Java
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] marks = {72, 95, 58, 88};
        Arrays.sort(marks);
        System.out.println(Arrays.toString(marks));
        System.out.println("Highest: " + marks[marks.length - 1]);
        System.out.println("Sum: " + Arrays.stream(marks).sum());

        int[][] grid = {{1, 2, 3}, {4, 5, 6}};
        System.out.println("Row 2, col 3: " + grid[1][2]);
    }
}
Output
[58, 72, 88, 95]
Highest: 95
Sum: 313
Row 2, col 3: 6
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

Java Arrays Practice Exercises

Check what you learned in “Java Arrays”.

/ 2 solved
Fill in the blank

Get the number of elements in the array.

int n = nums.;
✓ Correct! Not quite. Try again.
Multiple choice

Which class has a static sort() method for arrays?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services