SlideShare a Scribd company logo
4
Most read
7
Most read
13
Most read
Arrays
Dr. Kuppusamy .P
Associate Professor / SCOPE
Access Specifiers / modifier in Java
• Defines an access scope of the variable, methods and classes.
• Can change the access level of fields, constructors, methods, and
class by applying the access modifier on it.
1. Public: Accessible from any other class, methods or interface.
2. Private: Accessible within the class or method where it is
defined.
3. Protected: Accessible in the same class or its child class or in all
those classes which are defined in the same package.
4. Default: When none of the access specifiers is specified. Can be
accessed by all classes which are defined in same package only.
Dr. Kuppusamy P
Access Specifiers / modifier in Java
Dr. Kuppusamy P
Access
Modifier
within
class within
package
outside
package by
subclass
only
outside
package
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y
Arrays
• Array is a container object that contains a finite number of values
of a similar data type .
• When an array is created
• The length (size) of an array is fixed
• Array elements are automatically initialized with the default
value of their data type
• Two types
• One dimensional Array
• Multi-dimensional Array
• 2D Array, 3D Array
Dr. Kuppusamy P
One Dimensional Array
• Syntax:
• data-type[] variable = new data-type[array_length]
• Eg. int[] a = new int[6]; //define integer array with size 6
Dr. Kuppusamy P
Create and Initialize Array
• Create and initialize array as below
o int[] x = {10, 20, 30};
o int[] x = new int[] {10, 20, 30};
• Here the length of an array is determined by the number of
values provided between { and }.
• The built-in length property determines the size of any array
• E.g.
o int[] x = new int[10];
oint x_len = x.length;
Dr. Kuppusamy P
Example - Array
public class ArrayEx
{
public static void main(String[] args)
{
int[] marks; // declares an array of integers
marks = new int[5]; // allocates memory for 5integers
marks[0] = 11;
marks[4] = 22;
System.out.println("Element at index 0: " + marks[0]);
System.out.println("Element at index 1: " + marks[1]);
System.out.println("Element at index 4: " + marks[4]);
}
} Dr. Kuppusamy P
Array bounds and Array Resizing
• Array index (subscripts) begins with 0
• Can't access an array element beyond the defined range.
• Can't resize an array.
• Can use the same reference variable to refer new array.
• int x[] = new int[5];
• x= new int[10];
Dr. Kuppusamy P
Copying Array
• To copy array elements from one array to another array, use
arraycopy static method from System class.
Syntax:
public static void arraycopy(Object s, int sIndex, Object d, int dIndex, int length)
Ex:
int source[] = {1, 2, 3, 4, 5, 6};
int dest[] = new int[10];
System.arraycopy(source, 0, dest, 0, source.length);
Dr. Kuppusamy P
Two-Dimensional Array
• Two-dimensional array is a collection of 1-Dimensional arrays.
• Initializing two-dimensional arrays:
int[][] y = new int[2][4];
• The 1st dimension represent rows or number of one dimension
• The 2nd dimension represent columns or number of elements in the each one
dimensions
Jagged Array:
• Column representation is optional in two dimensional arrays.
int[][] y = new int[3][]
• The curly braces { } may also be used to initialize two dimensional arrays.
Eg: int[][] y = { {1,2,3}, {4,5,6}, {7,8,9} };
int[][] y = new int[3][]
Dr. Kuppusamy P
Two-Dimensional Array
• Array can be initialized with the row dimension and without initializing the
columns, but not vice versa
• int[][] x = new int[3][];
• int[][] x = new int[][3]; //error
• The length of the columns can vary for each row and initialize number of
columns in each row
Ex1:
• int [][] numbers = new int [3][];
• numbers[0] = new int[] {6,9,3,7}; //row1
• numbers[1] = new int [] {5,2} // row2
• numbers[2] = new int [] {8,4,1}; // row3
Dr. Kuppusamy P
Example: 2D-Array
public class TwoD
{
public static void main(String[] args)
{
int [][] x = new int[3][]; // initialize number of rows
x[0] = new int[3]; // define number of columns in each row
x[1] = new int[2];
x[2] = new int[5];
for(int i=0; i < x.length; i++) // print array elements
{
for (int j=0; j < x[i].length; j++)
{
x[i][j] = i;
System.out.print(x[i][j]+ ” “);
}
System.out.println();
}
}
}
Dr. Kuppusamy P
References
Dr. Kuppusamy P
Herbert Schildt, “Java: The Complete Reference”, McGraw-Hill Education, Tenth edition,
2017.

