0% found this document useful (0 votes)
2 views23 pages

Lecture 5 - Array

An array is a collection of variables of the same type, allowing for the storage of multiple values without declaring separate variables. Arrays have ordered elements accessed via an index, and can be initialized using array literals. Multidimensional arrays, such as two-dimensional arrays, contain other arrays and require multiple indexes for element access.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views23 pages

Lecture 5 - Array

An array is a collection of variables of the same type, allowing for the storage of multiple values without declaring separate variables. Arrays have ordered elements accessed via an index, and can be initialized using array literals. Multidimensional arrays, such as two-dimensional arrays, contain other arrays and require multiple indexes for element access.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Array

Array
 An array is a collection of variable of the same
type.
 When you need to store a list of values, such
as numbers, you can store them in an array,
instead of declaring separate variables for
each number.
For example, to declare an array
of integers:
To define the array’s capacity, or the number
of elements it will hold, the keyword new
must be used:
 In an array, the elements are ordered
and each has a specific and constant
position, which is called an index.
 To reference elements in an array, type
the
name of the array followed by the index
position within a pair of square brackets
([ ]).
Example:
Here is an example of Array that define the array
capacity of the number of elements it will hold. It
also references elements in an array and print
specified elements through index.
Initializing Arrays
 You Java provides a shortcut for
instantiating arrays of primitives types and
strings.
 If you already know what values to insert
into the array, you can use an array literal.
Example of an array literal:
 Place the values in a comma-separated
list, enclosed in curly braces.
 The code above automatically initializes
an array containing four (4) elements,
and stores the provided values.
Adding Element Values in Arrays
Array Length
• You can access the length of an
array(the number of elements it stores)
via its length property
Example:
Arrays
• The for loop is the most used loop when working
with arrays, as we can use the length of the array to
determine how many times to run the loop.
Example:
Enhanced for Loop
The enhanced for loop (sometimes called
as “for each” loop) is used to traverse
elements in arrays.
The advantages are that it eliminates the
possibility of bugs and makes the code
easier to read.
Example:
Example Output:
The enhanced for loop declares a variable of a
type compatible with the elements of the array
being accessed. The variable will be available
within the for block, and its value will be the
same as the current array element.
So, on each iteration of the loop, the variable t
will be equal to the corresponding element in
the array.
Multidimensional Arrays
 These are arrays that contain other arrays.
These are arrays that contain other arrays.
 The two-dimensional array, most known as
a data table, is the most basic
multidimensional array.
 To create multidimensional arrays, place
each array within its own set of square
brackets.
Example of a two-dimensional
array:
 This declares an array with two (2)
arrays as
its elements.

 To access an element in the two-


dimensional
array, provide two (2) indexes, one (1)
for the
array, and another for the element
inside that
array.
The following example accesses the
first element in the second array of
sample:
END

You might also like