Difference between revisions of "Creating Arrays"

From CompSciWiki
Jump to: navigation, search
(Quick change of typo on variable.)
Line 17: Line 17:
  
  
This statement declears ''numbers'' as an array-reference-variable.  The ''numbers'' varibale can only reference an array of int values.The next step is to use the ''new'' key word to create an array and assign its address to the ''numbers'' variable.  The following statement shows an example.
+
This statement declears ''numbers'' as an array-reference-variable.  The ''numbers'' variable can only reference an array of int values.The next step is to use the ''new'' key word to create an array and assign its address to the ''numbers'' variable.  The following statement shows an example.
  
  

Revision as of 22:46, 2 December 2007

COMP 1010 Home > Arrays


Introduction

This topic contains information on how to create arrays

   

{{{Body}}}

Creating an Array

(ABU)

In this section, we will discuss how to create an array. An array can store a group of values of the same type (e.g. char, int, or String). Creating and using an array in java is similar to creating and using any other type of object. First we must create a variable to contain the object. The formats for creating array variables are shown below:


 
int [] numbers;
    or
int numbers[];


This statement declears numbers as an array-reference-variable. The numbers variable can only reference an array of int values.The next step is to use the new key word to create an array and assign its address to the numbers variable. The following statement shows an example.


numbers = new int[10];


The number inside the brackets is the array's size declarator. It indiactes the number of elements or values an array can hold. We can also declare a reference variable and create an instance of an array with one statement. Here is an example:


int[] numbers = new int[10];
          or
int numbers[] = new int[10]; 


The above statements create an array of size 10. But the index of array always starts from 0 and it ends at length-1. Here the array length is 10 and the last index of array is 9.

Array.JPG


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



char[] characters = new char[10];

long[] numbers = new long[50];

double[] size = new double[100];


The first example declares a character-type array, second one a long-type and last one a double-type array.


The following example shows another way to declare arrays, where we use two reference variables of integer type.


int [] numbers,size;
        or
int numbers[],size[];


In the previous examples we declared the arrays with a constant for its size. We can also declare the array where the size of array in a variable. We can use this variable instead of a literal number when we refer to the size of the array. Here is an example:


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

Note: if you change the size varible, you will not change the size of the array.


You can also create and initialize an array at the same time. Here is an example:


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

This statement declares the reference varibale numbers, create an array in memory and stores initial values in the memory space. This is an initial array. The elements are stored in order. You may think how java compiler detemines the size of the array. Java compiler determines the size of the array by counting the number of items.