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

What's hot (20)

Arrays
ArraysArrays
Arrays
Shakila Mahjabin
 
7array in c#
7array in c#7array in c#
7array in c#
Sireesh K
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
Gagan Deep
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
Tareq Hasan
 
Arrays Class presentation
Arrays Class presentationArrays Class presentation
Arrays Class presentation
Neveen Reda
 
Arrays in c
Arrays in cArrays in c
Arrays in c
baabtra.com - No. 1 supplier of quality freshers
 
Array in C# 3.5
Array in C# 3.5Array in C# 3.5
Array in C# 3.5
Gopal Ji Singh
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java language
Hareem Naz
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
Shubham Sharma
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
Rajendran
 
Computer programming 2 Lesson 13
Computer programming 2  Lesson 13Computer programming 2  Lesson 13
Computer programming 2 Lesson 13
MLG College of Learning, Inc
 
Arrays In C
Arrays In CArrays In C
Arrays In C
yndaravind
 
Array BPK 2
Array BPK 2Array BPK 2
Array BPK 2
Riki Afriansyah
 
Array in c++
Array in c++Array in c++
Array in c++
Mahesha Mano
 
C++ programming (Array)
C++ programming (Array)C++ programming (Array)
C++ programming (Array)
طارق بالحارث
 
Array in Java
Array in JavaArray in Java
Array in Java
Shehrevar Davierwala
 
Arrays in c
Arrays in cArrays in c
Arrays in c
Jeeva Nanthini
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
imtiazalijoono
 
intorduction to Arrays in java
intorduction to Arrays in javaintorduction to Arrays in java
intorduction to Arrays in java
Muthukumaran Subramanian
 
One dimensional arrays
One dimensional arraysOne dimensional arrays
One dimensional arrays
Satyam Soni
 

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

Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
TaseerRao
 
17-Arrays en java presentación documento
17-Arrays en java presentación documento17-Arrays en java presentación documento
17-Arrays en java presentación documento
DiegoGamboaSafla
 
Lec 1.5 Object Oriented Programming
Lec 1.5 Object Oriented ProgrammingLec 1.5 Object Oriented Programming
Lec 1.5 Object Oriented Programming
Badar Waseer
 
Arrays in java.pptx
Arrays in java.pptxArrays in java.pptx
Arrays in java.pptx
Nagaraju Pamarthi
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
OXUS 20
 
ch06.ppt
ch06.pptch06.ppt
ch06.ppt
AqeelAbbas94
 
ch06.ppt
ch06.pptch06.ppt
ch06.ppt
ansariparveen06
 
array Details
array Detailsarray Details
array Details
shivas379526
 
ch06.ppt
ch06.pptch06.ppt
ch06.ppt
chandrasekar529044
 
Array
ArrayArray
Array
Kongu Engineering College, Perundurai, Erode
 
Java arrays (1)
Java arrays (1)Java arrays (1)
Java arrays (1)
Liza Abello
 
Chap1 array
Chap1 arrayChap1 array
Chap1 array
raksharao
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
Maulen Bale
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Naz Abdalla
 
javaarray
javaarrayjavaarray
javaarray
Arjun Shanka
 
OOPs with java
OOPs with javaOOPs with java
OOPs with java
AAKANKSHA JAIN
 
Java chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and useJava chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and use
Mukesh Tekwani
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
Eo gaddis java_chapter_07_5e
Eo gaddis java_chapter_07_5eEo gaddis java_chapter_07_5e
Eo gaddis java_chapter_07_5e
Gina Bullock
 
07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptx
MURADSANJOUM
 
Ad

More from AndiNurkholis1 (20)

Technopreneurship - 9 Analisis Biaya dan Keuangan
Technopreneurship - 9 Analisis Biaya dan KeuanganTechnopreneurship - 9 Analisis Biaya dan Keuangan
Technopreneurship - 9 Analisis Biaya dan Keuangan
AndiNurkholis1
 
Pengantar Bisnis - 14 Manajemen Keuangan
Pengantar Bisnis - 14 Manajemen KeuanganPengantar Bisnis - 14 Manajemen Keuangan
Pengantar Bisnis - 14 Manajemen Keuangan
AndiNurkholis1
 
