SlideShare a Scribd company logo
Java - Arrays
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Introduction
 An array is a data structure that consists of an ordered
collection of similar items.
 An array has a single name.
 The items in an array are referred / accessed in terms of
their position / index in the array.
 The items in an array are called elements.
 All of the elements need to be of the same type.
 The type can be any primitive or reference type.
 The length of an array is measured by the number of
elements.
 The first element is element[0], the second is
element[1], etc.
 An item’s position with an array is its index or subscript.
2Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Introduction
3Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Introduction
 Declaring Array Variables (One Dimensional Arrays)
 Creating Arrays
 Declaring and Creating in One Step
 Array Initialization
 Default Values
 The Length of an Array
 Array Size through Input
 Accessing Array Elements
 Validating Indexes
 Two Dimensional Arrays
4Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Declaring Array Variables
 Syntax: datatype[] arrayRefVar;
 Example: double[] myList;
 Alternative syntax: datatype arrayRefVar[];
// This style is allowed, but not preferred
 Example:
 double myDouble[];
 Int[] myInt;
5Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Creating Arrays
 Syntax:
arrayRefVar = new datatype[arraySize];
 Example:
myDouble = new double[10];
 myDouble[0] references the first element in the array.
 myDouble[9] references the last element in the array.
myInt = new int[5];
6Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Declaring and Creating in One Step
 Syntax:
datatype[] arrayRefVar = new datatype[arraySize];
 Example: double[] myDouble = new double[10];
 Alternative Syntax:
datatype arrayRefVar[] = new datatype[arraySize];
 Example: int myInt[] = new int[10];
String[] myString = new String[5];
MyClass[] myObject= new MyClass[2]; //object
7Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Array Initializers
 Java has a shorthand notation to declare, create, and
initialize an array in one statement:
double[] myList = {1.9, 2.9, 3.4, 3.5};
 This shorthand notation is equivalent to the following
statements:
double[] myList = new double[4];
myList[0] = 1.9;
myList[1] = 2.9;
myList[2] = 3.4;
myList[3] = 3.5;
8Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
CAUTION
 This shorthand syntax must be in one statement
 The following is incorrect:
double[] myList = new double[4];
myList= {1.9, 2.9, 3.4, 3.5};
9Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Default Initialization
 When array is created, array elements are initialized
 Numeric values (int, double, etc.) to 0
 Boolean values to false
 Char values to ‘u0000’ (unicode for blank character)
 Class types to null
10Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
The Length of an Array
 Once an array is created, its size is fixed
 Array size cannot be changed
 We can find its size using the following syntax:
arrayRefVar.length
 Note that length is a constant, not a method
 There is no parentheses following length
 For example, myList.length returns 10
 myInt.length
Sample Code:
long[] primes = new long[20];
System.out.println(primes.length);
Output: 20
11Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Accessing Array Elements
 Index of an array is defined as
 Positive int, byte or short values
 Expression that results into these types
 Any other types used for index will give error
 long, double, etc.
 Incase Expression results in long, then type cast to int
 Indexing starts from 0 and ends at N-1
primes[2]=0;
int k = primes[2];
…
12Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Array Size through Input
13Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
What happens if …
long[] primes = new long[20];
primes[25]=33;
….
Runtime Error:
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 25
at MorePrimes.main(MorePrimes.java:6)
14Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
15
Copying Arrays (The Wrong Way)
 Often, in a program, you need to duplicate an array or a part of an array.
In such cases you could attempt to use the assignment statement (=), as
follows:
myList1= new int[10];
myList2 = new int[10];
myList2 = myList1;
Contents
of myList1
list1
Contents
of myList2
list2
Before the assignment
Contents
of myList1
list1
Contents
of myList2
list2
After the assignment
Garbage
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
16
Copying Arrays (The Correct Way)
 Using a loop:
int[] sourceArray = {2, 3, 1, 5, 10};
int[] targetArray = new int[sourceArray.length];
for (int i = 0; i < sourceArrays.length; i++)
targetArray[i] = sourceArray[i];
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
17
Passing Arrays to Methods
 Defining a method that takes an int array as a formal
parameter
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
 Invoking the method with an int array as an argument
