SlideShare uma empresa Scribd logo
1 de 11
Baixar para ler offline
Array: Multi dimensional Array
You can also declare an array of arrays (also known as a multidimensional array) by using
two or more sets of brackets, such as String[ ][ ] names. Each element, therefore, must be
accessed by a corresponding number of index values.
In the Java programming language, a multidimensional array is an array whose components
are themselves arrays. This is unlike arrays in C or Fortran. A consequence of this is that the
rows are allowed to vary in length
Example of multi dimensional array is excel sheet:
It has 3 rows and 3 columns. This type of arrays are known as Multi Dimensional Array
Declaring a Variable to Refer to a Multi-dimensional array:
datatype[ ][ ] arrayName; or
datatype [ ][ ]arrayName; or
datatype arrayName[ ][ ];
For example to declare an array of integer variables:
int[ ][ ] a; or
int [ ][ ]a; or
int a[ ][ ]; this form is discouraged, please don’t use this
always use the first form i.e. datatype[ ][ ] arrayName;
Similarly, you can declare arrays of other types:
byte[][] aMultidimensionalArrayOfBytes;
short[][] aMultidimensionalArrayOfShorts;
long[][] aMultidimensionalArrayOfLongs;
float[][] aMultidimensionalArrayOfFloats;
double[][] aMultidimensionalArrayOfDoubles;
boolean[][] aMultidimensionalArrayOfBooleans;
char[][] aMultidimensionalArrayOfChars;
String[][] aMultidimensionalArrayOfStrings;
One way to create a multi-dimensional array is with the new operator.
int[ ][ ] a; //declare a array variable
a=new int[3][4]; //create an array of 3 rows and 4 columns
we can also write:
a=new int[m][n]; //m and n must be initialised before
if we don’t use this statement then following error is printed:
Variable a may not have been initialized.
we can also combine the above two statements into one:
int[ ][ ] a=new int[3][4];
When we write this statement, a memory area for 12 integers is reserved. All elements are
assigned value zero.
Index
1000 2000 3000
0 1 2
3
length
Values
Memory Address: 5000(Suppose)
5000
a a points Memory Address: 5000(Suppose)
0 0 0 0
0 1 2 3
4
length
0 0 0 0
0 1 2 3
4
length
0 0 0 0
0 1 2 3
4
length
Memory Address: 1000(Suppose) Memory Address: 2000(Suppose) Memory Address: 3000(Suppose)
a.length = 3
a[0].length = 4 a[1].length = 4 a[2].length = 4
As we have defined array as :
int[ ][ ] a=new int[3][4];
So elements are accessed like:
To print all elements of array you can use the following loop:
for(int i=0;i<a.length;i++)
for(int j=0;j<a[i].length;j++)
System.out.print(a[ i ][ j ]+” “);
We can also initialise the array as:
int[ ][ ] a={ { 1,2,3} , {4, 5, 6} , {7, 8, 9} };
We can make rows of variable length as:
int[ ][ ] a={ { 1,2,3} , {4, 5, 6,7,8} , {9,10} };
this will initialise the array as:
a[0][0]=1 a[1][0]=4 a[1][3]=7 a[2][0]=9
a[0][1]=2 a[1][1]=5 a[1][4]=8 a[2][1]=10
a[0][2]=3 a[1][2]=6
a[0] a[1] a[2]
a[0][0] a[0][1] a[0][2]
We can also make rows of variable length as:
int[ ][ ] a=new int[3][ ];
a[0]=new int[ 3];
a[1]=new int[5];
a[2]=new int[2];
this will initialise the array as:
a[0][0]=0 a[1][0]=0 a[1][3]=0 a[2][0]=0
a[0][1]=0 a[1][1]=0 a[1][4]=0 a[2][1]=0
a[0][2]=0 a[1][2]=0
Output:
For more Visit:
http://gsb-programming.blogspot.in/search/label/Java

Mais conteúdo relacionado

Mais procurados (20)

Presentation on array
Presentation on array Presentation on array
Presentation on array
 
