Difference between revisions of "Creating Arrays"

From CompSciWiki
Jump to: navigation, search
(Formatted 2 lines of code near the end to match the rest of the page (and rest of the document))
Line 1: Line 1:
{{1010Topic|Introduction=Arrays are an essential tool for all programmers. They allow you to group related pieces of information together allowing you to make difficult calculations with greater ease.
+
{{1010Topic
 +
|Body=
 +
|Chapter_TOC=[[Arrays]]
 +
|Introduction=Arrays are an essential tool for all programmers. They allow you to group related pieces of information together allowing you to make difficult calculations with greater ease.
  
 
This section covers precisely that... Creating and Editing Arrays.|Overview=Once you have completed this section you will be able to:
 
This section covers precisely that... Creating and Editing Arrays.|Overview=Once you have completed this section you will be able to:
 
*Create arrays
 
*Create arrays
 
*Edit Arrays
 
*Edit Arrays
|Chapter_TOC=[[Arrays]]}}
+
 
 +
}}
  
 
=='''Creating an Array'''==
 
=='''Creating an Array'''==

Revision as of 11:52, 1 December 2011

COMP 1010 Home > Arrays


Introduction

Arrays are an essential tool for all programmers. They allow you to group related pieces of information together allowing you to make difficult calculations with greater ease.

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

   

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 primitive 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.