0% found this document useful (0 votes)
33 views34 pages

Java Programming - Array

The document discusses arrays and matrices in Java programming. It begins by defining an array as a collection of similar data types stored in contiguous memory locations and accessed using an index. It then covers how to declare, define the size, and initialize arrays and matrices in Java. The document also provides examples of using for and for-each loops to iterate through arrays to output or calculate values of the elements.

Uploaded by

Thanh Nhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views34 pages

Java Programming - Array

The document discusses arrays and matrices in Java programming. It begins by defining an array as a collection of similar data types stored in contiguous memory locations and accessed using an index. It then covers how to declare, define the size, and initialize arrays and matrices in Java. The document also provides examples of using for and for-each loops to iterate through arrays to output or calculate values of the elements.

Uploaded by

Thanh Nhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Array & Matrix

Nguyễn Đức Huy – [email protected]


Agenda
1. Array
2. Matrix
3. Sawtooth Array
4. References

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 2
1
Arrray
Image in memory

index 0 1 2 3 4

5 7 2 4 9

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 4
Define array
DataType[ ] variableName;
Or
DataType variableName [ ];

1 // Define int array


2 int[] arr1;
3 // Define long array
4 long[] arr2;
5 // Define float array
6 float[] arr3;
7 // Define double array
8 double[] arr4;
9 // Define boolean array
10 boolean[] arr5;
11 // Define string array
12 String[] arr6

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 5
Memory allocation
Option 1 DataType[] variableName = new DataType [n] ;
Option 2 DataType[] variableName;
variableName = new DataType[n];
1 // Define and allocation for int array
2 int[] arr1 = new int[5];// a.length = 5
3 // Define and allocation for long array
4 long[] arr2 = new long[5];// a.length = 5
5 // Define and allocation for float array
6 float[] arr3 = new float[7];//a.length = 7
7 // Define and allocation for double array
8 double[] arr4 = new double[7];//a.length = 7
9 // Define and allocation for boolean array
10 boolean[] arr5 = new boolean[8];//a.length = 8
11 // Define and allocation for string array
12 String[] arr6 = new String[6];//a.length = 6
13

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 6
Initialize
1 // Option 1
2 int[] arr = {1, 3, 5, 7, 9}; //a.length = 5
3 // Option 2
4 int [] arr = new int[5];
5 arr[0]=1;
6 arr[1]=3;
7 arr[2]=5;
8 arr[3]=7;
9 arr[4]=9;

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 7
Init
1 // Init long array
2 long[] arr1 = {1, 3, 5, 7, 9}; //a.length = 5
3 // Init float array
4 float[] arr2 = {1.3, 3.2, 5.5}; //a.length = 3
5 // Init double array
6 double[] arr2 = {2.3, 7.2, 9.5};//a.length= 3
7 // Init string array
8 String[] ngay = {
9 "Sunday","Monthday","Tuesday",
10 "Wednesday","Thurday","Friday","Staturday"
11 };//a.length= 7

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 8
foreach - statement
// variableName is item in array
for (DataType variableName : arrayName){
Block code;
}

1 System.out.println("Number of items in array " + a.length);


2 for (int item : a){
3 System.out.println(item);
4 }

1 System.out.println("Number of items in array " + a.length);


