SlideShare a Scribd company logo
Programming in Java
Lecture 10: Arrays
Array
• Definition:
An array is a group/collection of variables of the
same type that are referred to by a common name.
• Arrays of any type can be created and may have one
or more dimensions.
• A specific element in an array is accessed by its index
(subscript).
• Examples:
• Collection of numbers
• Collection of names
• Collection of suffixes
Array of numbers:
Array of names:
Array of suffixes:
Examples
10 23 863 8 229
ment tion ness ves
Sam Shanu Riya
One-Dimensional Arrays
• A one-dimensional array is a list of variables of same
type.
• The general form of a one-dimensional array
declaration is:
type var-name[ ]; array-var = new type[size];
or, type var-name[ ] = new type[size];
Example:
int num [] = new int [10];
Declaration of array variable:
data-type variable-name[];
eg. int marks[];
This will declare an array named ‘marks’ of type ‘int’. But no memory is
allocated to the array.
Allocation of memory:
variable-name = new data-type[size];
eg. marks = new int[5];
This will allocate memory of 5 integers to the array ‘marks’ and it can store
upto 5 integers in it. ‘new’ is a special operator that allocates memory.
Syntax
Accessing elements in the array:
• Specific element in the array is accessed by specifying
name of the array followed the index of the element.
• All array indexes in Java start at zero.
variable-name[index] = value;
Example:
marks[0] = 10;
This will assign the value 10 to the 1st
element in the array.
marks[2] = 863;;
This will assign the value 863 to the 3rd
element in the array.
STEP 1 : (Declaration)
int marks[];
marks  null
STEP 2: (Memory Allocation)
marks = new int[5];
marks 
marks[0] marks[1] marks[2] marks[3] marks[4]
STEP 3: (Accessing Elements)
marks[0] = 10;
marks 
marks[0] marks[1] marks[2] marks[3] marks[4]
Example
0 0 0 0 0
10 0 0 0 0
• Size of an array can’t be changed after the array is created.
• Default values:
– zero (0) for numeric data types,
– u0000 for chars and
– false for boolean types
• Length of an array can be obtained as:
array_ref_var.length
Example
class Demo_Array
{
public static void main(String args[])
{
int marks[];
marks = new int[3];
marks[0] = 10;
marks[1] = 35;
marks[2] = 84;
System.out.println(“Marks of 2nd
student=” + marks[1]);
}
}
• Arrays can store elements of the same data type. Hence an
int array CAN NOT store an element which is not an int.
• Though an element of a compatible type can be converted
to int and stored into the int array.
eg. marks[2] = (int) 22.5;
This will convert ‘22.5’ into the int part ‘22’ and store it into the 3rd
place
in the int array ‘marks’.
• Array indexes start from zero. Hence ‘marks[index]’ refers
to the (index+1)th
element in the array and ‘marks[size-1]’
refers to last element in the array.
Note
Array Initialization
1. data Type [] array_ref_var = {value0, value1, …, value n};
2. data Type [] array_ref_var;
array_ref_var = {value0, value1, …,value n};
3. data Type [] array_ref_var = new data Type [n+1];
array_ref_var [0] = value 0;
array_ref_var [1] = value 1;
…
array_ref_var [n] = value n;
Multi-Dimensional Array
• Multidimensional arrays are arrays of arrays.
• Two-Dimensional arrays are used to represent a table or a
matrix.
• Creating Two-Dimensional Array:
int twoD[][] = new int[4][5];
Conceptual View of 2-Dimensional Array
class TwoDimArr
{
public static void main(String args[])
{
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++)
{
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++)
{
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
• When we allocate memory for a multidimensional
array, we need to only specify the memory for the
first (leftmost) dimension.
int twoD[][] = new int[4][];
• The other dimensions can be assigned manually.
// Manually allocate differing size second dimensions.
class TwoDAgain {
public static void main(String args[]) {
int twoD[][] = new int[4][];
twoD[0] = new int[1];
twoD[1] = new int[2];
twoD[2] = new int[3];
twoD[3] = new int[4];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<i+1; j++)
{
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<i+1; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
Initializing Multi-Dimensional Array
class Matrix {
public static void main(String args[]) {
double m[][] = {
{ 0*0, 1*0, 2*0, 3*0 },
{ 0*1, 1*1, 2*1, 3*1 },
{ 0*2, 1*2, 2*2, 3*2 },
{ 0*3, 1*3, 2*3, 3*3 }
};
int i, j;
for(i=0; i<4; i++) {
for(j=0; j<4; j++)
System.out.print(m[i][j] + " ");
System.out.println();
}
}
}
Alternative Array Declaration
type[ ] var-name;
• Example:
char twod1[][] = new char[3][4];
char[][] twod2 = new char[3][4];
• This alternative declaration form offers convenience
when declaring several arrays at the same time.
• Example: int[] nums, nums2, nums3;
Array Cloning
• To actually create another array with its own values,
Java provides the clone()method.
• arr2 = arr1; (assignment)
is not equivalent to
arr2 = arr1.clone(); (cloning)
Example: ArrayCloning.java
Array as Argument
• Arrays can be passed as an argument to any method.
Example:
void show ( int []x) {}
public static void main(String arr[]) {}
void compareArray(int [] a, int [] b ){}
Array as a return Type
• Arrays can also be returned as the return type of any
method
Example:
public int [] Result (int roll_No, int marks )
Assignment for Practice
• WAP in which use a method to convert all the double
values of argumented array into int.
public int[] double_To_Int(double [] ar)
• WAP to create a jagged Array in which rows and
columns should be taken as input from user.
L10 array

More Related Content

What's hot

Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
sandhya yadav
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
eShikshak
 
Array in c#
Array in c#Array in c#
Array in c#
Prem Kumar Badri
 
Arrays in c
Arrays in cArrays in c
Arrays in c
Jeeva Nanthini
 
Core java day2
Core java day2Core java day2
Core java day2
Soham Sengupta
 
Array in C
Array in CArray in C
Array in C
Kamal Acharya
 
Row major and column major in 2 d
Row major and column major in 2 dRow major and column major in 2 d
Row major and column major in 2 d
nikhilarora2211
 
Arrays
ArraysArrays
Chap09
Chap09Chap09
Chap09
Terry Yoast
 
Array in Java
Array in JavaArray in Java
Array in Java
Ali shah
 
A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)
Imdadul Himu
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
eShikshak
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
Gagan Deep
 
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
Ain-ul-Moiz Khawaja
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
Nikhil Pandit
 
Multi-Dimensional Lists
Multi-Dimensional ListsMulti-Dimensional Lists
Multi-Dimensional Lists
primeteacher32
 
Arrays in java (signle dimensional array)
Arrays in java (signle dimensional array)Arrays in java (signle dimensional array)
Arrays in java (signle dimensional array)
Talha mahmood
 
Arrays
ArraysArrays
Arrays
ArraysArrays
Array in c
Array in cArray in c
Array in c
AnIsh Kumar
 

What's hot (20)

Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
 
Array in c#
Array in c#Array in c#
Array in c#
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Core java day2
Core java day2Core java day2
Core java day2
 
Array in C
Array in CArray in C
Array in C
 
Row major and column major in 2 d
Row major and column major in 2 dRow major and column major in 2 d
Row major and column major in 2 d
 
Arrays
ArraysArrays
Arrays
 
Chap09
Chap09Chap09
Chap09
 
Array in Java
Array in JavaArray in Java
Array in Java
 
A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
 
Multi-Dimensional Lists
Multi-Dimensional ListsMulti-Dimensional Lists
Multi-Dimensional Lists
 
Arrays in java (signle dimensional array)
Arrays in java (signle dimensional array)Arrays in java (signle dimensional array)
Arrays in java (signle dimensional array)
 
Arrays
ArraysArrays
Arrays
 
Arrays
ArraysArrays
Arrays
 
Array in c
Array in cArray in c
Array in c
 

Similar to L10 array

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
 
array
array array
javaArrays.pptx
javaArrays.pptxjavaArrays.pptx
javaArrays.pptx
AshishNayyar11
 
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
rohinitalekar1
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
Aseelhalees
 
Array
ArrayArray
Array
ArrayArray
Array
PRN USM
 
Arrays In General
Arrays In GeneralArrays In General
Arrays In General
martha leon
 
Java arrays
Java arraysJava arrays
Java arrays
BHUVIJAYAVELU
 
Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................
suchitrapoojari984
 
Array and Collections in c#
Array and Collections in c#Array and Collections in c#
Array and Collections in c#
Umar Farooq
 
C++ lecture 04
C++ lecture 04C++ lecture 04
C++ lecture 04
HNDE Labuduwa Galle
 
Lecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptxLecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptx
ShahinAhmed49
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
أحمد محمد
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
أحمد محمد
 
Arrays
ArraysArrays
Arrays
Neeru Mittal
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
Maulen Bale
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
AnisZahirahAzman
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
HEMAHEMS5
 

Similar to L10 array (20)

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
 
6 arrays injava
6 arrays injava6 arrays injava
6 arrays injava
 
array
array array
array
 
javaArrays.pptx
javaArrays.pptxjavaArrays.pptx
javaArrays.pptx
 
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
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
 
Array
ArrayArray
Array
 
Array
ArrayArray
Array
 
Arrays In General
Arrays In GeneralArrays In General
Arrays In General
 
Java arrays
Java arraysJava arrays
Java arrays
 
Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................
 
Array and Collections in c#
Array and Collections in c#Array and Collections in c#
Array and Collections in c#
 
C++ lecture 04
C++ lecture 04C++ lecture 04
C++ lecture 04
 
Lecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptxLecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptx
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
Arrays
ArraysArrays
Arrays
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
 

More from teach4uin

Controls
ControlsControls
Controls
teach4uin
 
validation
validationvalidation
validation
teach4uin
 
validation
validationvalidation
validation
teach4uin
 
Master pages
Master pagesMaster pages
Master pages
teach4uin
 
.Net framework
.Net framework.Net framework
.Net framework
teach4uin
 
Scripting languages
Scripting languagesScripting languages
Scripting languages
teach4uin
 
Css1
Css1Css1
Css1
teach4uin
 
Code model
Code modelCode model
Code model
teach4uin
 
Asp db
Asp dbAsp db
Asp db
teach4uin
 
State management
State managementState management
State management
teach4uin
 
security configuration
security configurationsecurity configuration
security configuration
teach4uin
 
static dynamic html tags
 static dynamic html tags static dynamic html tags
static dynamic html tags
teach4uin
 
static dynamic html tags
static dynamic html tagsstatic dynamic html tags
static dynamic html tags
teach4uin
 
New microsoft office power point presentation
New microsoft office power point presentationNew microsoft office power point presentation
New microsoft office power point presentation
teach4uin
 
.Net overview
.Net overview.Net overview
.Net overview
teach4uin
 
Stdlib functions lesson
Stdlib functions lessonStdlib functions lesson
Stdlib functions lesson
teach4uin
 
enums
enumsenums
enums
teach4uin
 
memory
memorymemory
memory
teach4uin
 
array
arrayarray
array
teach4uin
 
storage clas
storage classtorage clas
storage clas
teach4uin
 

More from teach4uin (20)

Controls
ControlsControls
Controls
 
validation
validationvalidation
validation
 
validation
validationvalidation
validation
 
Master pages
Master pagesMaster pages
Master pages
 
.Net framework
.Net framework.Net framework
.Net framework
 
Scripting languages
Scripting languagesScripting languages
Scripting languages
 
Css1
Css1Css1
Css1
 
Code model
Code modelCode model
Code model
 
Asp db
Asp dbAsp db
Asp db
 
State management
State managementState management
State management
 
security configuration
security configurationsecurity configuration
security configuration
 
static dynamic html tags
 static dynamic html tags static dynamic html tags
static dynamic html tags
 
static dynamic html tags
static dynamic html tagsstatic dynamic html tags
static dynamic html tags
 
New microsoft office power point presentation
New microsoft office power point presentationNew microsoft office power point presentation
New microsoft office power point presentation
 
.Net overview
.Net overview.Net overview
.Net overview
 
Stdlib functions lesson
Stdlib functions lessonStdlib functions lesson
Stdlib functions lesson
 
enums
enumsenums
enums
 
memory
memorymemory
memory
 
array
arrayarray
array
 
storage clas
storage classtorage clas
storage clas
 

Recently uploaded

Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
Alex Pruden
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
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 Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
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
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
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
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
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
 
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
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
ThomasParaiso2
 
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
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 

Recently uploaded (20)

Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
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 Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
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...
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
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
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
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
 
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
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
 
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
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 

L10 array

  • 2. Array • Definition: An array is a group/collection of variables of the same type that are referred to by a common name. • Arrays of any type can be created and may have one or more dimensions. • A specific element in an array is accessed by its index (subscript). • Examples: • Collection of numbers • Collection of names • Collection of suffixes
  • 3. Array of numbers: Array of names: Array of suffixes: Examples 10 23 863 8 229 ment tion ness ves Sam Shanu Riya
  • 4. One-Dimensional Arrays • A one-dimensional array is a list of variables of same type. • The general form of a one-dimensional array declaration is: type var-name[ ]; array-var = new type[size]; or, type var-name[ ] = new type[size]; Example: int num [] = new int [10];
  • 5. Declaration of array variable: data-type variable-name[]; eg. int marks[]; This will declare an array named ‘marks’ of type ‘int’. But no memory is allocated to the array. Allocation of memory: variable-name = new data-type[size]; eg. marks = new int[5]; This will allocate memory of 5 integers to the array ‘marks’ and it can store upto 5 integers in it. ‘new’ is a special operator that allocates memory. Syntax
  • 6. Accessing elements in the array: • Specific element in the array is accessed by specifying name of the array followed the index of the element. • All array indexes in Java start at zero. variable-name[index] = value; Example: marks[0] = 10; This will assign the value 10 to the 1st element in the array. marks[2] = 863;; This will assign the value 863 to the 3rd element in the array.
  • 7. STEP 1 : (Declaration) int marks[]; marks  null STEP 2: (Memory Allocation) marks = new int[5]; marks  marks[0] marks[1] marks[2] marks[3] marks[4] STEP 3: (Accessing Elements) marks[0] = 10; marks  marks[0] marks[1] marks[2] marks[3] marks[4] Example 0 0 0 0 0 10 0 0 0 0
  • 8. • Size of an array can’t be changed after the array is created. • Default values: – zero (0) for numeric data types, – u0000 for chars and – false for boolean types • Length of an array can be obtained as: array_ref_var.length
  • 9. Example class Demo_Array { public static void main(String args[]) { int marks[]; marks = new int[3]; marks[0] = 10; marks[1] = 35; marks[2] = 84; System.out.println(“Marks of 2nd student=” + marks[1]); } }
  • 10. • Arrays can store elements of the same data type. Hence an int array CAN NOT store an element which is not an int. • Though an element of a compatible type can be converted to int and stored into the int array. eg. marks[2] = (int) 22.5; This will convert ‘22.5’ into the int part ‘22’ and store it into the 3rd place in the int array ‘marks’. • Array indexes start from zero. Hence ‘marks[index]’ refers to the (index+1)th element in the array and ‘marks[size-1]’ refers to last element in the array. Note
  • 11. Array Initialization 1. data Type [] array_ref_var = {value0, value1, …, value n}; 2. data Type [] array_ref_var; array_ref_var = {value0, value1, …,value n}; 3. data Type [] array_ref_var = new data Type [n+1]; array_ref_var [0] = value 0; array_ref_var [1] = value 1; … array_ref_var [n] = value n;
  • 12. Multi-Dimensional Array • Multidimensional arrays are arrays of arrays. • Two-Dimensional arrays are used to represent a table or a matrix. • Creating Two-Dimensional Array: int twoD[][] = new int[4][5];
  • 13. Conceptual View of 2-Dimensional Array
  • 14. class TwoDimArr { public static void main(String args[]) { int twoD[][]= new int[4][5]; int i, j, k = 0; for(i=0; i<4; i++) for(j=0; j<5; j++) { twoD[i][j] = k; k++; } for(i=0; i<4; i++) { for(j=0; j<5; j++) System.out.print(twoD[i][j] + " "); System.out.println(); } } }
  • 15. • When we allocate memory for a multidimensional array, we need to only specify the memory for the first (leftmost) dimension. int twoD[][] = new int[4][]; • The other dimensions can be assigned manually.
  • 16. // Manually allocate differing size second dimensions. class TwoDAgain { public static void main(String args[]) { int twoD[][] = new int[4][]; twoD[0] = new int[1]; twoD[1] = new int[2]; twoD[2] = new int[3]; twoD[3] = new int[4]; int i, j, k = 0; for(i=0; i<4; i++) for(j=0; j<i+1; j++) { twoD[i][j] = k; k++; } for(i=0; i<4; i++) { for(j=0; j<i+1; j++) System.out.print(twoD[i][j] + " "); System.out.println(); } } }
  • 17. Initializing Multi-Dimensional Array class Matrix { public static void main(String args[]) { double m[][] = { { 0*0, 1*0, 2*0, 3*0 }, { 0*1, 1*1, 2*1, 3*1 }, { 0*2, 1*2, 2*2, 3*2 }, { 0*3, 1*3, 2*3, 3*3 } }; int i, j; for(i=0; i<4; i++) { for(j=0; j<4; j++) System.out.print(m[i][j] + " "); System.out.println(); } } }
  • 18. Alternative Array Declaration type[ ] var-name; • Example: char twod1[][] = new char[3][4]; char[][] twod2 = new char[3][4]; • This alternative declaration form offers convenience when declaring several arrays at the same time. • Example: int[] nums, nums2, nums3;
  • 19. Array Cloning • To actually create another array with its own values, Java provides the clone()method. • arr2 = arr1; (assignment) is not equivalent to arr2 = arr1.clone(); (cloning) Example: ArrayCloning.java
  • 20. Array as Argument • Arrays can be passed as an argument to any method. Example: void show ( int []x) {} public static void main(String arr[]) {} void compareArray(int [] a, int [] b ){}
  • 21. Array as a return Type • Arrays can also be returned as the return type of any method Example: public int [] Result (int roll_No, int marks )
  • 22. Assignment for Practice • WAP in which use a method to convert all the double values of argumented array into int. public int[] double_To_Int(double [] ar) • WAP to create a jagged Array in which rows and columns should be taken as input from user.

Editor's Notes

  1. int i = 7; int j = 2; int k = 4; v[0] = 1; // element 0 of v given value 1 v[i] = 5; // element i of v given value 5 v[j] = v[i] + 3; // element j of v given value // of element i of v plus 3 v[j+1] = v[i] + v[0]; // element j+1 of v given value // of element i of v plus // value of element 0 of v v[v[j]] = 12; // element v[j] of v given // value 12 System.out.println(v[2]); // element 2 of v is displayed v[k] = Integer.parseInt(stdin.readLine()); // element k of v given next // extracted value
  2. int i = 7; int j = 2; int k = 4; v[0] = 1; // element 0 of v given value 1 v[i] = 5; // element i of v given value 5 v[j] = v[i] + 3; // element j of v given value // of element i of v plus 3 v[j+1] = v[i] + v[0]; // element j+1 of v given value // of element i of v plus // value of element 0 of v v[v[j]] = 12; // element v[j] of v given // value 12 System.out.println(v[2]); // element 2 of v is displayed v[k] = Integer.parseInt(stdin.readLine()); // element k of v given next // extracted value
  3. int i = 7; int j = 2; int k = 4; v[0] = 1; // element 0 of v given value 1 v[i] = 5; // element i of v given value 5 v[j] = v[i] + 3; // element j of v given value // of element i of v plus 3 v[j+1] = v[i] + v[0]; // element j+1 of v given value // of element i of v plus // value of element 0 of v v[v[j]] = 12; // element v[j] of v given // value 12 System.out.println(v[2]); // element 2 of v is displayed v[k] = Integer.parseInt(stdin.readLine()); // element k of v given next // extracted value
  4. int i = 7; int j = 2; int k = 4; v[0] = 1; // element 0 of v given value 1 v[i] = 5; // element i of v given value 5 v[j] = v[i] + 3; // element j of v given value // of element i of v plus 3 v[j+1] = v[i] + v[0]; // element j+1 of v given value // of element i of v plus // value of element 0 of v v[v[j]] = 12; // element v[j] of v given // value 12 System.out.println(v[2]); // element 2 of v is displayed v[k] = Integer.parseInt(stdin.readLine()); // element k of v given next // extracted value
  5. int i = 7; int j = 2; int k = 4; v[0] = 1; // element 0 of v given value 1 v[i] = 5; // element i of v given value 5 v[j] = v[i] + 3; // element j of v given value // of element i of v plus 3 v[j+1] = v[i] + v[0]; // element j+1 of v given value // of element i of v plus // value of element 0 of v v[v[j]] = 12; // element v[j] of v given // value 12 System.out.println(v[2]); // element 2 of v is displayed v[k] = Integer.parseInt(stdin.readLine()); // element k of v given next // extracted value
  6. int i = 7; int j = 2; int k = 4; v[0] = 1; // element 0 of v given value 1 v[i] = 5; // element i of v given value 5 v[j] = v[i] + 3; // element j of v given value // of element i of v plus 3 v[j+1] = v[i] + v[0]; // element j+1 of v given value // of element i of v plus // value of element 0 of v v[v[j]] = 12; // element v[j] of v given // value 12 System.out.println(v[2]); // element 2 of v is displayed v[k] = Integer.parseInt(stdin.readLine()); // element k of v given next // extracted value
  7. int i = 7; int j = 2; int k = 4; v[0] = 1; // element 0 of v given value 1 v[i] = 5; // element i of v given value 5 v[j] = v[i] + 3; // element j of v given value // of element i of v plus 3 v[j+1] = v[i] + v[0]; // element j+1 of v given value // of element i of v plus // value of element 0 of v v[v[j]] = 12; // element v[j] of v given // value 12 System.out.println(v[2]); // element 2 of v is displayed v[k] = Integer.parseInt(stdin.readLine()); // element k of v given next // extracted value
  8. int i = 7; int j = 2; int k = 4; v[0] = 1; // element 0 of v given value 1 v[i] = 5; // element i of v given value 5 v[j] = v[i] + 3; // element j of v given value // of element i of v plus 3 v[j+1] = v[i] + v[0]; // element j+1 of v given value // of element i of v plus // value of element 0 of v v[v[j]] = 12; // element v[j] of v given // value 12 System.out.println(v[2]); // element 2 of v is displayed v[k] = Integer.parseInt(stdin.readLine()); // element k of v given next // extracted value