(actual parameter)
int[] list = {3, 1, 2, 6, 4, 2};
printArray(list);
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
18
Returning an Array from a Method
 Define the method that returns an array
public static int[] reverse(int[] list) {
int[] result = new int[list.length];
for (int i = 0, j = result.length - 1;
i < list.length; i++, j--) {
result[j] = list[i];
}
return result;
}
 Call the method
int[] list1 = {1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Arrays of Arrays
 Two-Dimensional arrays
 float[][] temperature=new float[10][365];
 10 arrays each having 365 elements
 First index: specifies array (row)
 Second Index: specifies element in that array (column)
 In JAVA float is 4 bytes, total Size=4*10*365=14,600 bytes
19Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Graphical Representation
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
Sample[0]
Sample[1]
Sample[2]
20Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Initializing Array of Arrays
int[][] array2D = {
{99, 42, 74, 83, 100},
{90, 91, 72, 88, 95},
{88, 61, 74, 89, 96},
{61, 89, 82, 98, 93},
{93, 73, 75, 78, 99},
};
//5 arrays with 5 elements each
21Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Sample Program
class unevenExample3
{
public static void main( String[] arg )
{ // declare and construct a 2D array
int[][] uneven = { { 1, 9, 4 }, { 0, 2}, { 0, 1, 2, 3, 4 } };
// print out the array
for ( int row=0; row < uneven.length; row++ ) //changes row
{
System.out.print("Row " + row + ": ");
for ( int col=0; col < uneven[row].length; col++ ) //changes column
System.out.print( uneven[row][col] + " ");
System.out.println();
}
}
}
22Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Output
Row 0: 1 9 4
Row 1: 0 2
Row 2: 0 1 2 3 4
23Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
Multidimensional Arrays
 A farmer has 10 farms of beans each in 5 countries, and
each farm has 30 fields!
 Three-dimensional array
long[][][] beans=new long[5][10][30];
//beans[country][farm][fields]
24Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
25
Examples of Arrays Processing
 Initializing an array with random values between 0.0
and 99.0
double[] myList = new double[4];
for (int i = 0; i < myList.length; i++) {
myList[i] = Math.random() * 100;
}
 Printing an array
for (int i = 0; i < myList.length; i++) {
System.out.print(myList[i] + " ");
}
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
26
 Summing all elements
double total = 0;
for (int i = 0; i < myList.length; i++) {
total += myList[i];
}
 Finding the largest element
double max = myList[0];
for (int i = 1; i < myList.length; i++) {
if (myList[i] > max)
max = myList[i];
}
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
27
 Finding the smallest index of the largest element
double max = myList[0];
int indexOfMax = 0;
for (int i = 1; i < myList.length; i++) {
if (myList[i] > max) {
max = myList[i];
indexOfMax = i;
}
}
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
28
The End…
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam

More Related Content

What's hot

DSA 103 Object Oriented Programming :: Week 3
DSA 103 Object Oriented Programming :: Week 3DSA 103 Object Oriented Programming :: Week 3
DSA 103 Object Oriented Programming :: Week 3
Ferdin Joe John Joseph PhD
 
Icom4015 lecture14-f16
Icom4015 lecture14-f16Icom4015 lecture14-f16
Icom4015 lecture14-f16
BienvenidoVelezUPR
 
Java™ (OOP) - Chapter 6: "Arrays"
Java™ (OOP) - Chapter 6: "Arrays"Java™ (OOP) - Chapter 6: "Arrays"
Java™ (OOP) - Chapter 6: "Arrays"
Gouda Mando
 
Icom4015 lecture13-f16
Icom4015 lecture13-f16Icom4015 lecture13-f16
Icom4015 lecture13-f16
BienvenidoVelezUPR
 
Icom4015 lecture15-f16
Icom4015 lecture15-f16Icom4015 lecture15-f16
Icom4015 lecture15-f16
BienvenidoVelezUPR
 
James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on Python
CP-Union
 
6 arrays injava
6 arrays injava6 arrays injava
6 arrays injava
irdginfo
 
Hub102 - JS - Lesson3
Hub102 - JS - Lesson3Hub102 - JS - Lesson3
Hub102 - JS - Lesson3
Tiểu Hổ
 
arrays-120712074248-phpapp01
arrays-120712074248-phpapp01arrays-120712074248-phpapp01
arrays-120712074248-phpapp01
Abdul Samee
 
05slide
05slide05slide
Icom4015 lecture4-f17
Icom4015 lecture4-f17Icom4015 lecture4-f17
Icom4015 lecture4-f17
BienvenidoVelezUPR
 
Advanced Programming Lecture 6 Fall 2016
Advanced Programming Lecture 6 Fall 2016Advanced Programming Lecture 6 Fall 2016
Advanced Programming Lecture 6 Fall 2016
BienvenidoVelezUPR
 
Icom4015 lecture7-f16
Icom4015 lecture7-f16Icom4015 lecture7-f16
Icom4015 lecture7-f16
BienvenidoVelezUPR
 
Data structures
Data structuresData structures
Data structures
Saurabh Mishra
 
Computer programming 2 Lesson 13
Computer programming 2  Lesson 13Computer programming 2  Lesson 13
Computer programming 2 Lesson 13
MLG College of Learning, Inc
 
18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set
Intro C# Book
 
Analytical Study and Newer Approach towards Frequent Pattern Mining using Boo...
Analytical Study and Newer Approach towards Frequent Pattern Mining using Boo...Analytical Study and Newer Approach towards Frequent Pattern Mining using Boo...
Analytical Study and Newer Approach towards Frequent Pattern Mining using Boo...
iosrjce
 
Icom4015 lecture4-f16
Icom4015 lecture4-f16Icom4015 lecture4-f16
Icom4015 lecture4-f16
BienvenidoVelezUPR
 
Chapter 10: hashing data structure
Chapter 10:  hashing data structureChapter 10:  hashing data structure
Chapter 10: hashing data structure
Mahmoud Alfarra
 

What's hot (19)

DSA 103 Object Oriented Programming :: Week 3
DSA 103 Object Oriented Programming :: Week 3DSA 103 Object Oriented Programming :: Week 3
DSA 103 Object Oriented Programming :: Week 3
 
Icom4015 lecture14-f16
Icom4015 lecture14-f16Icom4015 lecture14-f16
Icom4015 lecture14-f16
 
Java™ (OOP) - Chapter 6: "Arrays"
Java™ (OOP) - Chapter 6: "Arrays"Java™ (OOP) - Chapter 6: "Arrays"
Java™ (OOP) - Chapter 6: "Arrays"
 
Icom4015 lecture13-f16
Icom4015 lecture13-f16Icom4015 lecture13-f16
Icom4015 lecture13-f16
 
Icom4015 lecture15-f16
Icom4015 lecture15-f16Icom4015 lecture15-f16
Icom4015 lecture15-f16
 
James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on Python
 
6 arrays injava
6 arrays injava6 arrays injava
6 arrays injava
 
Hub102 - JS - Lesson3
Hub102 - JS - Lesson3Hub102 - JS - Lesson3
Hub102 - JS - Lesson3
 
arrays-120712074248-phpapp01
arrays-120712074248-phpapp01arrays-120712074248-phpapp01
arrays-120712074248-phpapp01
 
05slide
05slide05slide
05slide
 
Icom4015 lecture4-f17
Icom4015 lecture4-f17Icom4015 lecture4-f17
Icom4015 lecture4-f17
 
Advanced Programming Lecture 6 Fall 2016
Advanced Programming Lecture 6 Fall 2016Advanced Programming Lecture 6 Fall 2016
Advanced Programming Lecture 6 Fall 2016
 
Icom4015 lecture7-f16
Icom4015 lecture7-f16Icom4015 lecture7-f16
Icom4015 lecture7-f16
 
Data structures
Data structuresData structures
Data structures
 
Computer programming 2 Lesson 13
Computer programming 2  Lesson 13Computer programming 2  Lesson 13
Computer programming 2 Lesson 13
 
18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set
 
Analytical Study and Newer Approach towards Frequent Pattern Mining using Boo...
Analytical Study and Newer Approach towards Frequent Pattern Mining using Boo...Analytical Study and Newer Approach towards Frequent Pattern Mining using Boo...
Analytical Study and Newer Approach towards Frequent Pattern Mining using Boo...
 
Icom4015 lecture4-f16
Icom4015 lecture4-f16Icom4015 lecture4-f16
Icom4015 lecture4-f16
 
Chapter 10: hashing data structure
Chapter 10:  hashing data structureChapter 10:  hashing data structure
Chapter 10: hashing data structure
 

Similar to Java - Arrays Concepts

Array
ArrayArray
07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptx
MURADSANJOUM
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
shafat6712
 
Array
ArrayArray
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1
sotlsoc
 
Lecture 6
Lecture 6Lecture 6
07slide.ppt
07slide.ppt07slide.ppt
07slide.ppt
ankurgupta857016
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
Aseelhalees
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
Maulen Bale
 
Array
ArrayArray
Array
PRN USM
 
Arrays C#
Arrays C#Arrays C#
Chap1 array
Chap1 arrayChap1 array
Chap1 array
raksharao
 
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
 
javaArrays.pptx
javaArrays.pptxjavaArrays.pptx
javaArrays.pptx
AshishNayyar11
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
Tareq Hasan
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
Jussi Pohjolainen
 
07 Arrays
07 Arrays07 Arrays
07 Arrays
maznabili
 
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
 
CH1 ARRAY (1).pptx
CH1 ARRAY (1).pptxCH1 ARRAY (1).pptx
CH1 ARRAY (1).pptx
AnkitaVerma776806
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
TaseerRao
 

Similar to Java - Arrays Concepts (20)

Array
ArrayArray
Array
 
07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptx
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
Array
ArrayArray
Array
 
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
 
07slide.ppt
07slide.ppt07slide.ppt
07slide.ppt
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
 
Array
ArrayArray
Array
 
Arrays C#
Arrays C#Arrays C#
Arrays C#
 
Chap1 array
Chap1 arrayChap1 array
Chap1 array
 
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
 
javaArrays.pptx
javaArrays.pptxjavaArrays.pptx
javaArrays.pptx
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
07 Arrays
07 Arrays07 Arrays
07 Arrays
 
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
 
CH1 ARRAY (1).pptx
CH1 ARRAY (1).pptxCH1 ARRAY (1).pptx
CH1 ARRAY (1).pptx
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
 

More from Victer Paul

OOAD - UML - Sequence and Communication Diagrams - Lab
OOAD - UML - Sequence and Communication Diagrams - LabOOAD - UML - Sequence and Communication Diagrams - Lab
OOAD - UML - Sequence and Communication Diagrams - Lab
Victer Paul
 
OOAD - UML - Class and Object Diagrams - Lab
OOAD - UML - Class and Object Diagrams - LabOOAD - UML - Class and Object Diagrams - Lab
OOAD - UML - Class and Object Diagrams - Lab
Victer Paul
 
OOAD - Systems and Object Orientation Concepts
OOAD - Systems and Object Orientation ConceptsOOAD - Systems and Object Orientation Concepts
OOAD - Systems and Object Orientation Concepts
Victer Paul
 
Java - Strings Concepts
Java - Strings ConceptsJava - Strings Concepts
Java - Strings Concepts
Victer Paul
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
Victer Paul
 
Java - OOPS and Java Basics
Java - OOPS and Java BasicsJava - OOPS and Java Basics
Java - OOPS and Java Basics
Victer Paul
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
Victer Paul
 
Java - Object Oriented Programming Concepts
Java - Object Oriented Programming ConceptsJava - Object Oriented Programming Concepts
Java - Object Oriented Programming Concepts
Victer Paul
 
Java - Basic Concepts
Java - Basic ConceptsJava - Basic Concepts
Java - Basic Concepts
Victer Paul
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
Victer Paul
 
Java - Inheritance Concepts
Java - Inheritance ConceptsJava - Inheritance Concepts
Java - Inheritance Concepts
Victer Paul
 
Java applet programming concepts
Java  applet programming conceptsJava  applet programming concepts
Java applet programming concepts
Victer Paul
 

More from Victer Paul (12)

OOAD - UML - Sequence and Communication Diagrams - Lab
OOAD - UML - Sequence and Communication Diagrams - LabOOAD - UML - Sequence and Communication Diagrams - Lab
OOAD - UML - Sequence and Communication Diagrams - Lab
 
OOAD - UML - Class and Object Diagrams - Lab
OOAD - UML - Class and Object Diagrams - LabOOAD - UML - Class and Object Diagrams - Lab
OOAD - UML - Class and Object Diagrams - Lab
 
OOAD - Systems and Object Orientation Concepts
OOAD - Systems and Object Orientation ConceptsOOAD - Systems and Object Orientation Concepts
OOAD - Systems and Object Orientation Concepts
 
Java - Strings Concepts
Java - Strings ConceptsJava - Strings Concepts
Java - Strings Concepts
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
 
Java - OOPS and Java Basics
Java - OOPS and Java BasicsJava - OOPS and Java Basics
Java - OOPS and Java Basics
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
 
Java - Object Oriented Programming Concepts
Java - Object Oriented Programming ConceptsJava - Object Oriented Programming Concepts
Java - Object Oriented Programming Concepts
 
Java - Basic Concepts
Java - Basic ConceptsJava - Basic Concepts
Java - Basic Concepts
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
 
Java - Inheritance Concepts
Java - Inheritance ConceptsJava - Inheritance Concepts
Java - Inheritance Concepts
 
Java applet programming concepts
Java  applet programming conceptsJava  applet programming concepts
Java applet programming concepts
 

Recently uploaded

みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website
Pixlogix Infotech
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Zilliz
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
Rohit Gautam
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 

Recently uploaded (20)

みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 

Java - Arrays Concepts

  • 1. Java - Arrays Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 2. Introduction  An array is a data structure that consists of an ordered collection of similar items.  An array has a single name.  The items in an array are referred / accessed in terms of their position / index in the array.  The items in an array are called elements.  All of the elements need to be of the same type.  The type can be any primitive or reference type.  The length of an array is measured by the number of elements.  The first element is element[0], the second is element[1], etc.  An item’s position with an array is its index or subscript. 2Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 3. Introduction 3Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 4. Introduction  Declaring Array Variables (One Dimensional Arrays)  Creating Arrays  Declaring and Creating in One Step  Array Initialization  Default Values  The Length of an Array  Array Size through Input  Accessing Array Elements  Validating Indexes  Two Dimensional Arrays 4Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 5. Declaring Array Variables  Syntax: datatype[] arrayRefVar;  Example: double[] myList;  Alternative syntax: datatype arrayRefVar[]; // This style is allowed, but not preferred  Example:  double myDouble[];  Int[] myInt; 5Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 6. Creating Arrays  Syntax: arrayRefVar = new datatype[arraySize];  Example: myDouble = new double[10];  myDouble[0] references the first element in the array.  myDouble[9] references the last element in the array. myInt = new int[5]; 6Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 7. Declaring and Creating in One Step  Syntax: datatype[] arrayRefVar = new datatype[arraySize];  Example: double[] myDouble = new double[10];  Alternative Syntax: datatype arrayRefVar[] = new datatype[arraySize];  Example: int myInt[] = new int[10]; String[] myString = new String[5]; MyClass[] myObject= new MyClass[2]; //object 7Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 8. Array Initializers  Java has a shorthand notation to declare, create, and initialize an array in one statement: double[] myList = {1.9, 2.9, 3.4, 3.5};  This shorthand notation is equivalent to the following statements: double[] myList = new double[4]; myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5; 8Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 9. CAUTION  This shorthand syntax must be in one statement  The following is incorrect: double[] myList = new double[4]; myList= {1.9, 2.9, 3.4, 3.5}; 9Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 10. Default Initialization  When array is created, array elements are initialized  Numeric values (int, double, etc.) to 0  Boolean values to false  Char values to ‘u0000’ (unicode for blank character)  Class types to null 10Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 11. The Length of an Array  Once an array is created, its size is fixed  Array size cannot be changed  We can find its size using the following syntax: arrayRefVar.length  Note that length is a constant, not a method  There is no parentheses following length  For example, myList.length returns 10  myInt.length Sample Code: long[] primes = new long[20]; System.out.println(primes.length); Output: 20 11Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 12. Accessing Array Elements  Index of an array is defined as  Positive int, byte or short values  Expression that results into these types  Any other types used for index will give error  long, double, etc.  Incase Expression results in long, then type cast to int  Indexing starts from 0 and ends at N-1 primes[2]=0; int k = primes[2]; … 12Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 13. Array Size through Input 13Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 14. What happens if … long[] primes = new long[20]; primes[25]=33; …. Runtime Error: Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 25 at MorePrimes.main(MorePrimes.java:6) 14Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 15. 15 Copying Arrays (The Wrong Way)  Often, in a program, you need to duplicate an array or a part of an array. In such cases you could attempt to use the assignment statement (=), as follows: myList1= new int[10]; myList2 = new int[10]; myList2 = myList1; Contents of myList1 list1 Contents of myList2 list2 Before the assignment Contents of myList1 list1 Contents of myList2 list2 After the assignment Garbage Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 16. 16 Copying Arrays (The Correct Way)  Using a loop: int[] sourceArray = {2, 3, 1, 5, 10}; int[] targetArray = new int[sourceArray.length]; for (int i = 0; i < sourceArrays.length; i++) targetArray[i] = sourceArray[i]; Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 17. 17 Passing Arrays to Methods  Defining a method that takes an int array as a formal parameter public static void printArray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } }  Invoking the method with an int array as an argument (actual parameter) int[] list = {3, 1, 2, 6, 4, 2}; printArray(list); Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 18. 18 Returning an Array from a Method  Define the method that returns an array public static int[] reverse(int[] list) { int[] result = new int[list.length]; for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; }  Call the method int[] list1 = {1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 19. Arrays of Arrays  Two-Dimensional arrays  float[][] temperature=new float[10][365];  10 arrays each having 365 elements  First index: specifies array (row)  Second Index: specifies element in that array (column)  In JAVA float is 4 bytes, total Size=4*10*365=14,600 bytes 19Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 20. Graphical Representation 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 Sample[0] Sample[1] Sample[2] 20Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 21. Initializing Array of Arrays int[][] array2D = { {99, 42, 74, 83, 100}, {90, 91, 72, 88, 95}, {88, 61, 74, 89, 96}, {61, 89, 82, 98, 93}, {93, 73, 75, 78, 99}, }; //5 arrays with 5 elements each 21Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 22. Sample Program class unevenExample3 { public static void main( String[] arg ) { // declare and construct a 2D array int[][] uneven = { { 1, 9, 4 }, { 0, 2}, { 0, 1, 2, 3, 4 } }; // print out the array for ( int row=0; row < uneven.length; row++ ) //changes row { System.out.print("Row " + row + ": "); for ( int col=0; col < uneven[row].length; col++ ) //changes column System.out.print( uneven[row][col] + " "); System.out.println(); } } } 22Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 23. Output Row 0: 1 9 4 Row 1: 0 2 Row 2: 0 1 2 3 4 23Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 24. Multidimensional Arrays  A farmer has 10 farms of beans each in 5 countries, and each farm has 30 fields!  Three-dimensional array long[][][] beans=new long[5][10][30]; //beans[country][farm][fields] 24Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 25. 25 Examples of Arrays Processing  Initializing an array with random values between 0.0 and 99.0 double[] myList = new double[4]; for (int i = 0; i < myList.length; i++) { myList[i] = Math.random() * 100; }  Printing an array for (int i = 0; i < myList.length; i++) { System.out.print(myList[i] + " "); } Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 26. 26  Summing all elements double total = 0; for (int i = 0; i < myList.length; i++) { total += myList[i]; }  Finding the largest element double max = myList[0]; for (int i = 1; i < myList.length; i++) { if (myList[i] > max) max = myList[i]; } Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 27. 27  Finding the smallest index of the largest element double max = myList[0]; int indexOfMax = 0; for (int i = 1; i < myList.length; i++) { if (myList[i] > max) { max = myList[i]; indexOfMax = i; } } Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
  • 28. 28 The End… Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam