6.1 Array Creation and Access

  • Arrays are used to store one data type
  • Unlike Arraylists, arrays have a fixed size and cannot be changed
  • Arrays can be denoted using braces {} Below is an example of a simple array storing our scrum team names
[Meena, Shraddha, Madhumita, Pranavi]

To use an array you have to use the command

import java.util.Arrays;

Making Arrays

There are two ways to make arrays

  • Using Constructors
  • Using Pre-initialized Arrays
dataType[] arrayName = new dataType[numberOfItems]; //Constructor
int[] arraySample = {1,3,5,7,9}; //pre-initialized arrays

Accessing Elements in Arrays

  • You can access the elements in an array using different commands
arrayName.Length //determine the size
arrayName.length - 1 //to access the last item in the array