SlideShare a Scribd company logo
Object Oriented
Programming
Andi Nurkholis, S.Kom., M.Kom.
Study Program of Informatics
Faculty of Engineering and Computer Science
SY. 2019-2020
March 19, 2020
5.1 Array
2
3
Array
Arrays are used to store multiple values in a
single variable, instead of declaring separate
variables for each value.
Java Array
4
In Java, to declare an array, define the variable type with square brackets:
String[] cars;
To insert values to it, we can use an array literal - place the values in a
comma-separated list, inside curly braces:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
Access the Elements of an Array
You access an array element by referring to the index number.
This statement accesses the value of the first element in cars:
5
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars[0]);
// Outputs Volvo
Change an Array Element
6
To change the value of a specific element, refer to the index number:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
System.out.println(cars[0]);
// Now outputs Opel instead of Volvo
7
Array Length
To find out how many elements an array has, use the length property:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars.length);
// Outputs 4
Loop Through an Array
8
You can loop through the array elements with the for loop, and use the
length property to specify how many times the loop should run.
The following example outputs all elements in the cars array:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < cars.length; i++) {
System.out.println(cars[i]);
}
// Outputs Volvo, BMW, Ford, Mazda
Multidimensional Arrays
A multidimensional array is an array
containing one or more arrays.
To create a two-dimensional array, add each
array within its own set of curly braces:
9
Access the Element of an Array
10
To access the elements of the myNumbers array, specify two indexes:
one for the array, and one for the element inside that array.
This example accesses the third element (2) in the second array (1) of
myNumbers:
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
int x = myNumbers[1][2];
System.out.println(x); // Outputs 7
11
Loop Through an Array
public class MyClass {
public static void main(String[] args) {
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
for (int i = 0; i < myNumbers.length; ++i) {
for(int j = 0; j < myNumbers[i].length; ++j) {
System.out.println(myNumbers[i][j]);
}
}
}
}
Array in OOP
12
Create an Array
13
You can create an array by using the new operator with the following
syntax:
arrayRefVar = new dataType[arraySize];
The above statement does two things:
• It creates an array using new dataType[arraySize].
• It assigns the reference of the newly created array to the variable
arrayRefVar.
dataType arrayRefVar[];
Multidimensional Arrays
double[] myList = new double[10];
14
The Foreach Loops
15
JDK 1.5 introduced a new for loop known as foreach loop or enhanced
for loop, which enables you to traverse the complete array sequentially
without using an index variable.
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
int x = myNumbers[1][2];
System.out.println(x); // Outputs 7
16
Example
public class TestArray {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements
for (double element: myList) {
System.out.println(element);
}
}
}
Passing Arrays to Methods
17
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
//printArray(new int[]{3, 1, 2, 6, 4, 2});
Just as you can pass primitive type values to methods, you can also pass
arrays to methods. For example, the following method displays the
elements in an int array:
Thank You, Next …
Relation between Classes
Study Program of Informatics
Faculty of Engineering and Computer Science
SY. 2019-2020
Andi Nurkholis, S.Kom., M.Kom.
March 19, 2020

More Related Content

PDF
Array and Collections in c#
PPTX
PPTX
C++ lecture 04
PPTX
Array lecture
PPTX
Array in c#
PPTX
Array in C
PPTX
Array in c
Array and Collections in c#
C++ lecture 04
Array lecture
Array in c#
Array in C
Array in c

What's hot (20)

PDF
PPTX
7array in c#
PPSX
C Programming : Arrays
PPT
Java: Introduction to Arrays
PPT
Arrays Class presentation
PPTX
Array in C# 3.5
PPTX
Arrays in java language
PPTX
Arrays in C language
PPT
Two dimensional array
PPTX
Computer programming 2 Lesson 13
PDF
Arrays In C
PPTX
Array BPK 2
PPTX
Array in c++
PPTX
C++ programming (Array)
PPT
Array in Java
PPTX
Arrays in c
PPTX
Array Introduction One-dimensional array Multidimensional array
PDF
intorduction to Arrays in java
PPTX
One dimensional arrays
7array in c#
C Programming : Arrays
Java: Introduction to Arrays
Arrays Class presentation
Array in C# 3.5
Arrays in java language
Arrays in C language
Two dimensional array
Computer programming 2 Lesson 13
Arrays In C
Array BPK 2
Array in c++
C++ programming (Array)
Array in Java
Arrays in c
Array Introduction One-dimensional array Multidimensional array
intorduction to Arrays in java
One dimensional arrays
Ad