Arrays in c language
Arrays in c languageArrays in c language
Arrays in c language
 
array
array array
array
 
Array
ArrayArray
Array
 
Array in C# 3.5
Array in C# 3.5Array in C# 3.5
Array in C# 3.5
 
ARRAY
ARRAYARRAY
ARRAY
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
 
One dimensional arrays
One dimensional arraysOne dimensional arrays
One dimensional arrays
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
 
Md05 arrays
Md05 arraysMd05 arrays
Md05 arrays
 
Arrays
ArraysArrays
Arrays
 
Java arrays
Java arraysJava arrays
Java arrays
 
Arrays accessing using for loops
Arrays accessing using for loopsArrays accessing using for loops
Arrays accessing using for loops
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
Arrays
ArraysArrays
Arrays
 
Java arrays (1)
Java arrays (1)Java arrays (1)
Java arrays (1)
 
C++ lecture 04
C++ lecture 04C++ lecture 04
C++ lecture 04
 
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1
 
Array and string
Array and stringArray and string
Array and string
 
Computer programming 2 Lesson 13
Computer programming 2  Lesson 13Computer programming 2  Lesson 13
Computer programming 2 Lesson 13
 

Destaque

Destaque (13)

Stars
StarsStars
Stars
 
Learn Java Part 6
Learn Java Part 6Learn Java Part 6
Learn Java Part 6
 
Learn Java Part 11
Learn Java Part 11Learn Java Part 11
Learn Java Part 11
 
Learn Java Part 7
Learn Java Part 7Learn Java Part 7
Learn Java Part 7
 
Learn Java Part 4
Learn Java Part 4Learn Java Part 4
Learn Java Part 4
 
Learn Java Part 5
Learn Java Part 5Learn Java Part 5
Learn Java Part 5
 
Defing locations in Oracle Apps
Defing locations in Oracle AppsDefing locations in Oracle Apps
Defing locations in Oracle Apps
 
Learn Java Part 10
Learn Java Part 10Learn Java Part 10
Learn Java Part 10
 
Assigning role AME_BUS_ANALYST
Assigning role AME_BUS_ANALYSTAssigning role AME_BUS_ANALYST
Assigning role AME_BUS_ANALYST
 
Creating business group in oracle apps
Creating business group in oracle appsCreating business group in oracle apps
Creating business group in oracle apps
 
Learn Java Part 1
Learn Java Part 1Learn Java Part 1
Learn Java Part 1
 
Learn Java Part 2
Learn Java Part 2Learn Java Part 2
Learn Java Part 2
 
Introduction to Jquery
Introduction to JqueryIntroduction to Jquery
Introduction to Jquery
 

Semelhante a Learn Java Part 9 (20)

Arrays
ArraysArrays
Arrays
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
3.ArraysandPointers.pptx
3.ArraysandPointers.pptx3.ArraysandPointers.pptx
3.ArraysandPointers.pptx
 
Array and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdfArray and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdf
 
Arrays
ArraysArrays
Arrays
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Arrays
ArraysArrays
Arrays
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Array
ArrayArray
Array
 
Chapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdfChapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdf
 
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
 
Array lecture
Array lectureArray lecture
Array lecture
 
Arrays
ArraysArrays
Arrays
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
 
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...
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 

Mais de Gurpreet singh

Introduction to Oracle Fusion BIP Reporting
Introduction to Oracle Fusion BIP ReportingIntroduction to Oracle Fusion BIP Reporting
Introduction to Oracle Fusion BIP ReportingGurpreet singh
 
Understanding Flex Fields with Accounting Flexfields(Chart of Accounts) in O...
Understanding Flex Fields with  Accounting Flexfields(Chart of Accounts) in O...Understanding Flex Fields with  Accounting Flexfields(Chart of Accounts) in O...
Understanding Flex Fields with Accounting Flexfields(Chart of Accounts) in O...Gurpreet singh
 
Oracle Application Developmenr Framework
Oracle Application Developmenr FrameworkOracle Application Developmenr Framework
Oracle Application Developmenr FrameworkGurpreet singh
 