Pengantar Bisnis - 13 Manajemen Operasi
Pengantar Bisnis - 13 Manajemen OperasiPengantar Bisnis - 13 Manajemen Operasi
Pengantar Bisnis - 13 Manajemen Operasi
AndiNurkholis1
 
Pengantar Bisnis - 12 Kebijakan Harga
Pengantar Bisnis - 12 Kebijakan HargaPengantar Bisnis - 12 Kebijakan Harga
Pengantar Bisnis - 12 Kebijakan Harga
AndiNurkholis1
 
Pengantar Bisnis - 11 Kebijakan Distribusi
Pengantar Bisnis - 11 Kebijakan DistribusiPengantar Bisnis - 11 Kebijakan Distribusi
Pengantar Bisnis - 11 Kebijakan Distribusi
AndiNurkholis1
 
Technopreneurship - 8 Manajemen Sumber Daya Manusia
Technopreneurship - 8 Manajemen Sumber Daya ManusiaTechnopreneurship - 8 Manajemen Sumber Daya Manusia
Technopreneurship - 8 Manajemen Sumber Daya Manusia
AndiNurkholis1
 
Pengantar Bisnis - 10 Kebijakan Produk
Pengantar Bisnis - 10 Kebijakan ProdukPengantar Bisnis - 10 Kebijakan Produk
Pengantar Bisnis - 10 Kebijakan Produk
AndiNurkholis1
 
Technopreneurship - 7 Manajemen Pemasaran dan Operasional Bisnis
Technopreneurship - 7 Manajemen Pemasaran dan Operasional BisnisTechnopreneurship - 7 Manajemen Pemasaran dan Operasional Bisnis
Technopreneurship - 7 Manajemen Pemasaran dan Operasional Bisnis
AndiNurkholis1
 
Pengantar Bisnis - 9 Manajemen Pemasaran
Pengantar Bisnis - 9 Manajemen PemasaranPengantar Bisnis - 9 Manajemen Pemasaran
Pengantar Bisnis - 9 Manajemen Pemasaran
AndiNurkholis1
 
Technopreneurship - 6 Business Plan
Technopreneurship - 6 Business PlanTechnopreneurship - 6 Business Plan
Technopreneurship - 6 Business Plan
AndiNurkholis1
 
Pengantar Bisnis - 8 Kepemimpinan
Pengantar Bisnis - 8 KepemimpinanPengantar Bisnis - 8 Kepemimpinan
Pengantar Bisnis - 8 Kepemimpinan
AndiNurkholis1
 
Technopreneurship - 5 Model Bisnis
Technopreneurship - 5 Model BisnisTechnopreneurship - 5 Model Bisnis
Technopreneurship - 5 Model Bisnis
AndiNurkholis1
 
Technopreneurship - 4 Studi Kelayakan Usaha
Technopreneurship - 4 Studi Kelayakan UsahaTechnopreneurship - 4 Studi Kelayakan Usaha
Technopreneurship - 4 Studi Kelayakan Usaha
AndiNurkholis1
 
Pengantar Bisnis - 7 Motivasi Kerja
Pengantar Bisnis - 7 Motivasi KerjaPengantar Bisnis - 7 Motivasi Kerja
Pengantar Bisnis - 7 Motivasi Kerja
AndiNurkholis1
 
Pengantar Bisnis - 6 Manajemen Sumber Daya Manusia
Pengantar Bisnis - 6 Manajemen Sumber Daya ManusiaPengantar Bisnis - 6 Manajemen Sumber Daya Manusia
Pengantar Bisnis - 6 Manajemen Sumber Daya Manusia
AndiNurkholis1
 
Pengantar Bisnis - 5 Pengelolaan & Pengorganisasian Bisnis
Pengantar Bisnis - 5 Pengelolaan & Pengorganisasian BisnisPengantar Bisnis - 5 Pengelolaan & Pengorganisasian Bisnis
Pengantar Bisnis - 5 Pengelolaan & Pengorganisasian Bisnis
AndiNurkholis1
 
