SlideShare une entreprise Scribd logo
1  sur  4
Télécharger pour lire hors ligne
Core Java

Debasish Pratihari

Array :



In Java arrays are objects that represent a
group of contiguous memory locations of the
same type and referred to by the same name.



The element type for an array can be any
primitive data type or object.



All Java arrays are technically one-dimensional.
Two-dimensional arrays are arrays of arrays.



Declaring an array does not create an array
object or allocate space in memory; it creates a
variable with a reference to an array



Array variable declarations must indicate a
dimension by using []



All arrays are zero-based



Arrays must be indexed by int values or byte,
short or char values (as these can be promoted
to int)



Using a long index value to access an array
causes a compile error



Attempting to access an array with an index less
than 0 or greater than the length of the array
causes an ArrayIndexOutOfBoundsException
to be thrown at runtime



Since arrays are Objects they can be initialized
using the new operator



When created, arrays are automatically
initialized with the default value of their type



Array references declared as members are
initialized to null BUT array references declared
in methods are not initialized

Lecture/core/array/06

Page #1

feel the Technology…
Core Java

Debasish Pratihari

Examples of valid array declarations :



String[]s;



String s[];



String[][]s;



String s[][];



String[] s[];

Initializing Array :












String[ ] s = new String[100]; // default values: null
boolean[] b = new boolean[4]; // default values: false
int i[ ]= new int[5];
int[][] i = new int[10][10];
// default values: 0
String[] oneDimArray = { "abc","def","xyz" };
int[] array = null;
int[][] twoDimArray = { {1,2,3}, {4,5,6}, {7,8,9} };
int[] a = { 1, primes[2], c, (int)Math.pow(2,2) };
int[] arr = new int[] {1,2,3};
int[][] myArray = new int[5][];
int arrSize = 100;
String[] myArray = new String[arrSize];

Illegal Initialization of Array :






String[5] s;
int[] array = new int[];
int[][] myArray = new int[][5];
String[] s;
s = { "Lakshya", "debasish", "java"};

Lecture/core/array/06

Page #2

feel the Technology…
Core Java

Debasish Pratihari

Finding length of an Array :



Arrays have a length data field (read only),
specifying the number of elements in the array.



The length data field can be accessed through
the dot operator.



The bounds of an array are integers between 0
and length – 1



Example
for ( int k=0; k < a.length ; k++ )
System.out.println( a[ k ] );

Multidimensional Arrays:


Multidimensional arrays are implemented as
arrays of arrays. Multidimensional arrays are
declared by appending the appropriate number
of bracket pairs after the array name.



The number of elements in each direction need
not be specified in the declaration.



The number of elements in each direction need
not be equal.

Example of irregular Array :

int[][] irregular = { { 1 },
{ 2, 3 },
{ 4, 5, 6, 7 },
{0}
};

Lecture/core/array/06

Page #3

feel the Technology…
Core Java

Debasish Pratihari

An Example -Triangle Array :
Output

int[][] triangle = new int[10][];
for ( int j = 0; j<triangle.length ; j++ )
triangle[ j ] = new int[ j + 1 ];
for ( int i = 0 ; i < triangle.length ; i++ )
{
for ( int j = 0 ; j < triangle[i].length ; j++ )
System.out.print( triangle[i][j] + " " );
System.out.println();
}

0
00
000
0000
00000
000000
0000000
00000000
000000000
0000000000

Coping Array :
25% 



Arrays can be copied using the Java System
method arraycopy():
Signature:public static native void
arraycopy(Object src, int src_position, Object
dst, int dst_position, int length)
Parameters:
src - the source array.
src_position - start position (first cell to copy) in the
source array.
dst - the destination array.
dst_position - start position in the destination array.
length - the number of array elements to be copied.



By default, all Java arrays support the clone
method.

Things to Remember :


An array is a container object that holds a
fixed number of values of a single type.



The length of an array is established when
the array is created. After creation, its
length is fixed.



Each item in an array is called an element



each element is accessed by its numerical
index.

Lecture/core/array/06

Page #4

feel the Technology…

Contenu connexe

Tendances

Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
Princess Sam
 
Csharp4 arrays and_tuples
Csharp4 arrays and_tuplesCsharp4 arrays and_tuples
Csharp4 arrays and_tuples
Abed Bukhari
 

Tendances (20)

Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
 
array
array array
array
 
Array in C# 3.5
Array in C# 3.5Array in C# 3.5
Array in C# 3.5
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
C# Arrays
C# ArraysC# Arrays
C# Arrays
 
Arrays in Java | Edureka
Arrays in Java | EdurekaArrays in Java | Edureka
Arrays in Java | Edureka
 
Arrays C#
Arrays C#Arrays C#
Arrays C#
 
An Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysAn Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: Arrays
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Array&amp;string
Array&amp;stringArray&amp;string
Array&amp;string
 
7array in c#
7array in c#7array in c#
7array in c#
 