More Related Content

What's hot (20)

PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
PPTX
Arrays in java
bhavesh prakash
 
PPTX
Lecture_7-Encapsulation in Java.pptx
ShahinAhmed49
 
PPT
Applet life cycle
myrajendra
 
PDF
Strings in java
Kuppusamy P
 
PPTX
Ppt on this and super keyword
tanu_jaswal
 
PDF
Wrapper classes
Ravi_Kant_Sahu
 
PPTX
2- Dimensional Arrays
Education Front
 
PPTX
Multithreading in java
Monika Mishra
 
PDF
Java Arrays
OXUS 20
 
PPTX
Two-dimensional array in java
Talha mahmood
 
PPTX
SQL
Vineeta Garg
 
PPTX
Array in c language
home
 
PPSX
Stack
Seema Sharma
 
PDF
Class and Objects in Java
Spotle.ai
 
PPTX
Method overloading
Lovely Professional University
 
PPTX
Operator overloading
Ramish Suleman
 
PPTX
C string
University of Potsdam
 
PPT
Java interfaces
Raja Sekhar
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
Arrays in java
bhavesh prakash
 
Lecture_7-Encapsulation in Java.pptx
ShahinAhmed49
 
Applet life cycle
myrajendra
 
Strings in java
Kuppusamy P
 
Ppt on this and super keyword
tanu_jaswal
 
Wrapper classes
Ravi_Kant_Sahu
 
2- Dimensional Arrays
Education Front
 
Multithreading in java
Monika Mishra
 
Java Arrays
OXUS 20
 
Two-dimensional array in java
Talha mahmood
 
Array in c language
home
 
Class and Objects in Java
Spotle.ai
 
Method overloading
Lovely Professional University
 
Operator overloading
Ramish Suleman
 
Java interfaces
Raja Sekhar
 

Similar to Java arrays (20)

PPTX
OOPs with java
AAKANKSHA JAIN
 
PPT
L10 array
teach4uin
 
PPTX
javaArrays.pptx
AshishNayyar11
 
PPSX
dizital pods session 6-arrays.ppsx
VijayKumarLokanadam
 
PPTX
dizital pods session 6-arrays.pptx
VijayKumarLokanadam
 
PPTX
6_Array.pptx
shafat6712
 
PPTX
Arrays in programming
TaseerRao
 
PDF
Array
Ravi_Kant_Sahu
 
PPTX
Java arrays
BHUVIJAYAVELU
 
PPTX
Lecture 7 arrays
manish kumar
 
PDF
Learn Java Part 9
Gurpreet singh
 
PPT
Core java day2
Soham Sengupta
 
PPTX
Java Programming
Nanthini Kempaiyan
 
PPT
Data Structure Midterm Lesson Arrays
Maulen Bale
 
PDF
Arrays a detailed explanation and presentation
riazahamed37
 
PPTX
Arrays
Trupti Agrawal
 
PDF
Java Arrays
Soba Arjun
 
PPTX
Array.pptx
ssuser8698eb
 
PPTX
the array, which stores a fixed-size sequential collection of elements of the...
Kavitha S
 
DOCX
Java R20 - UNIT-3.docx
Pamarthi Kumar
 
OOPs with java
AAKANKSHA JAIN
 
L10 array
teach4uin
 
javaArrays.pptx
AshishNayyar11
 
dizital pods session 6-arrays.ppsx
VijayKumarLokanadam
 
dizital pods session 6-arrays.pptx
VijayKumarLokanadam
 
6_Array.pptx
shafat6712
 
Arrays in programming
TaseerRao
 
Java arrays
BHUVIJAYAVELU
 
Lecture 7 arrays
manish kumar
 
