Introduction

A collection groups related values in a particular order. Indexes start at zero in these languages. Check whether a collection is empty before accessing its first value, and avoid changing its length while iterating unless you intend to.

Syntax & example

public class Main {
  public static void main(String[] args) {
    String[] foods = {"pasta", "rice", "soup"};
    System.out.println(foods[2]);
  }
}

Expected output

soup

Common mistakes

  • Using a variable before assigning a value to it.
  • Mixing text and numbers without converting deliberately.
  • Skipping an empty input or a boundary value when testing.

Practice

Create a collection of three favorite foods and display the last one.

13px
Main.javaYour ideas, in code.
Loading your editor…
Run your code to see the output here.
Ready to buildUTF-86 lines