Two-dimensional array in java
Two-dimensional array in javaTwo-dimensional array in java
Two-dimensional array in java
 
Array and Collections in c#
Array and Collections in c#Array and Collections in c#
Array and Collections in c#
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Csharp4 arrays and_tuples
Csharp4 arrays and_tuplesCsharp4 arrays and_tuples
Csharp4 arrays and_tuples
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | Edureka
 

En vedette

香港六合彩
香港六合彩香港六合彩
香港六合彩
wejia
 
Paragraphs and topic sentences
Paragraphs and topic sentencesParagraphs and topic sentences
Paragraphs and topic sentences
Sussan Roo
 
クラウド
クラウドクラウド
クラウド
yuu_2003
 
Learning pool community rewards
Learning pool community rewardsLearning pool community rewards
Learning pool community rewards
Paul McElvaney
 

En vedette (20)

Carol Judge, Warwickshire County Council
Carol Judge, Warwickshire County CouncilCarol Judge, Warwickshire County Council
Carol Judge, Warwickshire County Council
 
Learning Pool's Elaine Walton & Dave Briggs on 'E-learning for Councillors an...
Learning Pool's Elaine Walton & Dave Briggs on 'E-learning for Councillors an...Learning Pool's Elaine Walton & Dave Briggs on 'E-learning for Councillors an...
Learning Pool's Elaine Walton & Dave Briggs on 'E-learning for Councillors an...
 
Reed business preso
Reed business presoReed business preso
Reed business preso
 
香港六合彩
香港六合彩香港六合彩
香港六合彩
 
Reviewing Screen-Based Content
Reviewing Screen-Based ContentReviewing Screen-Based Content
Reviewing Screen-Based Content
 
Nilga Feb 2009
Nilga Feb 2009Nilga Feb 2009
Nilga Feb 2009
 
Paragraphs and topic sentences
Paragraphs and topic sentencesParagraphs and topic sentences
Paragraphs and topic sentences
 
عرض ملتقى النهائي جديد
عرض ملتقى النهائي جديدعرض ملتقى النهائي جديد
عرض ملتقى النهائي جديد
 
Lezing abc
Lezing abcLezing abc
Lezing abc
 
クラウド
クラウドクラウド
クラウド
 
Systems engineering leidraad se gww door ms
Systems engineering leidraad se gww door msSystems engineering leidraad se gww door ms
Systems engineering leidraad se gww door ms
 
Scotweb Presentation
Scotweb PresentationScotweb Presentation
Scotweb Presentation
 
Learning Pool Social Care Webinar Presentation
Learning Pool Social Care Webinar PresentationLearning Pool Social Care Webinar Presentation
Learning Pool Social Care Webinar Presentation
 
Worksheet
WorksheetWorksheet
Worksheet
 
Learning pool community rewards
Learning pool community rewardsLearning pool community rewards
Learning pool community rewards
 
Microcamp Milano
Microcamp MilanoMicrocamp Milano
Microcamp Milano
 
Online Public Relations
Online Public RelationsOnline Public Relations
Online Public Relations
 
MISIÓN INTERCULTURALIDAD - 1E-A
MISIÓN INTERCULTURALIDAD - 1E-AMISIÓN INTERCULTURALIDAD - 1E-A
MISIÓN INTERCULTURALIDAD - 1E-A
 
Lecture 16
Lecture 16Lecture 16
Lecture 16
 
Adding A Link To A Flick’R Photo
Adding A Link To A Flick’R PhotoAdding A Link To A Flick’R Photo
Adding A Link To A Flick’R Photo
 

Similaire à Lecture 6

arrays in c# including Classes handling arrays
arrays in c#  including Classes handling arraysarrays in c#  including Classes handling arrays
arrays in c# including Classes handling arrays
JayanthiM19
 
6 arrays injava
6 arrays injava6 arrays injava
6 arrays injava
irdginfo
 

Similaire à Lecture 6 (20)

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
 
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
 
Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................
 
arrays in c# including Classes handling arrays
arrays in c#  including Classes handling arraysarrays in c#  including Classes handling arrays
arrays in c# including Classes handling arrays
 
Lecture 7 arrays
Lecture   7 arraysLecture   7 arrays
Lecture 7 arrays
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
Arrays
ArraysArrays
Arrays
 
CH1 ARRAY (1).pptx
CH1 ARRAY (1).pptxCH1 ARRAY (1).pptx
CH1 ARRAY (1).pptx
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 
2 arrays
2   arrays2   arrays
2 arrays
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and Maps
 
Module 7 : Arrays
Module 7 : ArraysModule 7 : Arrays
Module 7 : Arrays
 
Arrays in java.pptx
Arrays in java.pptxArrays in java.pptx
Arrays in java.pptx
 
6 arrays injava
6 arrays injava6 arrays injava
6 arrays injava
 
