Difference between revisions of "Creating Arrays"

From CompSciWiki
Jump to: navigation, search
(Just fixed a few typos. Nothing major.)
Line 1: Line 1:
{{1010Topic|Introduction=This topic contains information on how to create arrays|Overview=Arrays can be created using the following syntax:
+
{{1010Topic|Introduction=Before you can learn how arrays can make some calculations easier you need to know how to make an array.
  
Object array[] = new Object[100];|Chapter_TOC=[[Arrays]]}}
+
This section covers precisely that... Creating and Editing Arrays.|Overview=Once you have completed this section you will be able to:
 +
*Create arrays
 +
*Edit Arrays
 +
}}
  
 
=='''Creating an Array'''==
 
=='''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:
 
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:
Line 11: Line 12:
  
 
<pre>  
 
<pre>  
int [] numbers;
+
int[] numbers;
 
     or
 
     or
 
int numbers[];
 
int numbers[];
 
</pre>
 
</pre>
  
 +
For consistency throughout this text we will use the first way, even though both are considered valid Java code.
  
 
This statement declares ''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.
 
This statement declares ''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.
Line 31: Line 33:
 
<pre>
 
<pre>
 
int[] numbers = new int[10];
 
int[] numbers = new int[10];
          or
 
int numbers[] = new int[10];
 
 
</pre>
 
</pre>
 
  
 
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.
 
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.

Revision as of 09:17, 3 December 2007

COMP 1010 Home > Back to Chapter Topics


Introduction

Before you can learn how arrays can make some calculations easier you need to know how to make an array.

This section covers precisely that... Creating and Editing Arrays.

   

{{{Body}}}

Creating an Array

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[];

For consistency throughout this text we will use the first way, even though both are considered valid Java code.

This statement declares 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 indicates 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];

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 variable, 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 variable 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 determines the size of the array. Java compiler determines the size of the array by counting the number of items.