Single-Dimensional Arrays

A single-dimensional array stores a list of elements of the same type. We can make this list be any size that we want.

Declaring a single-dimensional array

We can declare a single-dimensional array like this:

type[] name;

This will declare an array called name that can hold elements of type type. For example, to declare an array of ints called nums, we can do:

int[] nums;

Before we can add any elements to the array, we need to allocate space for the array (i.e., specify the number of slots we want to reserve). Here’s how:

name = new type[size];

Here, name is the name of the array, type is the type of elements the array holds, and size is a positive integer that specifies how many slots we want in the array. size can be either a constant value (like 10) or an int variable. Here’s how to make our nums array have 10 slots:

nums = new int[10];

We can also declare an array and reserve space for it on the same line, like this:

int[] nums = new int[10];

Default array values

After we have allocated space for an array, all array slots are automatically filled with the default element for the type. In the case of any numeric type, the default value is 0. Here is what our nums array would look like after allocations space for 10 integers:

Array index 0 1 2 3 4 5 6 7 8 9
Array value 0 0 0 0 0 0 0 0 0 0

Initialization

Now that we have space for the array elements, we can start putting values in the array slots. Here’s how to access array elements:

name[index]

This accesses the element in array name at index index. C# array indices start at 0 and go up to size-1 (where size is the number of slots we reserved for the array). For example, here’s how we’d set the first element in our previous nums array to 7:

nums[0] = 7;

Here’s how we’d set the last element in nums to 4:

//9 is the last index since the array size is 10
nums[9] = 4; 

Here is what our nums array would look like after setting the first element to 7 and the last element to 9 (remembering that all other elements would retain the default value of 0)

Array index 0 1 2 3 4 5 6 7 8 9
Array value 7 0 0 0 0 0 0 0 0 4

Looping with an array

Arrays are very naturally processed with for loops. The loop counter is the array index – we start at 0, and we continue looping while the loop counter is less than the array size. Each time, we add one to the loop counter. For example, here’s how we’d use a loop to set every element in the nums array to 10:

for (int i = 0; i < 10; i++) 
{
    nums[i] = 10;
}

We can also retrieve the size of the array with the command:

name.length

where name is the name of the array. We could have rewritten the previous example to use nums.length instead, like this:

for (int i = 0; i < nums.length; i++) 
{
    nums[i] = 10;
}

Here is what our nums array would look like after the loop above:

Array index 0 1 2 3 4 5 6 7 8 9
Array value 10 10 10 10 10 10 10 10 10 10

Array example

Suppose we want to ask the user for 10 numbers, and then we want to print them in reverse order. Here’s how we could do that:

Scanner s = new Scanner(System.in);
int[] nums = new int[10];

for (int i = 0; i < nums.length; i++) 
{
    System.out.print("Enter a number: ");
    nums[i] = s.nextInt();
}
for (int i = nums.length-1; i >= 0; i--) 
{
    System.out.println(nums[i]);
}