Similar to Object Oriented Programming - 5.1. Array (20)

PPTX
Arrays in programming
PPT
17-Arrays en java presentación documento
PPTX
Lec 1.5 Object Oriented Programming
PPTX
Arrays in java.pptx
PDF
Java Arrays
PPT
ch06.ppt
PPT
PPT
ch06.ppt
PPT
array Details
PDF
Java arrays (1)
PPTX
ch 7 single dimension array in oop .pptx
PPTX
Chap1 array
PPT
Data Structure Midterm Lesson Arrays
PDF
Arrays in Java
PDF
javaarray
PPTX
OOPs with java
PDF
Java chapter 6 - Arrays -syntax and use
PDF
Class notes(week 4) on arrays and strings
PPT
Eo gaddis java_chapter_07_5e
Arrays in programming
17-Arrays en java presentación documento
Lec 1.5 Object Oriented Programming
Arrays in java.pptx
Java Arrays
ch06.ppt
ch06.ppt
array Details
Java arrays (1)
ch 7 single dimension array in oop .pptx
Chap1 array
Data Structure Midterm Lesson Arrays
Arrays in Java
javaarray
OOPs with java
Java chapter 6 - Arrays -syntax and use
Class notes(week 4) on arrays and strings
Eo gaddis java_chapter_07_5e
Ad

More from AndiNurkholis1 (20)

PDF
Data Structure - 4 Pointer & Linked List
PDF
Data Structure - 3 Array: Concept & Implementation
PDF
Data Structure - 1 Learning Contract
PDF
Data Structure - 2 Introduction of Data Structure
PDF
Struktur Data - 4 Pointer & Linked List
PDF
Struktur Data - 3 Array: Konsep & Implementasi
PDF
Struktur Data - 2 Pengantar Struktur Data
PDF
Struktur Data - 1 Kontrak Perkuliahan
PDF
Technopreneurship - 9 Analisis Biaya dan Keuangan
PDF
Pengantar Bisnis - 14 Manajemen Keuangan
PDF
Pengantar Bisnis - 13 Manajemen Operasi
PDF
Pengantar Bisnis - 12 Kebijakan Harga
PDF
Pengantar Bisnis - 11 Kebijakan Distribusi
PDF
Technopreneurship - 8 Manajemen Sumber Daya Manusia
PDF
Pengantar Bisnis - 10 Kebijakan Produk
PDF
Technopreneurship - 7 Manajemen Pemasaran dan Operasional Bisnis
PDF
Pengantar Bisnis - 9 Manajemen Pemasaran
PDF
Technopreneurship - 6 Business Plan
PDF
Pengantar Bisnis - 8 Kepemimpinan
PDF
Technopreneurship - 5 Model Bisnis
Data Structure - 4 Pointer & Linked List
Data Structure - 3 Array: Concept & Implementation
Data Structure - 1 Learning Contract
Data Structure - 2 Introduction of Data Structure
Struktur Data - 4 Pointer & Linked List
Struktur Data - 3 Array: Konsep & Implementasi
Struktur Data - 2 Pengantar Struktur Data
Struktur Data - 1 Kontrak Perkuliahan
Technopreneurship - 9 Analisis Biaya dan Keuangan
Pengantar Bisnis - 14 Manajemen Keuangan
Pengantar Bisnis - 13 Manajemen Operasi
Pengantar Bisnis - 12 Kebijakan Harga
Pengantar Bisnis - 11 Kebijakan Distribusi
Technopreneurship - 8 Manajemen Sumber Daya Manusia
Pengantar Bisnis - 10 Kebijakan Produk
Technopreneurship - 7 Manajemen Pemasaran dan Operasional Bisnis
Pengantar Bisnis - 9 Manajemen Pemasaran
Technopreneurship - 6 Business Plan
Pengantar Bisnis - 8 Kepemimpinan
Technopreneurship - 5 Model Bisnis