Oracle advanced queuing
Oracle advanced queuingOracle advanced queuing
Oracle advanced queuingGurpreet singh
 
Generics and collections in Java
Generics and collections in JavaGenerics and collections in Java
Generics and collections in JavaGurpreet singh
 
IO Streams, Serialization, de-serialization, autoboxing
IO Streams, Serialization, de-serialization, autoboxingIO Streams, Serialization, de-serialization, autoboxing
IO Streams, Serialization, de-serialization, autoboxingGurpreet singh
 
Introduction to Data Flow Diagram (DFD)
Introduction to Data Flow Diagram (DFD)Introduction to Data Flow Diagram (DFD)
Introduction to Data Flow Diagram (DFD)Gurpreet singh
 
Ingenium test(Exam Management System) Project Presentation (Full)
Ingenium test(Exam Management System) Project Presentation (Full)Ingenium test(Exam Management System) Project Presentation (Full)
Ingenium test(Exam Management System) Project Presentation (Full)Gurpreet singh
 
Computer Graphics Notes
Computer Graphics NotesComputer Graphics Notes
Computer Graphics NotesGurpreet singh
 

Mais de Gurpreet singh (20)

Introduction to Oracle Fusion BIP Reporting
Introduction to Oracle Fusion BIP ReportingIntroduction to Oracle Fusion BIP Reporting
Introduction to Oracle Fusion BIP Reporting
 
Why Messaging system?
Why Messaging system?Why Messaging system?
Why Messaging system?
 
Understanding Flex Fields with Accounting Flexfields(Chart of Accounts) in O...
Understanding Flex Fields with  Accounting Flexfields(Chart of Accounts) in O...Understanding Flex Fields with  Accounting Flexfields(Chart of Accounts) in O...
Understanding Flex Fields with Accounting Flexfields(Chart of Accounts) in O...
 
Oracle Application Developmenr Framework
Oracle Application Developmenr FrameworkOracle Application Developmenr Framework
Oracle Application Developmenr Framework
 
Java Servlet part 3
Java Servlet part 3Java Servlet part 3
Java Servlet part 3
 
Oracle advanced queuing
Oracle advanced queuingOracle advanced queuing
Oracle advanced queuing
 
Oracle SQL Part 3
Oracle SQL Part 3Oracle SQL Part 3
Oracle SQL Part 3
 
Oracle SQL Part 2
Oracle SQL Part 2Oracle SQL Part 2
Oracle SQL Part 2
 
Oracle SQL Part1
Oracle SQL Part1Oracle SQL Part1
Oracle SQL Part1
 
Generics and collections in Java
Generics and collections in JavaGenerics and collections in Java
Generics and collections in Java
 
IO Streams, Serialization, de-serialization, autoboxing
IO Streams, Serialization, de-serialization, autoboxingIO Streams, Serialization, de-serialization, autoboxing
IO Streams, Serialization, de-serialization, autoboxing
 
Java Servlets Part 2
Java Servlets Part 2Java Servlets Part 2
Java Servlets Part 2
 
PL/SQL Part 5
PL/SQL Part 5PL/SQL Part 5
PL/SQL Part 5
 
PL/SQL Part 3
PL/SQL Part 3PL/SQL Part 3
PL/SQL Part 3
 
PL/SQL Part 2
PL/SQL Part 2PL/SQL Part 2
PL/SQL Part 2
 
PL/SQL Part 1
PL/SQL Part 1PL/SQL Part 1
PL/SQL Part 1
 
Introduction to Data Flow Diagram (DFD)
Introduction to Data Flow Diagram (DFD)Introduction to Data Flow Diagram (DFD)
Introduction to Data Flow Diagram (DFD)
 
Ingenium test(Exam Management System) Project Presentation (Full)
Ingenium test(Exam Management System) Project Presentation (Full)Ingenium test(Exam Management System) Project Presentation (Full)
Ingenium test(Exam Management System) Project Presentation (Full)
 