2 for (int i=0 ; i<a.length; i++){
3 System.out.println(a[i]);
4 }

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 9
Input/output one-dimensional array
1 // Input aray
2 System.out.print("Enter number of items: ");
3 int n = Integer.parseInt(scan.nextLine());
4 int [] a = new int [n]; //a.Length = n
5 for (int i = 0; i < a.length; i++) {
6 System.out.print("a["+i+"]=");
7 a[i] = Integer.parseInt(scan.nextLine());
8 }
9 // output use for
10 System.out.println("Output array with for");
11 System.out.println("Number of items is " + a.length);
12 for (int i = 0; i < a.length; i++)
13 {
14 System.out.println(a[i]);
15 }
16 // output use foreach
17 System.out.println("Output array with foreach");
18 System.out.println("Number of items is " + a.length);
19 for (int pt : a)
20 {
21 System.out.println(pt);
22 }

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 10
Sum all items in array
1 // use for
2 int s = 0;
3 for (int i = 0; i < a.length; i++)
4 {
5 s = s + a[i];
6 }
7 System.out.println("s = {0}", s);
8
9 // use foreach
10 s = 0;
11 for (int pt : a)
12 {
13 s = s + pt;
14 }
15 System.out.println("s = " + s);

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 11
Sort increase (Selection Sort)
1 int i, j;
2 int min, temp;
3 for (i = 0; i < a.length - 1; i++)
4 {
5 min = i;
6 for (j = i + 1; j < a.length; j++)
7 {
8 if (a[j] < a[min])
9 {
10 min = j;
11 }
12 }
13 temp = a[i];
14 a[i] = a[min];
15 a[min] = temp;
16 }

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 12
Exercises
§ Developing a program with:
§ Input one-dimensional array
§ Sum of items
§ Find MAX item
§ Sort increase
§ Print array to console

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 13
2
Matrix
Image in memory Column index

0 1 2 3 4

0 5 7 2 4 9

Row index 1 3 2 0 1 4

2 6 1 9 2 3

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 15
Define matrix
DataType [][] variableName;
Or
DataType variableName [][];
1 // Define int matrix
2 int[][] arr1;
3 // Define long matrix
4 long[][] arr2;
5 // Define float matrix
6 float[][] arr3;
7 // Define double matrix
8 double[][] arr4;
9 // Define boolean matrix
10 boolean[][] arr5;
11 // Define String matrix
12 String[][] arr6;

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 16
Memory allocation
Option 1 DataType[][] matrixName = new DataType [n][m] ;
Option 2 DataType[][] matrixName;
matrixName = new DataType [n][m];
n Number of rows : matrixName.length
m Number of columns : matrixName [i].length
1 // Define and allocation int matrix
2 int[][] arr1 = new int[3][5];
3 int numRow = a.length; //numRow = 3
4 int numColumn = a[i].length; //numColumn = 5
5 // Define and allocation long matrix
6 long[][] arr2 = new long[5][6];
7 int numRow = a.length; //numRow = 5
8 int numColumn = a[i].length;; //numColumn = 6
9 // Define and allocation float matrix
10 float[][] arr3 = new float[7][9];
11 int numRow = a.length; //numRow = 7
12 int numColumn = a[i].length; //numColumn = 9

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 17
Initialize 1
2
3
// Option 1
int[][] a = {
{ 1, 2 },
4 { 3, 4 },
5 { 5, 6 },
6 { 7, 8 }
7 };
8 int numRow = a.length; //numRow = 4
9 int numColumn = a[i].length; //numColumn = 2
10
11 // Option 2
12 int [][] a = new int[4][2];
13 int numRow = a.length; //numRow = 4
14 int numColumn = a[0].length; //numColumn = 2
15 int k = 1;
16 for (int i = 0; i < numRow; i++)
17 {
18 for (int j = 0; j < numColumn; j++)
19 {
20 a[i][j]=k++;
21 }
22 }

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 18
Input matrix
1 // Input matrix
2 int[][] a;
3 System.out.print("Enter number of rows: ");
4 int n = Integer.parseInt(scan.nextLine());
5 System.out.print("Enter number of column: ");
6 int m = Integer.parseInt(scan.nextLine());
7 a=new int[n][m];
8 for (int i = 0; i < a.length; i++){
9 for (int j = 0; j < a[i].length; j++){
10 System.out.print("a[" + i + "][" + j + "] = ");
11 a[i][j] = Integer.parseInt(scan.nextLine());
12 }
13 }
14

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 19
Output matrix
1 // Output matrix
2 System.out.println("Number of rows :" + a.length);
3 System.out.println("Number of columns : {0}", a[0].length);
4 for (int i = 0; i < a.length; i++){
5 for (int j = 0; j < a[i].length; j++) {
6 System.out.print(a[i][j]+"\t");
7 }
8 System.out.println();
9 }
10

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 20
Sum all items in matrix
1 int s = 0;
2 for (int i = 0; i < a.length; i++){
3 for (int j = 0; j < a[i].length; j++){
4 s = s + a[i][j];
5 }
6 }
7 System.out.println("s=" + s);

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 21
Exercises
§ Developing a program with:
§ Input matrix
§ Sum of items in matrix
§ Find MAX items
§ Sort increase by Columns
§ Sort increase by Rows
§ Print matrix

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 22
3
Sawtooth Array
Image in memory

