topic 2 part 4.1
TRANSCRIPT
-
7/30/2019 Topic 2 Part 4.1
1/19
ARRAY
Single Array
-
7/30/2019 Topic 2 Part 4.1
2/19
Course Learning Outcome :
1. Draw Diagram of PP and OOP
2. Identify Anatomy of Java Program
3. Identify Programming Style andDocumentation
4. Identify the Programming Errors
-
7/30/2019 Topic 2 Part 4.1
3/19
Preview Question
1. Array are not objects in Java. [TRUE/FALSE]
2. The default length of an array is 10. [TRUE/FALSE]
3. Arrays are declared using the [] symbols. [TRUE/FALSE]
4. You can declare a 3 dimensional array. [TRUE/FALSE]5. An array with size of 10 will have a maximum index of 9.
[TRUE/FALSE]
6. An array can contain data of different types. [TRUE/FALSE]
7. Arrays of objects cannot be declare in Java. [TRUE/FALSE]8. Arrays can be extending by assigning it a new value.
[TRUE/FALSE]
-
7/30/2019 Topic 2 Part 4.1
4/19
Objectives :
Write a program using single array :
Define an array
Declare and initialize an array
Pass array to methods
Return array to methods
Write program using single array
-
7/30/2019 Topic 2 Part 4.1
5/19
Array Fundamental
An Array is a collection of similar type ofvariables having a common name.
Values in array are stored in consecutive
memory locations.
Two types of Arrays
One-dimensional array
Two-dimensional array
-
7/30/2019 Topic 2 Part 4.1
6/19
One-deminsional Array
Will have a single row and can have any
number of columns.
Will have only one subscript. Subscriptrefers to the dimension of the array.
Example : int x[ ] =new int[3];
-
7/30/2019 Topic 2 Part 4.1
7/19
Representation Array
-
7/30/2019 Topic 2 Part 4.1
8/19
Declaration & Initialization
You need to declare an array before usingit in the program.
An array can be defined using differentdata types such as integer, double, float,char and so on.
However, all the values in an array mustbe of the same data type.
-
7/30/2019 Topic 2 Part 4.1
9/19
Array Declaration
Syntax
[ ] =
new [Size of the array]
Example: int marks[ ] = new int[5];
-
7/30/2019 Topic 2 Part 4.1
10/19
Array Initialization
Initialisation is the process of assigning values to the
array you have created.
Example
marks[0] = 95; marks[1] = 85;
marks[2] = 75;
marks[3] = 80;
marks[4] = 65;
-
7/30/2019 Topic 2 Part 4.1
11/19
Array Initialization
Syntax :
[Array index] = Value;
The first element will always have the array index as 0.
Array index refers to the location of the values in an array.
For example:
marks[0]=95;
marks[1]=85;
marks[2]=75;
-
7/30/2019 Topic 2 Part 4.1
12/19
Array Element
The representation of the array for the
example in the previous slide is as follows.
Here, 0, 1 and 2 are the index of the array, marks.
-
7/30/2019 Topic 2 Part 4.1
13/19
Array Initialize
You can also initialise the array at the time of
declaration as shown in the following
example:
int marks[] = {95,85,75};When initialising an array, the values are
enclosed within curly brackets { }.
-
7/30/2019 Topic 2 Part 4.1
14/19
Accessing Array Element
Using the array index you can access thearray elements.
To print the value of the second element inan array, the code will be:
System.out.println("The second
element:
+ marks[1]);
-
7/30/2019 Topic 2 Part 4.1
15/19
Entering Data into an Array
When more number of values are to be stored in anarray, a for loop can be used.
The sample code shows how to use a forloop in anarray.
for(int i=0;i
-
7/30/2019 Topic 2 Part 4.1
16/19
Reading Data from an Array
You can use a for loop with a single println
statement to print the values from an array.
for (int i=0;i
-
7/30/2019 Topic 2 Part 4.1
17/19
Activity 1
Identify the array index of each element.
int ary[ ] = {2, 4, 6, 8}
a. 0, 1, 2, 3
b. 1, 2, 3, 4
c. 2, 4, 6, 8
d. 0, 2, 4, 6
-
7/30/2019 Topic 2 Part 4.1
18/19
int scores[ ] = new int[25];
Identify the valid elements in the
following: Give reason for the same.
a. scores[0]
b. scores[1]
c. scores[-1]
d. scores[25]
-
7/30/2019 Topic 2 Part 4.1
19/19
PASS & RETURN ARRAY TO METHODMULTIDIMENSIONAL ARRAY
To be continue