Technopreneurship - 3 Ide dan Prinsip Bisnis
Technopreneurship - 3 Ide dan Prinsip BisnisTechnopreneurship - 3 Ide dan Prinsip Bisnis
Technopreneurship - 3 Ide dan Prinsip Bisnis
AndiNurkholis1
 
Pengantar Bisnis - 4 Bentuk Organisasi Bisnis
Pengantar Bisnis - 4 Bentuk Organisasi BisnisPengantar Bisnis - 4 Bentuk Organisasi Bisnis
Pengantar Bisnis - 4 Bentuk Organisasi Bisnis
AndiNurkholis1
 
Technopreneurship - 2 Pengantar Technopreneurship
Technopreneurship - 2 Pengantar TechnopreneurshipTechnopreneurship - 2 Pengantar Technopreneurship
Technopreneurship - 2 Pengantar Technopreneurship
AndiNurkholis1
 
Pengantar Bisnis - 3 Globalisasi Ekonomi & Bisnis Internasional
Pengantar Bisnis - 3 Globalisasi Ekonomi & Bisnis InternasionalPengantar Bisnis - 3 Globalisasi Ekonomi & Bisnis Internasional
Pengantar Bisnis - 3 Globalisasi Ekonomi & Bisnis Internasional
AndiNurkholis1
 
Technopreneurship - 9 Analisis Biaya dan Keuangan
Technopreneurship - 9 Analisis Biaya dan KeuanganTechnopreneurship - 9 Analisis Biaya dan Keuangan
Technopreneurship - 9 Analisis Biaya dan Keuangan
AndiNurkholis1
 
Pengantar Bisnis - 14 Manajemen Keuangan
Pengantar Bisnis - 14 Manajemen KeuanganPengantar Bisnis - 14 Manajemen Keuangan
Pengantar Bisnis - 14 Manajemen Keuangan
AndiNurkholis1
 
Pengantar Bisnis - 13 Manajemen Operasi
Pengantar Bisnis - 13 Manajemen OperasiPengantar Bisnis - 13 Manajemen Operasi
Pengantar Bisnis - 13 Manajemen Operasi
AndiNurkholis1
 
Pengantar Bisnis - 12 Kebijakan Harga
Pengantar Bisnis - 12 Kebijakan HargaPengantar Bisnis - 12 Kebijakan Harga
Pengantar Bisnis - 12 Kebijakan Harga
AndiNurkholis1
 
Pengantar Bisnis - 11 Kebijakan Distribusi
Pengantar Bisnis - 11 Kebijakan DistribusiPengantar Bisnis - 11 Kebijakan Distribusi
Pengantar Bisnis - 11 Kebijakan Distribusi
AndiNurkholis1
 
Technopreneurship - 8 Manajemen Sumber Daya Manusia
Technopreneurship - 8 Manajemen Sumber Daya ManusiaTechnopreneurship - 8 Manajemen Sumber Daya Manusia
Technopreneurship - 8 Manajemen Sumber Daya Manusia
AndiNurkholis1
 
Pengantar Bisnis - 10 Kebijakan Produk
Pengantar Bisnis - 10 Kebijakan ProdukPengantar Bisnis - 10 Kebijakan Produk
Pengantar Bisnis - 10 Kebijakan Produk
AndiNurkholis1
 
Technopreneurship - 7 Manajemen Pemasaran dan Operasional Bisnis
Technopreneurship - 7 Manajemen Pemasaran dan Operasional BisnisTechnopreneurship - 7 Manajemen Pemasaran dan Operasional Bisnis
Technopreneurship - 7 Manajemen Pemasaran dan Operasional Bisnis
AndiNurkholis1
 
Pengantar Bisnis - 9 Manajemen Pemasaran
Pengantar Bisnis - 9 Manajemen PemasaranPengantar Bisnis - 9 Manajemen Pemasaran
Pengantar Bisnis - 9 Manajemen Pemasaran
AndiNurkholis1
 
Technopreneurship - 6 Business Plan
Technopreneurship - 6 Business PlanTechnopreneurship - 6 Business Plan
Technopreneurship - 6 Business Plan
AndiNurkholis1
 
