Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 6 of 10

Arrays

Arrays are fixed-size, ordered collections of elements of the same type. They are the simplest data structure in Java - the foundation for understanding Collections. Knowing when to use an array vs a List is an important design decision.

12 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Arrays - Declaration, Access, Iteration

Arrays - Declaration, Access, Iteration
import java.util.Arrays;

public class ArraysDemo {
    public static void main(String[] args) {
        // Declare and initialize
        int[] scores = {90, 85, 78, 92, 88};   // preferred
        String[] names = new String[3];          // fixed size, null by default
        names[0] = "Alice";
        names[1] = "Bob";
        names[2] = "Charlie";

        // Access by index (0-based)
        System.out.println("First score: " + scores[0]);   // 90
        System.out.println("Last score: " + scores[scores.length - 1]); // 88

        // Iterate with for-each (preferred when index not needed)
        int total = 0;
        for (int score : scores) total += score;
        double average = (double) total / scores.length;
        System.out.printf("Average: %.2f%n", average);     // 86.60

        // Sort and search
        Arrays.sort(scores);  // {78, 85, 88, 90, 92}
        System.out.println(Arrays.toString(scores));

        int idx = Arrays.binarySearch(scores, 88);  // 2 (found at index 2)
        System.out.println("Index of 88: " + idx);

        // 2D array - matrix (rows × columns)
        int[][] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };
        System.out.println("Center: " + matrix[1][1]);  // 5

        // Copy an array
        int[] copy = Arrays.copyOf(scores, scores.length);
        int[] partial = Arrays.copyOfRange(scores, 1, 4); // {85, 88, 90}
    }
}

Array vs List - When to Use Which

  • Array: fixed size, primitives allowed, slightly faster, low-level APIs
  • List (ArrayList): dynamic size, only objects, more utility methods - prefer for most application code
  • Array use cases: static lookup tables, pixel buffers, performance-critical numerical work
  • List use cases: almost everything else - use List<String> over String[]

Quick Quiz

Tip

Tip

Practice Arrays in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Arrays

Practice Task

Note

Practice Task - (1) Write a working example of Arrays from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Arrays is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.

Key Takeaways

  • Arrays are fixed-size, ordered collections of elements of the same type.
  • Array: fixed size, primitives allowed, slightly faster, low-level APIs
  • List (ArrayList): dynamic size, only objects, more utility methods - prefer for most application code
  • Array use cases: static lookup tables, pixel buffers, performance-critical numerical work

Topics in This Module

0/10 done