Prev | Current Page 183 | Next

Carl Reynolds and Paul Tymann

"Schaum's Outline of Principles of Computer Science"

The result will be:
name: Carl
ARRAYS
A data structure is a way of organizing data, and arrays are among the simplest of data structures. An array
is a data structure that holds multiple values of the same type. One speaks of an array of Strings, or an array
of ints.
One declares an array by using square brackets, either after the type declaration or after the name:
int[] x; // either form declares an array of ints
int y[];
Declaring an array simply gives it a name. To create the elements of an array, one must also use the new
key word, along with an integer within square brackets to indicate the number of elements in the array:
x = new int[15]; // 15 int elements, each set to 0
y = new int[10]; // 10 int elements, each set to 0
Once the array is created, its size cannot be changed.
Individual elements in an array receive default values of zero for numeric types, nulls for characters, and
nulls for Strings and other objects.
By using a subscript, one can assign values to elements of an array, or read the value of an element. In Java,
arrays are zero-based, which means that the first element of the array is referred to with a subscript of 0.
x[4] = 66; // assign the value 66 to the 5th element
c = y[1];// read the value of the 2nd element into c
Each Java program??™s main method declares an array of Strings, by convention called args, for
accepting any arguments that the user might supply from the command line.


Pages:
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
Paula Atherton hotele DMOZ biznes prezenty Athlete