Recently uploaded (20)

PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPT
Teaching material agriculture food technology
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Electronic commerce courselecture one. Pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Cloud computing and distributed systems.
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
cuic standard and advanced reporting.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Review of recent advances in non-invasive hemoglobin estimation
Teaching material agriculture food technology
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Electronic commerce courselecture one. Pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
The Rise and Fall of 3GPP – Time for a Sabbatical?
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
MYSQL Presentation for SQL database connectivity
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Cloud computing and distributed systems.
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Diabetes mellitus diagnosis method based random forest with bat algorithm
cuic standard and advanced reporting.pdf
Programs and apps: productivity, graphics, security and other tools
“AI and Expert System Decision Support & Business Intelligence Systems”
Building Integrated photovoltaic BIPV_UPV.pdf

Object Oriented Programming - 5.1. Array

  • 1. Object Oriented Programming Andi Nurkholis, S.Kom., M.Kom. Study Program of Informatics Faculty of Engineering and Computer Science SY. 2019-2020 March 19, 2020
  • 3. 3 Array Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
  • 4. Java Array 4 In Java, to declare an array, define the variable type with square brackets: String[] cars; To insert values to it, we can use an array literal - place the values in a comma-separated list, inside curly braces: String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
  • 5. Access the Elements of an Array You access an array element by referring to the index number. This statement accesses the value of the first element in cars: 5 String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; System.out.println(cars[0]); // Outputs Volvo
  • 6. Change an Array Element 6 To change the value of a specific element, refer to the index number: String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; cars[0] = "Opel"; System.out.println(cars[0]); // Now outputs Opel instead of Volvo
  • 7. 7 Array Length To find out how many elements an array has, use the length property: String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; System.out.println(cars.length); // Outputs 4
  • 8. Loop Through an Array 8 You can loop through the array elements with the for loop, and use the length property to specify how many times the loop should run. The following example outputs all elements in the cars array: String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; for (int i = 0; i < cars.length; i++) { System.out.println(cars[i]); } // Outputs Volvo, BMW, Ford, Mazda
  • 9. Multidimensional Arrays A multidimensional array is an array containing one or more arrays. To create a two-dimensional array, add each array within its own set of curly braces: 9
  • 10. Access the Element of an Array 10 To access the elements of the myNumbers array, specify two indexes: one for the array, and one for the element inside that array. This example accesses the third element (2) in the second array (1) of myNumbers: int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} }; int x = myNumbers[1][2]; System.out.println(x); // Outputs 7
  • 11. 11 Loop Through an Array public class MyClass { public static void main(String[] args) { int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} }; for (int i = 0; i < myNumbers.length; ++i) { for(int j = 0; j < myNumbers[i].length; ++j) { System.out.println(myNumbers[i][j]); } } } }
  • 13. Create an Array 13 You can create an array by using the new operator with the following syntax: arrayRefVar = new dataType[arraySize]; The above statement does two things: • It creates an array using new dataType[arraySize]. • It assigns the reference of the newly created array to the variable arrayRefVar. dataType arrayRefVar[];
  • 15. The Foreach Loops 15 JDK 1.5 introduced a new for loop known as foreach loop or enhanced for loop, which enables you to traverse the complete array sequentially without using an index variable. int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} }; int x = myNumbers[1][2]; System.out.println(x); // Outputs 7
  • 16. 16 Example public class TestArray { public static void main(String[] args) { double[] myList = {1.9, 2.9, 3.4, 3.5}; // Print all the array elements for (double element: myList) { System.out.println(element); } } }
  • 17. Passing Arrays to Methods 17 public static void printArray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } } //printArray(new int[]{3, 1, 2, 6, 4, 2}); Just as you can pass primitive type values to methods, you can also pass arrays to methods. For example, the following method displays the elements in an int array:
  • 18. Thank You, Next … Relation between Classes Study Program of Informatics Faculty of Engineering and Computer Science SY. 2019-2020 Andi Nurkholis, S.Kom., M.Kom. March 19, 2020