- Information
- AI Chat
Was this document helpful?
An Overview of Arrays
Course: Computer science
702 Documents
Students shared 702 documents in this course
University: University of Southern Mindanao
Was this document helpful?
An Overview of Arrays
How to declare various variables using various data types was covered in
earlier chapters. A distinct identifier name and data type should be used when
declaring variables. The variable's identifier name is called in order to use it.
For instance, the following three (3) int variables each have a unique name:
int num1 = 0;
int num2 = 0;
int num3 = 0;
num1 = 1;
num2 = 3;
num3 = 5;
The initialization, assignment, and use of the variables appear to be
laborious tasks. One feature of Java and other programming languages allows for
the efficient storage and manipulation of a list of data in a single variable. An
array is the name for this kind of variable.
A continuous block of memory that has been partitioned into a number of
slots is used by an array to store many instances of the same type of data. Consider
an array as a stretched variable—a location with only one name for identification but
the capacity to store multiple values. Instead of defining three (3) variables that can
contain one (1) value each, as in the example above, you can define an int array
variable named num that can hold three (3) values: 1, 3, and 5 (Fig. 51).
num: 0 1 2
1 3 5
Fig. 51. Visual Representation of an Array Variable
DECLARING ARRAYS
Like other variables, arrays must be declared. List the data type, a series of
square brackets [], the name of the array's identifier, and then the data type. For
instance,
int []num;
The square brackets can also be placed after the identifier name. For example,
int num[];
The array must be constructed after declaring it, and a constructor
statement must provide its length. In Java, this procedure is known as instantiation
(the Java word for create). Keep in mind that after the array is initialized, the size
cannot be modified. For instance,