Pengantar Bisnis - 8 Kepemimpinan
Pengantar Bisnis - 8 KepemimpinanPengantar Bisnis - 8 Kepemimpinan
Pengantar Bisnis - 8 Kepemimpinan
AndiNurkholis1
 
Technopreneurship - 5 Model Bisnis
Technopreneurship - 5 Model BisnisTechnopreneurship - 5 Model Bisnis
Technopreneurship - 5 Model Bisnis
AndiNurkholis1
 
Technopreneurship - 4 Studi Kelayakan Usaha
Technopreneurship - 4 Studi Kelayakan UsahaTechnopreneurship - 4 Studi Kelayakan Usaha
Technopreneurship - 4 Studi Kelayakan Usaha
AndiNurkholis1
 
Pengantar Bisnis - 7 Motivasi Kerja
Pengantar Bisnis - 7 Motivasi KerjaPengantar Bisnis - 7 Motivasi Kerja
Pengantar Bisnis - 7 Motivasi Kerja
AndiNurkholis1
 
Pengantar Bisnis - 6 Manajemen Sumber Daya Manusia
Pengantar Bisnis - 6 Manajemen Sumber Daya ManusiaPengantar Bisnis - 6 Manajemen Sumber Daya Manusia
Pengantar Bisnis - 6 Manajemen Sumber Daya Manusia
AndiNurkholis1
 
Pengantar Bisnis - 5 Pengelolaan & Pengorganisasian Bisnis
Pengantar Bisnis - 5 Pengelolaan & Pengorganisasian BisnisPengantar Bisnis - 5 Pengelolaan & Pengorganisasian Bisnis
Pengantar Bisnis - 5 Pengelolaan & Pengorganisasian Bisnis
AndiNurkholis1
 
Technopreneurship - 3 Ide dan Prinsip Bisnis
Technopreneurship - 3 Ide dan Prinsip BisnisTechnopreneurship - 3 Ide dan Prinsip Bisnis
Technopreneurship - 3 Ide dan Prinsip Bisnis
AndiNurkholis1
 
Pengantar Bisnis - 4 Bentuk Organisasi Bisnis
Pengantar Bisnis - 4 Bentuk Organisasi BisnisPengantar Bisnis - 4 Bentuk Organisasi Bisnis
Pengantar Bisnis - 4 Bentuk Organisasi Bisnis
AndiNurkholis1
 
Technopreneurship - 2 Pengantar Technopreneurship
Technopreneurship - 2 Pengantar TechnopreneurshipTechnopreneurship - 2 Pengantar Technopreneurship
Technopreneurship - 2 Pengantar Technopreneurship
AndiNurkholis1
 
Pengantar Bisnis - 3 Globalisasi Ekonomi & Bisnis Internasional
Pengantar Bisnis - 3 Globalisasi Ekonomi & Bisnis InternasionalPengantar Bisnis - 3 Globalisasi Ekonomi & Bisnis Internasional
Pengantar Bisnis - 3 Globalisasi Ekonomi & Bisnis Internasional
AndiNurkholis1
 
Ad

Recently uploaded (20)

Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptxFIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptxFIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdfEdge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
SOFTTECHHUB
 
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptxFIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Alliance
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
Edge AI and Vision Alliance
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdfvertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Analysis of the changes in the attitude of the news comments caused by knowin...
Analysis of the changes in the attitude of the news comments caused by knowin...Analysis of the changes in the attitude of the news comments caused by knowin...
Analysis of the changes in the attitude of the news comments caused by knowin...
Matsushita Laboratory
 
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
Edge AI and Vision Alliance
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptxFIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptxFIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptxFIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdfEdge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
SOFTTECHHUB
 
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptxFIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Alliance
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
Edge AI and Vision Alliance
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdfvertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Analysis of the changes in the attitude of the news comments caused by knowin...
Analysis of the changes in the attitude of the news comments caused by knowin...Analysis of the changes in the attitude of the news comments caused by knowin...
Analysis of the changes in the attitude of the news comments caused by knowin...
Matsushita Laboratory
 
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
Edge AI and Vision Alliance
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptxFIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 

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