Java-Arrays Compress
Java-Arrays Compress
Java Arrays
1. Array Declaration
2. Accessing an Array
3. Array Length
4. Multidimensional Array
In this lesson, Java arrays will be discuss. Arrays are defined first, followed by a discussion of how to
declare and utilize arrays.
V. LESSON CONTENT
There is a feature in Java and other programming languages that allows us to store a list of data in a
single variable and operate it more effectively. An array is the name for this sort of variable. An array is
a continuous block of memory split into a number of slots that holds numerous data objects of the same
datatype. Consider an array to be a stretched variable — a place with a single identifier name that may
store many values.
0 1 2
number: 1 2 3
Declaring Arrays
Arrays must be declared like all variables. When declaring an array, list the data type, followed by a set
of square bracket [], followed by the identifier name. For example,
int [ ] ages;
0 0
0 0
After declaring, create the array and specify its length with a constructor statement. In Java, this is
known as instantiation (the Java word for creates). It's important to remember that once an array is
created, it can't be changed.
The declaration tells the Java Compiler that the identifier ages will be used as the name of an array
containing integers, and to create or instantiate a new array containing 100 elements.
Instead of using the new keyword to instantiate an array, you can also automatically declare, construct
and assign values at once.
Example:
To access an array element, or a part of the array, use a number called index or a subscript. Each
member of the array is given an index number or subscript, allowing the program and the programmer
to retrieve individual values as needed. Integers are always used as index numbers. They start at zero
and work their way to the end of the array by adding whole integers one by one. Keep in mind that the
elements in your array range from 0 to (sizeOfArray-1).
Example:
0 0
0 0
Array Length
The length field of an array may be used to determine the number of elements in an array. The size of
an array is returned via the length field. It may be utilized in a variety of ways,
Example:
Output:
Multidimensional Array
0 0