0% found this document useful (0 votes)
46 views22 pages

Arrays: CC3 Week 1-3

The document discusses arrays including their definition, key concepts like dimensions and subscripts, and how to declare and initialize arrays in Java. Code examples are provided to demonstrate creating arrays and accessing elements.

Uploaded by

Rovell Asidera
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)
46 views22 pages

Arrays: CC3 Week 1-3

The document discusses arrays including their definition, key concepts like dimensions and subscripts, and how to declare and initialize arrays in Java. Code examples are provided to demonstrate creating arrays and accessing elements.

Uploaded by

Rovell Asidera
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/ 22

ARRAYS

CC3 Week 1-3


Learning Objectives
 Understand the concepts of arrays.

 Implement Array in java.


Array
-is a collection of Homogenous, Ordered and Finite set
of elements.
-An element is one variable or object in an array.
-Homogenous implies all elements must be of the
same type and have the same structure
-Ordered means that elements are organized in
sequence.
-Finite means that each array contains a fixed number
of elements.
 Array-name
- All elements in the array are referenced or accessed through the
array-name.
- Naming arrays follow the conventions for naming identifiers.
 Dimension
- Elements in an array can be grouped into sub-groups or categories.
The sub-groups are called dimensions.
- An array having 1 group is called a single-dimension or one-
dimensional array. An array having more than 1 group is called a
multi-dimensional array.
 Size
- The number of elements that an array can contain is called its size
- Providing values for all the elements in an array is called populating
the array.
 Subscript
- Subscripts define the relative position of an element in an
array. Subscripts always start with position 0.

 A parallel array is one with the same number of


elements as another, and for which the values in
corresponding elements are related.
 Arrays, like variables, are declared and initialized.
 To declare an array,

data-type [] <array-name>;
- data-type – specifies what type of data the array can hold.
Arrays do not support multiple data-types.
- array-name – specifies the array’s name using the naming
rules for identifiers.
- [] – empty pair of square brackets differentiates an array
declaration from a normal variable declaration.
Syntax

dataType[] VarName; // preferred way.

or

dataType VarName[]; // works but not preferred way.


Syntax

VarName = new dataType [arraySize];


Declare/Create Two-dimensional
Arrays
// Declare array age
dataType[][]age;

// Create array and assign its reference to


variable
age = new dataType[][];

// Combine declaration and creation in one


statement with a value of 10 rows and 5
columns
dataType[][] age = new dataType[10][5];

// Alternative syntax
dataType age[][] = new dataType[10][5];
Example

 Following statement declares an array variable, age,


creates an array of 5 elements of int type and assigns
its reference to age

 Double [] age = new Double[5];


Initial code!
Double [] age = new Double[5];

age[0] = 12;
age[1] = 56;
age[2] = 24;
age[3] = 75;
age[4] = 34;
The length field contains the number of
elements in an array.

for (int x = 0; x<age.length; x++) {


System.out.println (age[x]);
}
  
for (int x = 0; x<age.length; x++) {
System.out.println (age[x]);
}
=

for (int x = 0; x<5; x++) {


System.out.println (age[x]);
}
COMPLETE CODE
int [] age = new double[5];

age[0] = 12;
age[1] = 56;
age[2] = 24;
age[3] = 75;
age[4] = 34;

for (int x = 0; x<age.length; x++) {


System.out.println (age[x]);
}
Output
12
56
24
75
34
The code is also same as
int [] age = {12, 56, 24, 75, 34} ;

for (int x = 0; x<age.length; x++) {


System.out.println (age[x]);
}
 Exercise 10.1
Write the statements that will declare and initialize the
following arrays:
1. The first ten letters of the alphabet.
2. The ages of 5 students: 16, 20, 17, 18, 17
3. The salaries of employees. Note: Initialize array elements
to 0.
4. The Boolean values “true”, “false”, “true”, “false”.
5. The grades of 7 students. Assign specific grades to each
element.
 The first ten letters of the alphabet.
1. The ages of 5 students: 16, 20, 17,
18, 17
1. The salaries of 5 employees.
Note: Initialize array elements to
0.
1. The Boolean values “true”, “false”,
“true”, “false”.
1. The grades of 7 students. Assign
specific grades to each element.

You might also like