Professional Documents
Culture Documents
JAVA ARRAYS
*Property of STI
int number1;
Arrays
Declaring Java Arrays
number2 = 2;
number3 = 3;
Coding Guidelines
Array Length
Multidimensional Array
J0007
int number2;
int number 3;
Arrays
*Property of STI
1 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
0
number: 1
1
2
2
3
J0007
2 __________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
To declare an array, write the data type, followed by a set
of square brackets [], followed by the identifier name.
Arrays
number1 = 1;
ElementType[] arrayName;
Declaring
Java Arrays
Example:
int[] x;
int[] ages;
Or equivalently
int x[];
int ages[];
*Property of STI
J0007
3 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
*Property of STI
J0007
4 __________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
Initializing
Array
Variables
:
x
Initializing
Array
Variables
:
x[0]
x[1]
x[2]
x[3]
:
*Property of STI
J0007
5 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
*Property of STI
J0007
6 __________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
Example:
Initializing
Array
Variables
or
Initializing
Array
Variables
Example:
int[] x = new int[10];
int[] ages = new int[100];
*Property of STI
J0007
7 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
*Property of STI
J0007
8 __________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
Examples
J0007
9 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
*Property of STI
10 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
To access an array element, or a part of
the array, use a number called an index or
a subscript.
Example:
Accessing an
Array
Element
*Property of STI
J0007
J0007
11 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
*Property of STI
J0007
12 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
Accessing an
Array
Element
*Property of STI
J0007
13 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
Coding
Guidelines
J0007
14 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
/** Array manipulation */
public class upavon
{
public static void main(String[] args) {
int [] sizes = {31,28,31,30,31,30,31,31,30,31,30,31};
System.out.print("number ... ");
int month = (int) wellreader.read_number();
Array Length
Example of
Array
Manipulation
*Property of STI
J0007
15 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
*Property of STI
J0007
16 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
Example:
//integer array 512 x 128 elements
int[][] twoD = new int[512][128];
//character array 8 x 16
Multidimensional
Arrays
{toby, gray},
{fido, black}
};
*Property of STI
J0007
17 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
*Property of STI
J0007
18 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
Example:
Multidimensional
To access the first element in the first row of the
Arrays
array dogs, we write,
Example
System.out.print(dogs[0][0]);
}
}
J0007
19 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
*Property of STI
J0007
20 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________