0 1 2 3 4

0 5 7 2 4 Number of columns = 4

Row index 1 3 2 0 Number of columns = 3

Number of rows = 3 2 6 1 9 2 3 Number of columns = 5

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 24
Define
DataType [][] variableName;
Or
DataType variableName [][];
1 // Define int two-dimensional array
2 int[][] arr1;
3 // Define long two-dimensional array
4 long[][] arr2;
5 // Define float two-dimensional array
6 float[][] arr3;
7 // Define double two-dimensional array
8 double[][] arr4;
9 // Define boolean two-dimensional array
10 boolean[][] arr5;
11 // Define String two-dimensional array
12 String[][] arr6;

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 25
Memory allocation
Option 1 DataType[][] arrayName = new DataType [n][] ;
Option 2 Datatype[][] arrayName;
arrayName = new DataType [n][];
n Number of rows : arrayName.length

1 // Define and allocate int sawtooth array


2 int[][] arr1 = new int[3][];
3 int numRow = arr1.length; //numRow = 3
4 // Define and allocate long sawtooth array
5 long[][] arr2 = new long[5][];
6 int numRow = arr2.length; //numRow = 5
7 // Define and allocate float sawtooth array
8 float[][] arr3 = new float[7][];
9 int numRow = arr3.length; //numRow = 7

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 26
Initialize
1 int[][] a = {
2 { 1, 2 },
3 { 3, 4, 5 },
4 { 5, 6 , 7, 8 },
5 { 7, 8 ,9 ,10 }
6 };
7 int numRow = a.length; //numRow = 4
8 int numColumn = a[i].length; //numColumn in row i

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 27
Input sawtooth array
1 // Input sawtooth array
2 int[][] a;
3 System.out.print("Enter number of row: ");
4 int n = Integer.parseInt(scan.nextLine());
5 a=new int[n][];
6 for (int i = 0; i < a.length; i++){
7 System.out.print("Enter number of clumn at row " + i);
8 int m = Integer.parseInt(scan.nextLine());
9 a[i]=new int[m];
10 for (int j = 0; j < a[i].length; j++) {
11 System.out.print("a[" + i + "][" + j + "] = ");
12 a[i][j] = Integer.parseInt(scan.nextLine());
13 }
14 }

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 28
Output sawtooth array
1 // Output sawtooth array
2 System.out.println("Number of row is " + a.length);
3 for (int i = 0; i < a.length; i++){
4 System.out.println("Number of column at row " + i + " is " + a[i].length);
5 for (int j = 0; j < a[i].length; j++){
6 System.out.print(a[i][j] + "\t");
7 }
8 System.out.println();
9 }
10

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 29
Sum all items
1 int s = 0;
2 for (int i = 0; i < a.length; i++){
3 for (int j = 0; j < a[i].length; j++){
4 s = s + a[i][j];
5 }
6 }
7 System.out.println("s = " + s);

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 30
Exercises
§ Developing a program with:
§ Input sawtooth array
§ Sum of items in sawtooth array
§ Find row which have MAX sum of items
§ Print sawtooth array

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 31
4
References
References
§ Books
§ Nguyễn Hoàng Anh, slide and video for Java programming course,
FIT@HCMUS, 2010
§ The Java Language Specification Third Edition (2005)
§ Online references
§ Java documentation: https://docs.oracle.com/en/java/

| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 33
| Software Engineering Dept. – FIT@HCMUS Nguyễn Đức Huy - Java Programming 10/21/21 34

You might also like