Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

ITPF 01– Object Oriented Programming 1

The Java Array and


Intro to Java GUI
Melgine M. Bauat, MSIT
Instructor
The Java Array
Arrays are used to store different variables of the same data type in one
place. Therefore: The data sets “Melgine”, 37, “Female”, 1.25 cannot be
organized in an array.

To declare an array in Java, define a variable type with the square brackets.
For example: Java C++
//Example Declaration with Value;
String [] student_names = {“Melgine”, “Jaze”, “Jaeden”}; string student_names[] = {“Melgine”, “Jaze”, “Jaeden”};

int [] myNum = {5, 10, 15, 20}; int myNum[] = {5, 10, 15, 20};

//Example declaration with fixed size but no value


int [] myNum = new int [4]; int myNum[4];
The Java Array
Declaring and Creating Array
• datatype[] arrayRefVar;
Example:
double[] myList;

• datatype arrayRefVar[]; // This style is allowed, but not preferred


Example:
double myList[];
Declaring and Creating Array
arrayRefVar = new datatype[arraySize];

Example:
myList = new double[10];

myList[0] references the first element in the array.


myList[9] references the last element in the array.
Getting the length of an Array
Once an array is created, its size is fixed. It cannot be
changed. You can find its size using

arrayRefVar.length

For example: myList = new double[10];


myList.length returns 10

Usage:

for (int i= 0; i< myList.length; i++) {


System.out.println(myList[i]);
}
Java Array Default Values

When an array is created, its elements are assigned the


default value of

0 for the numeric primitive data types, (int, float, double)


'\u0000' for char types, and
false for boolean types.
Using Indexed Variable
We can set array element value using its index number:
double[] myList = new double[2];
myList[0] = 3.1416
myList[1] = 9.8

After an array is created, an indexed variable can be used in the same way
as a regular variable. For example, the following code adds the value in
myList[0] and myList[1] to sum.

double sum = myList[0] + myList[1];


One Statement Array Declaration
Using the shorthand notation, you have to declare, create, and initialize
the array all in one statement.
double[] myList = {1.9, 2.9};
Splitting it would cause a syntax error. For example, the following is
wrong:
double[] myList; Correct:
myList = {1.9, 2.9}; double[] myList;
myList[0] = 1.9;
myList[1] = 2.9;
Trace Program with Array
Declare array variable values, create an
array, and assign its reference to values

public class Test {


public static void main(String[] args) {
int[] values = new int[5];
for (int i = 1; i < 5; i++) {
values[i] = values[i] + values[i-1] + 1;
}
values[0] = values[1] + values[4];
} WHY?
}
Sample Java Program with Array
Sample Java Program with Array
Java Program with Array and Loop
Java Program with Array and Loop
Java Program with Array and Loop

1st Loop - Outer


Java Program with Array and Loop

1 2 3 4 5 6 7 8 9 10 1
2
3

1st Loop - Outer 4


5
6
7
0 0 1 8
9
10
Java Program with Array and Loop

1 2 3 4 5 6 7 8 9 10 2
2 4 6 8 10 12 14 16 18 20 4
6

2nd Loop - Outer 8


10

12
1 1 2 14

16
18
Java Program with Array and Loop

1 2 3 4 5 6 7 8 9 10 3
2 4 6 8 10 12 14 16 18 20 6
3 6 9 12 15 18 21 24 27 30 9

3rd Loop - Outer 12

15
18
2 2 3 21
24
27
30
Java Program with Array and Loop

1 2 3 4 5 6 7 8 9 10 4
2 4 6 8 10 12 14 16 18 20 8
3 6 9 12 15 18 21 24 27 30 12

4th Loop - Outer 4 8 12 16 20 24 28 32 36 40


16

20
3 3 4 24
28
32
36
Java Program with Array and Loop

1 2 3 4 5 6 7 8 9 10 5
2 4 6 8 10 12 14 16 18 20 10
3 6 9 12 15 18 21 24 27 30 15

5th Loop - Outer 4 8 12 16


5 10 15 20
20
25
24
30
28
35
32
40
36
45
40
50 20

25
4 4 5 30
35
40
45
Java Program with Array and Loop

1 2 3 4 5 6 7 8 9 10 6
2 4 6 8 10 12 14 16 18 20 12
3 6 9 12 15 18 21 24 27 30 18

6th Loop - Outer 4 8 12


5 10 15
16
20
20
25
24
30
28
35
32
40
36
45
40
50 24
6 12 18 24 30 36 42 48 54 60
30
5 5 6 36
42
48
54
Java Program with Array and Loop

1 2 3 4 5 6 7 8 9 10 7
2 4 6 8 10 12 14 16 18 20 14
3 6 9 12 15 18 21 24 27 30 21

7th Loop - Outer 4 8 12 16


5 10 15 20
20
25
24
30
28
35
32
40
36
45
40
50 28
6 12 18 24 30 36 42 48 54 60
35
6 6 7 7 14 21 28 35 42 49 56 63 70
42
49
56
63
Java Program with Array and Loop

1 2 3 4 5 6 7 8 9 10 8
2 4 6 8 10 12 14 16 18 20 16
3 6 9 12 15 18 21 24 27 30 24

8th Loop - Outer 4


5
8
10
12
15
16
20
20
25
24
30
28
35
32
40
36
45
40
50 32
6 12 18 24 30 36 42 48 54 60
40
7 7 8 7 14 21 28 35 42 49 56 63 70
48
8 16 24 32 40 48 56 64 72 80
56
64
72
Java Program with Array and Loop

1 2 3 4 5 6 7 8 9 10 9
2 4 6 8 10 12 14 16 18 20 18
3 6 9 12 15 18 21 24 27 30 27

9th Loop - Outer 4


5
8
10
12
15
16
20
20
25
24
30
28
35
32
40
36
45
40
50 36
6 12 18 24 30 36 42 48 54 60
45
8 8 9 7 14 21 28 35 42 49 56 63 70
54
8 16 24 32 40 48 56 64 72 80
63
9 18 27 36 45 54 63 72 81 90 72
81
Java Program with Array and Loop

1 2 3 4 5 6 7 8 9 10 10
2 4 6 8 10 12 14 16 18 20 20
3 6 9 12 15 18 21 24 27 30 30

10th Loop - Outer 4


5
8
10
12
15
16
20
20
25
24
30
28
35
32
40
36
45
40
50 40
6 12 18 24 30 36 42 48 54 60
50
9 9 10 7 14 21 28 35 42 49 56 63 70
60
8 16 24 32 40 48 56 64 72 80
70
9 18 27 36 45 54 63 72 81 90 80
10 20 30 40 50 60 70 80 90 100 90
Java Program with Array and Loop
Windowed Java
THANK YOU FOR LISTENING!

You might also like