SlideShare a Scribd company logo
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…

More Related Content

PPTX
Arrays in java
PPT
Java Arrays
PPTX
Array lecture
PPT
Array in Java
PPTX
Arrays in java language
PPTX
Java arrays
PDF
intorduction to Arrays in java
Arrays in java
Java Arrays
Array lecture
Array in Java
Arrays in java language
Java arrays
intorduction to Arrays in java

What's hot (20)

PPT
Lec 25 - arrays-strings
PPTX
Array in C# 3.5
PPT
Java: Introduction to Arrays
PPTX
C# Arrays
PDF
Arrays in Java | Edureka
PPTX
PDF
An Introduction to Programming in Java: Arrays
PPTX
Arrays in Java
PDF
Java Arrays
PDF
Array&amp;string
PPTX
7array in c#
PPTX
Two-dimensional array in java
PDF
Array and Collections in c#
PPT
Arrays in C++
PPT
Csharp4 arrays and_tuples
PPT
C++ Arrays
PDF
Arrays in python
PPTX
Arrays in C language
PDF
Arrays In Python | Python Array Operations | Edureka
Lec 25 - arrays-strings
Array in C# 3.5
Java: Introduction to Arrays
C# Arrays
Arrays in Java | Edureka
An Introduction to Programming in Java: Arrays
Arrays in Java
Java Arrays
Array&amp;string
7array in c#
Two-dimensional array in java
Array and Collections in c#
Arrays in C++
Csharp4 arrays and_tuples
C++ Arrays
Arrays in python
Arrays in C language
Arrays In Python | Python Array Operations | Edureka
Ad

Viewers also liked (20)

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

Similar to Lecture 6 (20)

DOCX
Class notes(week 4) on arrays and strings
PDF
Class notes(week 4) on arrays and strings
PPTX
Unit-2.Arrays and Strings.pptx.................
PPTX
SessionPlans_f3efa1.6 Array in java.pptx
PPTX
Arrays in Java with example and types of array.pptx
PPTX
arrays in c# including Classes handling arrays
PPTX
Lecture 7 arrays
PPTX
6_Array.pptx
PPTX
CH1 ARRAY (1).pptx
DOCX
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
PPT
Arrays JavaScript presentacion en power point
PPT
Arrays in Java Programming Language slides
PPTX
array is a fundamental data structure that stores a collection of elements.pptx
PPT
ARRAYS.ppt
PPT
2 arrays
PPT
17-Arrays en java presentación documento
PPTX
07. Java Array, Set and Maps
PPT
ARRAYSinJavaProgramming_Introduction.ppt
PPTX
Module 7 : Arrays
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
Unit-2.Arrays and Strings.pptx.................
SessionPlans_f3efa1.6 Array in java.pptx
Arrays in Java with example and types of array.pptx
arrays in c# including Classes handling arrays
Lecture 7 arrays
6_Array.pptx
CH1 ARRAY (1).pptx
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
Arrays JavaScript presentacion en power point
Arrays in Java Programming Language slides
array is a fundamental data structure that stores a collection of elements.pptx
ARRAYS.ppt
2 arrays
17-Arrays en java presentación documento
07. Java Array, Set and Maps
ARRAYSinJavaProgramming_Introduction.ppt
Module 7 : Arrays

More from Debasish Pratihari (19)

PDF
Lecture 24
PDF
Lecture 23
PDF
Lecture 22
PDF
Lecture 21
PDF
Lecture 20
PDF
Lecture 19
PDF
Lecture 18
PDF
Lecture 17
PDF
Lecture 14
PDF
Lecture 10
PDF
PDF
PDF
PDF
PDF
PDF
PDF
PDF
PDF

Recently uploaded (20)

PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Big Data Technologies - Introduction.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Machine Learning_overview_presentation.pptx
PPT
Teaching material agriculture food technology
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Approach and Philosophy of On baking technology
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Encapsulation theory and applications.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
MYSQL Presentation for SQL database connectivity
Reach Out and Touch Someone: Haptics and Empathic Computing
Big Data Technologies - Introduction.pptx
Unlocking AI with Model Context Protocol (MCP)
Spectral efficient network and resource selection model in 5G networks
Group 1 Presentation -Planning and Decision Making .pptx
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Spectroscopy.pptx food analysis technology
Machine Learning_overview_presentation.pptx
Teaching material agriculture food technology
Advanced methodologies resolving dimensionality complications for autism neur...
Approach and Philosophy of On baking technology
SOPHOS-XG Firewall Administrator PPT.pptx
Assigned Numbers - 2025 - Bluetooth® Document
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Electronic commerce courselecture one. Pdf
Encapsulation theory and applications.pdf
A Presentation on Artificial Intelligence
Encapsulation_ Review paper, used for researhc scholars
MYSQL Presentation for SQL database connectivity

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…