Computer Graphics Notes
Computer Graphics NotesComputer Graphics Notes
Computer Graphics Notes
 
Learn Java Part 11
Learn Java Part 11Learn Java Part 11
Learn Java Part 11
 

Último

VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spaintimesproduction05
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLManishPatel169454
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 

Último (20)

VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 

Learn Java Part 9

  • 2. You can also declare an array of arrays (also known as a multidimensional array) by using two or more sets of brackets, such as String[ ][ ] names. Each element, therefore, must be accessed by a corresponding number of index values. In the Java programming language, a multidimensional array is an array whose components are themselves arrays. This is unlike arrays in C or Fortran. A consequence of this is that the rows are allowed to vary in length Example of multi dimensional array is excel sheet: It has 3 rows and 3 columns. This type of arrays are known as Multi Dimensional Array
  • 3. Declaring a Variable to Refer to a Multi-dimensional array: datatype[ ][ ] arrayName; or datatype [ ][ ]arrayName; or datatype arrayName[ ][ ]; For example to declare an array of integer variables: int[ ][ ] a; or int [ ][ ]a; or int a[ ][ ]; this form is discouraged, please don’t use this always use the first form i.e. datatype[ ][ ] arrayName; Similarly, you can declare arrays of other types: byte[][] aMultidimensionalArrayOfBytes; short[][] aMultidimensionalArrayOfShorts; long[][] aMultidimensionalArrayOfLongs; float[][] aMultidimensionalArrayOfFloats; double[][] aMultidimensionalArrayOfDoubles; boolean[][] aMultidimensionalArrayOfBooleans; char[][] aMultidimensionalArrayOfChars; String[][] aMultidimensionalArrayOfStrings;
  • 4. One way to create a multi-dimensional array is with the new operator. int[ ][ ] a; //declare a array variable a=new int[3][4]; //create an array of 3 rows and 4 columns we can also write: a=new int[m][n]; //m and n must be initialised before if we don’t use this statement then following error is printed: Variable a may not have been initialized. we can also combine the above two statements into one: int[ ][ ] a=new int[3][4]; When we write this statement, a memory area for 12 integers is reserved. All elements are assigned value zero.
  • 5. Index 1000 2000 3000 0 1 2 3 length Values Memory Address: 5000(Suppose) 5000 a a points Memory Address: 5000(Suppose) 0 0 0 0 0 1 2 3 4 length 0 0 0 0 0 1 2 3 4 length 0 0 0 0 0 1 2 3 4 length Memory Address: 1000(Suppose) Memory Address: 2000(Suppose) Memory Address: 3000(Suppose) a.length = 3 a[0].length = 4 a[1].length = 4 a[2].length = 4
  • 6. As we have defined array as : int[ ][ ] a=new int[3][4]; So elements are accessed like: To print all elements of array you can use the following loop: for(int i=0;i<a.length;i++) for(int j=0;j<a[i].length;j++) System.out.print(a[ i ][ j ]+” “);
  • 7. We can also initialise the array as: int[ ][ ] a={ { 1,2,3} , {4, 5, 6} , {7, 8, 9} }; We can make rows of variable length as: int[ ][ ] a={ { 1,2,3} , {4, 5, 6,7,8} , {9,10} }; this will initialise the array as: a[0][0]=1 a[1][0]=4 a[1][3]=7 a[2][0]=9 a[0][1]=2 a[1][1]=5 a[1][4]=8 a[2][1]=10 a[0][2]=3 a[1][2]=6 a[0] a[1] a[2] a[0][0] a[0][1] a[0][2]
  • 8. We can also make rows of variable length as: int[ ][ ] a=new int[3][ ]; a[0]=new int[ 3]; a[1]=new int[5]; a[2]=new int[2]; this will initialise the array as: a[0][0]=0 a[1][0]=0 a[1][3]=0 a[2][0]=0 a[0][1]=0 a[1][1]=0 a[1][4]=0 a[2][1]=0 a[0][2]=0 a[1][2]=0
  • 9.