Learn Java Part 9
Gurpreet singh
 
Core java day2
Soham Sengupta
 
Java Programming
Nanthini Kempaiyan
 
Data Structure Midterm Lesson Arrays
Maulen Bale
 
Arrays a detailed explanation and presentation
riazahamed37
 
Java Arrays
Soba Arjun
 
Array.pptx
ssuser8698eb
 
the array, which stores a fixed-size sequential collection of elements of the...
Kavitha S
 
Java R20 - UNIT-3.docx
Pamarthi Kumar
 
Ad

More from Kuppusamy P (20)

PDF
Recurrent neural networks rnn
Kuppusamy P
 
PDF
Deep learning
Kuppusamy P
 
PDF
Image segmentation
Kuppusamy P
 
PDF
Image enhancement
Kuppusamy P
 
PDF
Feature detection and matching
Kuppusamy P
 
PDF
Image processing, Noise, Noise Removal filters
Kuppusamy P
 
PDF
Flowchart design for algorithms
Kuppusamy P
 
PDF
Algorithm basics
Kuppusamy P
 
PDF
Problem solving using Programming
Kuppusamy P
 
PDF
Parts of Computer, Hardware and Software
Kuppusamy P
 
PDF
Java methods or Subroutines or Functions
Kuppusamy P
 
PDF
Java iterative statements
Kuppusamy P
 
PDF
Java conditional statements
Kuppusamy P
 
PDF
Java data types
Kuppusamy P
 
PDF
Java introduction
Kuppusamy P
 
PDF
Logistic regression in Machine Learning
Kuppusamy P
 
PDF
Anomaly detection (Unsupervised Learning) in Machine Learning
Kuppusamy P
 
PDF
Machine Learning Performance metrics for classification
Kuppusamy P
 
PDF
Machine learning Introduction
Kuppusamy P
 
PDF
Reinforcement learning, Q-Learning
Kuppusamy P
 
Recurrent neural networks rnn
Kuppusamy P
 
Deep learning
Kuppusamy P
 
Image segmentation
Kuppusamy P
 
Image enhancement
Kuppusamy P
 
Feature detection and matching
Kuppusamy P
 
Image processing, Noise, Noise Removal filters
Kuppusamy P
 
Flowchart design for algorithms
Kuppusamy P
 
Algorithm basics
Kuppusamy P
 
Problem solving using Programming
Kuppusamy P
 
Parts of Computer, Hardware and Software
Kuppusamy P
 
Java methods or Subroutines or Functions
Kuppusamy P
 
Java iterative statements
Kuppusamy P
 
Java conditional statements
Kuppusamy P
 
Java data types
Kuppusamy P
 
Java introduction
Kuppusamy P
 
Logistic regression in Machine Learning
Kuppusamy P
 
Anomaly detection (Unsupervised Learning) in Machine Learning
Kuppusamy P
 
Machine Learning Performance metrics for classification
Kuppusamy P
 
Machine learning Introduction
Kuppusamy P
 
Reinforcement learning, Q-Learning
Kuppusamy P
 
Ad

Recently uploaded (20)

PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
Workbook de Inglés Completo - English Path.pdf
shityouenglishpath
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PPTX
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PDF
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
PPTX
Introduction to Indian Writing in English
Trushali Dodiya
 
PPTX
ENGlish 8 lesson presentation PowerPoint.pptx
marawehsvinetshe
 
PDF
epi editorial commitee meeting presentation
MIPLM
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
PPTX
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PPTX
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Workbook de Inglés Completo - English Path.pdf
shityouenglishpath
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
Introduction to Indian Writing in English
Trushali Dodiya
 
ENGlish 8 lesson presentation PowerPoint.pptx
marawehsvinetshe
 
epi editorial commitee meeting presentation
MIPLM
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
Controller Request and Response in Odoo18
Celine George
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 

