Intro to Java Web-Notes_Part61

Intro to Java Web-Notes_Part61 - Home - About Us Arrays of...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Home - About Us Arrays of the Built-In Data Types It is easy to declare and process a handful of variables: int X1, X2, X3; X1 : 5, X2 : 5, X3 — 5, However, it woud be a lot of work to declare and process a large number of variables . Arrays are a useful way of declaring many variables in one statement. For example, the following line of Java code declares five hundred int variables: int [1 X : new int[500]; For the purposes of this discussion we'll create a slightly smaller array -- the following line of code creates four integer elements: int [1 X : new int[4]; The four elements of the array can be accessed via the names X[0], X[l], X[2] and X[3]. The following four lines of code assign the value five to each of the four elements of the array: .\. \. WNl—‘O 01070707 \. \ NNNN Here is a simple visualization of these four integer variables: The value between the square braces, , is refered to as an index or an ofifvet. Any integer expression can be used to specify the index of an element that you wish to access. The following expressions all refer to the first element of the array, and assign the value five to that element (each of these expressions refer to the array element x[0]): x[ e x[ W Xl : —4] 5; 3+3) : QNO AMO 5; l 5; Variables can also be used in the expressions contained between the square braces. The following instructions also assign the value five to x[0]: int num : O; x[num] : 5; Usually, a variable is used to keep track of the current position in an array. This variable is refered to as the index variable. In the following example the expression arrfi] is used to set all five elements of the array arr to the value 30: int [: arr : new int[5]; int i = O; arr[i: = 30; i++; arr[i: : 30; i++; arr[i: = 30; i++; arr[i: = 30; i++; arr[i: : 30; Repeated statements, like those in the example above, are best handled with a loop. The loop shown below accomplishes the same task as the code shown above: int arr : new int[5]; int i = 0; while (i < 5) i arr[i] = 30; i++; } Each array has a member named length that contains the size of the array. The previous example had an array of five ints named arr. The length variable can be accessed via the expression arr. length, and its value is 5. That expression could be used to control the number of times the loop is performed: int arr : new int[5]; int i = 0; while (i < arr.length) l arr[i] : 30; i++; } The following code initializes an array of five elements to the value 33, then prints out the contents of the array: public class ArrOne { public static void main (String args) { int arr : new int[5]; ...
View Full Document

This note was uploaded on 11/17/2010 for the course CIS 1500 taught by Professor Staff during the Summer '08 term at Oakland CC.

Page1 / 2

Intro to Java Web-Notes_Part61 - Home - About Us Arrays of...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online