Computer Programming
Course Code#6803103-3
T.Aisha Alasmari
Computer Programming
Lecture#7:Arrays (one-dimensional)
T.Aisha Alasmari
Topics
Introduction.
Data Types.
Arrays.
Logical Array Representation.
Declaring and Creating Arrays.
Array Initializer.
3
Introduction
Data structures are collections of related data items.
Array objects are data structures consisting of related data items of the same type.
Arrays make it convenient to process related groups of values.
Arrays remain the same length once they’re created.
4
Data Types
Java’s types are divided into:
1. Primitive types.
Int, byte, short, long, float, double, char, boolean.
2. Reference types.
All nonprimitive types are reference types, so classes, which specify the types of objects, are reference types.
For example, String, Scanner and SecureRandom.
5
Data Types
A primitive-type variable can hold exactly one value of its declared type at a time.
For example, an int variable can store one integer at a time.
When another value is assigned to that variable, the new value replaces the previous one—which is lost.
Programs use variables of reference types (normally called references) to store the addresses of objects in the computer’s memory.
Such a variable is said to refer to an object in the program.
6
Data Types
For example:
Creates a scanner object, then assigns to the variable Input a reference to that Scanner object.
To call an object’s methods, you need a reference to the object. For example,
Uses the variable input, which refers to a Scanner object, to call the Scanner object’s method nextInt().
7
Data Types
If an object’s method requires additional data to perform its task, then you’d pass arguments in the method call.
For example:
Uses the variable randomNumbers, which refers to a SecureRandom object, to call a the object’s nextInt() method and pass it the argument 6.
Note that primitive type variables do not refer to objects, so such variables cannot be used to invoke methods.
8
Arrays
An array is a group of variables (called elements or components) containing values that all have the same type.
Arrays are objects, so they’re considered reference types.
The elements of an array can be either primitive types or reference types.
To refer to a particular element in an array, we specify the name of the
reference to the array and the position number of the element in the array.
The position number of the element is called the element’s index or subscript.
Array objects occupy space in memory. Like other objects,
Also, arrays can have one or several dimensions.
9
Logical Array Representation
10
For Example:
A logical representation of an integer array called c.
This array contains12 elements.
Logical Array Representation
11
A program refers to any one of these elements with an array-access
expression that includes the name of the array followed by the index of the particular element in square brackets ([ ]).
The first element in every array has index zero and is sometimes called the zeroth element. Thus, the elements of array c are c[0], c[1], c[2] and so on.
The value of c[0] is -45, the value of c[1] is 6, the value of c[2] is 0 and so on.
The highest index in array c is 11.
Array names follow the same conventions as other variable names.
Logical Array Representation
12
An index must be a nonnegative integer.
A program can use an expression as an index.
For example, if we assume that variable a is 5 and variable b is 6, adds 2 to array element c[11].
An indexed array name is an array-access expression.
Declaring Arrays
13
General Format of a Declare Array Statement by 3 ways:
Declaring Arrays
Example:
One dimensions.
Two dimensions.
14
Creating Arrays
15
General Format of a Create Array Statement:
Example:
Assigning Arrays Values
16
General Format of a Assigning Arrays Values Statement:
Example:
Declaring and Creating Arrays
Arrays are created with keyword new.
You can declaring and creating arrays in a several statement.
Example:
You can declaring and creating arryas in single statement.
General Format of Statement:
Example:
17
Declaring and Creating Arrays
Program can create several arrays in a single declaration.
The following declaration reserves 100 elements for b and 27 elements for x:
Every element of an int array is an int value, and every element of a String array is a reference to a String object.
18
Declaring and Creating Arrays
When an array is created, each of its elements receives a default value:
Zero for the numeric primitive-type elements,.
False for boolean elements.
Null for references.
you can provide nondefault element (assigning values ) by 2 ways:
1. When you create an array, assigning values later.
2. When you create an array, assigning values directly (called Array Initializer).
19
Array Initializer
You can create an array and initialize its elements with an array initializer—a comma-separated list of expressions (called an initializer list) enclosed in braces { }.
In this case, the array length is determined by the number of elements in the initializer list.
General Format of Statement:
Example:
Creates a five-element array with index values 0 - 4.
Element n[0] is initialized to 10, n[1] is initialized to 20, and so on.
20
Examples(1)
21
Example:
Output:
Examples(2)
22
Example:
Output:
Examples(3)
23
Example:
Output:
Thank you
Any Question?