Java arrays

  • 2. Access Specifiers / modifier in Java • Defines an access scope of the variable, methods and classes. • Can change the access level of fields, constructors, methods, and class by applying the access modifier on it. 1. Public: Accessible from any other class, methods or interface. 2. Private: Accessible within the class or method where it is defined. 3. Protected: Accessible in the same class or its child class or in all those classes which are defined in the same package. 4. Default: When none of the access specifiers is specified. Can be accessed by all classes which are defined in same package only. Dr. Kuppusamy P
  • 3. Access Specifiers / modifier in Java Dr. Kuppusamy P Access Modifier within class within package outside package by subclass only outside package Private Y N N N Default Y Y N N Protected Y Y Y N Public Y Y Y Y
  • 4. Arrays • Array is a container object that contains a finite number of values of a similar data type . • When an array is created • The length (size) of an array is fixed • Array elements are automatically initialized with the default value of their data type • Two types • One dimensional Array • Multi-dimensional Array • 2D Array, 3D Array Dr. Kuppusamy P
  • 5. One Dimensional Array • Syntax: • data-type[] variable = new data-type[array_length] • Eg. int[] a = new int[6]; //define integer array with size 6 Dr. Kuppusamy P
  • 6. Create and Initialize Array • Create and initialize array as below o int[] x = {10, 20, 30}; o int[] x = new int[] {10, 20, 30}; • Here the length of an array is determined by the number of values provided between { and }. • The built-in length property determines the size of any array • E.g. o int[] x = new int[10]; oint x_len = x.length; Dr. Kuppusamy P
  • 7. Example - Array public class ArrayEx { public static void main(String[] args) { int[] marks; // declares an array of integers marks = new int[5]; // allocates memory for 5integers marks[0] = 11; marks[4] = 22; System.out.println("Element at index 0: " + marks[0]); System.out.println("Element at index 1: " + marks[1]); System.out.println("Element at index 4: " + marks[4]); } } Dr. Kuppusamy P
  • 8. Array bounds and Array Resizing • Array index (subscripts) begins with 0 • Can't access an array element beyond the defined range. • Can't resize an array. • Can use the same reference variable to refer new array. • int x[] = new int[5]; • x= new int[10]; Dr. Kuppusamy P
  • 9. Copying Array • To copy array elements from one array to another array, use arraycopy static method from System class. Syntax: public static void arraycopy(Object s, int sIndex, Object d, int dIndex, int length) Ex: int source[] = {1, 2, 3, 4, 5, 6}; int dest[] = new int[10]; System.arraycopy(source, 0, dest, 0, source.length); Dr. Kuppusamy P
  • 10. Two-Dimensional Array • Two-dimensional array is a collection of 1-Dimensional arrays. • Initializing two-dimensional arrays: int[][] y = new int[2][4]; • The 1st dimension represent rows or number of one dimension • The 2nd dimension represent columns or number of elements in the each one dimensions Jagged Array: • Column representation is optional in two dimensional arrays. int[][] y = new int[3][] • The curly braces { } may also be used to initialize two dimensional arrays. Eg: int[][] y = { {1,2,3}, {4,5,6}, {7,8,9} }; int[][] y = new int[3][] Dr. Kuppusamy P
  • 11. Two-Dimensional Array • Array can be initialized with the row dimension and without initializing the columns, but not vice versa • int[][] x = new int[3][]; • int[][] x = new int[][3]; //error • The length of the columns can vary for each row and initialize number of columns in each row Ex1: • int [][] numbers = new int [3][]; • numbers[0] = new int[] {6,9,3,7}; //row1 • numbers[1] = new int [] {5,2} // row2 • numbers[2] = new int [] {8,4,1}; // row3 Dr. Kuppusamy P
  • 12. Example: 2D-Array public class TwoD { public static void main(String[] args) { int [][] x = new int[3][]; // initialize number of rows x[0] = new int[3]; // define number of columns in each row x[1] = new int[2]; x[2] = new int[5]; for(int i=0; i < x.length; i++) // print array elements { for (int j=0; j < x[i].length; j++) { x[i][j] = i; System.out.print(x[i][j]+ ” “); } System.out.println(); } } } Dr. Kuppusamy P
  • 13. References Dr. Kuppusamy P Herbert Schildt, “Java: The Complete Reference”, McGraw-Hill Education, Tenth edition, 2017.