Difference between revisions of "Creating Arrays"

From CompSciWiki
Jump to: navigation, search
m (Removed Overview tag.)
m (Intro clean up)
 
(13 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{1010Topic
+
{{Template:1010Topic
 +
|Chapter_TOC=[[Arrays]]
 +
|Previous=[[User-Defined Methods]]
 +
|Next=[[Entering and Using Array Elements]]
 
|Body=
 
|Body=
|Chapter_TOC=[[Arrays]]
+
==Introduction==
|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.
+
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.
  
This section covers precisely that... Creating and Editing Arrays.
+
In this section, we will discuss how to create arrays.
*Create arrays
+
*Edit 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 [[Common Primitive Variables|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:
+
==Creating an Array==
 
+
An array can store a group of values of the same [[Common Primitive Variables|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:
 
+
{{CodeBlock
<pre>
+
|Code=
int[] numbers;
+
int[] numbers; // For consistency, we will be using this format throughout the Wiki.
    or
+
}}
 +
or
 +
{{CodeBlock
 +
|Code=
 
int numbers[];
 
int numbers[];
</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.
+
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:
 
+
{{CodeBlock
 
+
|Code=
<pre>
+
 
numbers = new int[10];
 
numbers = new int[10];
</pre>
+
}}
  
  
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:
+
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.
 
+
 
+
 
+
<pre>
+
int[] numbers = new int[10];
+
</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.
+
  
 
[[Image:Array.JPG]]
 
[[Image: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:
+
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:
 +
{{CodeBlock
 +
|Code=
 +
char[] characters = new char[10]; // An array of chars
  
 +
long[] numbers = new long[50]; // An array of long ints
  
<pre>
+
double[] size = new double[100]; // An array of doubles
 +
}}
  
char[] characters = new char[10];
 
  
long[] numbers = new long[50];
+
The following example shows another way to declare arrays, where we use two reference variables of the Integer type:
 
+
{{CodeBlock
double[] size = new double[100];
+
|Code=
 
+
</pre>
+
 
+
 
+
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.
+
 
+
 
+
<pre>
+
 
int[] numbers,size;
 
int[] numbers,size;
        or
+
}}
 +
or
 +
{{CodeBlock
 +
|Code=
 
int numbers[],size[];
 
int numbers[],size[];
 +
}}
  
</pre>
 
  
 +
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:
 +
{{CodeBlock
 +
|Code=
 +
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.
  
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:
 
  
 +
Also, you can declare and initialize an array at the same time. Here is an example:
 +
{{CodeBlock
 +
|Code=
 +
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.
  
<pre>
 
int ARRAY_SIZE=6;
 
int[] numbers = new int [ARRAY_SIZE];
 
</pre>
 
Note: if you change the '''size''' variable, you will not change the size of the array.
 
  
 +
==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).
  
You can also create and initialize an array at the same time. Here is an example:
+
In the next section, we will be discussing about [[Entering & Using Array Elements]].
  
  
<pre>
+
}}
int[] numbers={1,2,3}  
+
</pre>
+
 
+
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.
+

Latest revision as of 02:36, 8 December 2011

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