Professional Documents
Culture Documents
Array With Looping
Array With Looping
1
What is an array???
• An Array is A “Data Structure”
• An Array consists of data items of the same type, all with the same
name.
• Once you have created an “array”, you cannot change the number of
elements it holds.
2
Creating Arrays
• An array is an object so it needs an object reference.
int[] numbers;
int[] numbers;
– However, the brackets can also go here:
int numbers[];
– These are equivalent but the first style is typical.
7-4
Creating Arrays
• It is possible to declare an array
reference and create it in the same
statement.
6
ARRAY
7
An Array is A “Data Structure”
x[0] 28
x[1] 44
x[2] 98
“28” is the first
element, with a x[3] 1
subscript of “0” x[4] 28
x[5] 5
x[6] 2
x[7] 29
x[8] 73
8
An Array is A “Data Structure”
• To learn the number of elements in the
array, you call a property called “length.”
For example:
int y = 0;
int[] x;
x = new int[5];
y = x.length;
• Notice, “length” is NOT a method, it is
a property. 9
Inputting and Outputting
Array Elements
• Array elements can be treated as any other variable.
• They are simply accessed by the same name and a subscript.
7-10 }
String Arrays
• If an initialization list is not provided, the new
keyword must be used to create the array:
String[] names = new String[4];
Address
names[0] null
names[1] null
names[2] null
names[3] null
7-11
Do while loop
12
While loop
13