Creating Arrays

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > Arrays


Introduction

Arrays are an essential tool for all programmers which will allow you to group related pieces of information together, individual pieces accessible with the array index.

In this section, we will discuss how to create arrays.


Creating an Array

An array can store a group of values of the same primitive type such as chars, ints, doubles, etc.. Creating an array is similar to creating any other types of primitives in Java. First, we must create an array variable. There are two valid formats for creating an array variable and they are shown below:

 int[] numbers; // For consistency, we will be using this format throughout the Wiki. 

or

 int numbers[]; 


The statement above declares numbers as an array-reference variable. The numbers variable can only reference an array of int values. To initialize the variable, use the new key word to create the array and assign its address to the numbers variable. The number inside the brackets is the array's size declarator. The size declarator indicates the number of elements/values the array can hold. An example is shown below:

 numbers = new int[10]; 


The above statement declares and initializes an int array of size 10. However, the index of arrays always starts from position 0, instead of 1, and ends at position (length-1), instead of length. Here (shown below), the array length is 10 and the last index of the array is 9.

Array.JPG


We should always remember that the array size declarator must be a positive integer and an array of any data type can be declared. Here are some examples:

 char[] characters = new char[10]; // An array of chars

long[] numbers = new long[50]; // An array of long ints

double[] size = new double[100]; // An array of doubles 


The following example shows another way to declare arrays, where we use two reference variables of the Integer type:

 int[] numbers,size; 

or

 int numbers[],size[]; 


In the previous examples, we have shown how to declare an array with a constant for its size. We can also declare the size of the array without literal numbers. Here is an example:

 int array_size = 6;
int[] numbers = new int[array_size]; 

Note: if you change the size variable, you will not change the size of the array after it has been initialized.


Also, you can declare and initialize an array at the same time. Here is an example:

 int[] numbers={1,2,3} 

This statement declares the reference variable numbers, creates an array in memory and stores the initial values (1, 2, 3) in the memory space. This is an initial array and the elements are stored in order. The Java compiler determines the size of the array by counting the number of items.


Summary

In this section, you have learned how to create an array. Similar to other types of data in Java, you will need to declare and initialize an array variable. You have learned that the array indices start from position 0, instead of 1, and end at position (length-1).

In the next section, we will be discussing about Entering & Using Array Elements.

Previous Page: User-Defined Methods Next Page: Entering and Using Array Elements