Declare and Print Array in Java: Explanation & Practice

Create and display array elements

Problem summary

Create an array of 5 integers and print all elements.

Starter code

public class Main {
    public static void main(String[] args) {
        // Declare array with values: 10, 20, 30, 40, 50
        // Print each element on a new line
    }
}

Expected output and test cases

  • Print all elements
    10
    20
    30
    40
    50

Hints

  1. int[] arr = {10, 20, 30, 40, 50};
  2. Use for loop to iterate
  3. Access elements with arr[i]

Related Arrays exercises

Practice all Arrays exercises · Run this idea in the Java compiler