javaArrays.pptx
javaArrays.pptxjavaArrays.pptx
javaArrays.pptx
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 
Arrays are used to store multiple values in a single variable, instead of dec...
Arrays are used to store multiple values in a single variable, instead of dec...Arrays are used to store multiple values in a single variable, instead of dec...
Arrays are used to store multiple values in a single variable, instead of dec...
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptx
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 

Plus de Debasish Pratihari (19)

Lecture 24
Lecture 24Lecture 24
Lecture 24
 
Lecture 23
Lecture 23Lecture 23
Lecture 23
 
Lecture 22
Lecture 22Lecture 22
Lecture 22
 
Lecture 21
Lecture 21Lecture 21
Lecture 21
 
Lecture 20
Lecture 20Lecture 20
Lecture 20
 
Lecture 19
Lecture 19Lecture 19
Lecture 19
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
 
Lecture 17
Lecture 17Lecture 17
Lecture 17
 
Lecture 14
Lecture 14Lecture 14
Lecture 14
 
Lecture 10
Lecture 10Lecture 10
Lecture 10
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Lecture 8
Lecture 8Lecture 8
Lecture 8
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Lecture25
Lecture25Lecture25
Lecture25
 

Dernier

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Dernier (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

Lecture 6

  • 1. Core Java Debasish Pratihari Array :  In Java arrays are objects that represent a group of contiguous memory locations of the same type and referred to by the same name.  The element type for an array can be any primitive data type or object.  All Java arrays are technically one-dimensional. Two-dimensional arrays are arrays of arrays.  Declaring an array does not create an array object or allocate space in memory; it creates a variable with a reference to an array  Array variable declarations must indicate a dimension by using []  All arrays are zero-based  Arrays must be indexed by int values or byte, short or char values (as these can be promoted to int)  Using a long index value to access an array causes a compile error  Attempting to access an array with an index less than 0 or greater than the length of the array causes an ArrayIndexOutOfBoundsException to be thrown at runtime  Since arrays are Objects they can be initialized using the new operator  When created, arrays are automatically initialized with the default value of their type  Array references declared as members are initialized to null BUT array references declared in methods are not initialized Lecture/core/array/06 Page #1 feel the Technology…
  • 2. Core Java Debasish Pratihari Examples of valid array declarations :  String[]s;  String s[];  String[][]s;  String s[][];  String[] s[]; Initializing Array :            String[ ] s = new String[100]; // default values: null boolean[] b = new boolean[4]; // default values: false int i[ ]= new int[5]; int[][] i = new int[10][10]; // default values: 0 String[] oneDimArray = { "abc","def","xyz" }; int[] array = null; int[][] twoDimArray = { {1,2,3}, {4,5,6}, {7,8,9} }; int[] a = { 1, primes[2], c, (int)Math.pow(2,2) }; int[] arr = new int[] {1,2,3}; int[][] myArray = new int[5][]; int arrSize = 100; String[] myArray = new String[arrSize]; Illegal Initialization of Array :     String[5] s; int[] array = new int[]; int[][] myArray = new int[][5]; String[] s; s = { "Lakshya", "debasish", "java"}; Lecture/core/array/06 Page #2 feel the Technology…
  • 3. Core Java Debasish Pratihari Finding length of an Array :  Arrays have a length data field (read only), specifying the number of elements in the array.  The length data field can be accessed through the dot operator.  The bounds of an array are integers between 0 and length – 1  Example for ( int k=0; k < a.length ; k++ ) System.out.println( a[ k ] ); Multidimensional Arrays:  Multidimensional arrays are implemented as arrays of arrays. Multidimensional arrays are declared by appending the appropriate number of bracket pairs after the array name.  The number of elements in each direction need not be specified in the declaration.  The number of elements in each direction need not be equal. Example of irregular Array : int[][] irregular = { { 1 }, { 2, 3 }, { 4, 5, 6, 7 }, {0} }; Lecture/core/array/06 Page #3 feel the Technology…
  • 4. Core Java Debasish Pratihari An Example -Triangle Array : Output int[][] triangle = new int[10][]; for ( int j = 0; j<triangle.length ; j++ ) triangle[ j ] = new int[ j + 1 ]; for ( int i = 0 ; i < triangle.length ; i++ ) { for ( int j = 0 ; j < triangle[i].length ; j++ ) System.out.print( triangle[i][j] + " " ); System.out.println(); } 0 00 000 0000 00000 000000 0000000 00000000 000000000 0000000000 Coping Array : 25%    Arrays can be copied using the Java System method arraycopy(): Signature:public static native void arraycopy(Object src, int src_position, Object dst, int dst_position, int length) Parameters: src - the source array. src_position - start position (first cell to copy) in the source array. dst - the destination array. dst_position - start position in the destination array. length - the number of array elements to be copied.  By default, all Java arrays support the clone method. Things to Remember :  An array is a container object that holds a fixed number of values of a single type.  The length of an array is established when the array is created. After creation, its length is fixed.  Each item in an array is called an element  each element is accessed by its numerical index. Lecture/core/array/06 Page #4 feel the Technology…