SlideShare a Scribd company logo
1 of 4
Download to read offline
3/18/2019 Arrays: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/arrays?module_item_id=21012844 1/4
Arrays
Array in java
Array is a collection of similar type of data. It is fixed in size means that you can't increase the size of
array at run time. It is a collection of homogeneous data elements. It stores the value on the basis of the
index value.
Advantage of Array
One variable can store multiple value: The main advantage of the array is we can represent multiple
value under the same name.
Code Optimization: No, need to declare a lot of variable of same type data, We can retrieve and sort
data easily.
Random access: We can retrieve any data from array with the help of the index value.
Disadvantage of Array
The main limitation of the array is Size Limit when once we declare array there is no chance to increase
and decrease the size of an array according to our requirement, Hence memory point of view array
concept is not recommended to use. To overcome this limitation in Java introduce the collection concept.
Types of Array
There are two types of array in Java.
Single Dimensional Array
Multidimensional Array
Array Declara on
Single dimension array declaration.
3/18/2019 Arrays: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/arrays?module_item_id=21012844 2/4
Syntax
1. int[] a;
2. int a[];
3. int []a;
Note: At the time of array declaration we cannot specify the size of the array. For Example int[5] a; this is
wrong.
2D Array declaration.
Syntax
1. int[][] a;
2. int a[][];
3. int [][]a;
4. int[] a[];
5. int[] []a;
6. int []a[];
Array crea on
Every array in a Java is an object, Hence we can create array by using new keyword.
Syntax
int[] arr = new int[10]; // The size of array is 10.
or
int[] arr = {10,20,30,40,50};
Accessing array elements
Access the elements of array by using index value of an elements.
Syntax
arrayname[n-1];
Access Array Elements
int[] arr={10,20,30,40};
System.out.println("Element at 4th place"+arr[2]);
3/18/2019 Arrays: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/arrays?module_item_id=21012844 3/4
Example of Array
public class ArrayEx
{
public static void main(String []args)
{
int arr[] = {10,20,30};
for (int i=0; i < arr.length; i++)
{
System.out.println(arr[i]);
}
}
}
Output
10
20
39
Note:
1) At the time of array creation we must be specify the size of array otherwise get an compile time error.
For Example
int[] a=new int[]; Invalid.
int[] a=new int[5]; Valid
2) If we specify the array size as negative int value, then we will get run-time error,
NegativeArraySizeException.
3) To specify the array size the allowed data types are byte, short, int, char If we use other data type then
we will get an Compile time error.
4) The maximum allowed size of array in Java is 2147483647 (It is the maximum value of int data type)
Difference Between Length and Length() in Java
length: It is a final variable and only applicable for array. It represent size of array.
Example
int[] a=new int[10];
System.out.println(a.length); // 10
System.out.println(a.length()); // Compile time error
3/18/2019 Arrays: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/arrays?module_item_id=21012844 4/4
length(): It is the final method applicable only for String objects. It represents the number of characters
present in the String.
Example
String s="Java";
System.out.println(s.length()); // 4
System.out.println(s.length); // Compile time error

More Related Content

What's hot

What's hot (20)

Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Hashing And Hashing Tables
Hashing And Hashing TablesHashing And Hashing Tables
Hashing And Hashing Tables
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Two-dimensional array in java
Two-dimensional array in javaTwo-dimensional array in java
Two-dimensional array in java
 
Array ppt
Array pptArray ppt
Array ppt
 
Hashing
HashingHashing
Hashing
 
Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Circular queue
Circular queueCircular queue
Circular queue
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
ITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in javaITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in java
 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++
 
arrays in c
arrays in carrays in c
arrays in c
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
String in java
String in javaString in java
String in java
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 

Similar to Arrays in java

Similar to Arrays in java (20)

Arrays in java.pptx
Arrays in java.pptxArrays in java.pptx
Arrays in java.pptx
 
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
 
arrays.docx
arrays.docxarrays.docx
arrays.docx
 
LectureNotes-05-DSA
LectureNotes-05-DSALectureNotes-05-DSA
LectureNotes-05-DSA
 
dizital pods session 6-arrays.ppsx
dizital pods session 6-arrays.ppsxdizital pods session 6-arrays.ppsx
dizital pods session 6-arrays.ppsx
 
dizital pods session 6-arrays.pptx
dizital pods session 6-arrays.pptxdizital pods session 6-arrays.pptx
dizital pods session 6-arrays.pptx
 
Array lecture
Array lectureArray lecture
Array lecture
 
Arrays
ArraysArrays
Arrays
 
Java Array String
Java Array StringJava Array String
Java Array String
 
OOPs with java
OOPs with javaOOPs with java
OOPs with java
 
Arrays in c v1 09102017
Arrays in c v1 09102017Arrays in c v1 09102017
Arrays in c v1 09102017
 
Arraysincv109102017 180831194256
Arraysincv109102017 180831194256Arraysincv109102017 180831194256
Arraysincv109102017 180831194256
 
Arrays in c language
Arrays in c languageArrays in c language
Arrays in c language
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Arrays
ArraysArrays
Arrays
 
Java R20 - UNIT-3.docx
Java R20 - UNIT-3.docxJava R20 - UNIT-3.docx
Java R20 - UNIT-3.docx
 
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
 
CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
 
Java arrays (1)
Java arrays (1)Java arrays (1)
Java arrays (1)
 

More from TharuniDiddekunta

More from TharuniDiddekunta (17)

String class
String classString class
String class
 
Exception handling basic
Exception handling basicException handling basic
Exception handling basic
 
Creating your own exception
Creating your own exceptionCreating your own exception
Creating your own exception
 
Built in exceptions
Built in exceptions Built in exceptions
Built in exceptions
 
Packages access protection, importing packages
Packages   access protection, importing packagesPackages   access protection, importing packages
Packages access protection, importing packages
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Inheritance used in java
Inheritance used in javaInheritance used in java
Inheritance used in java
 
Operators, control statements represented in java
Operators, control statements  represented in javaOperators, control statements  represented in java
Operators, control statements represented in java
 
Classes, objects, methods, constructors, this keyword in java
Classes, objects, methods, constructors, this keyword  in javaClasses, objects, methods, constructors, this keyword  in java
Classes, objects, methods, constructors, this keyword in java
 
Software Metrics (Testing)
Software Metrics (Testing)Software Metrics (Testing)
Software Metrics (Testing)
 
unit 3 Design 1
unit 3 Design 1unit 3 Design 1
unit 3 Design 1
 
Unit 4 testing
Unit 4 testingUnit 4 testing
Unit 4 testing
 
risk managment and quality
risk managment and qualityrisk managment and quality
risk managment and quality
 
Design
DesignDesign
Design
 
Network layer
Network layerNetwork layer
Network layer
 
Transport layer and Application layer
Transport layer and Application layerTransport layer and Application layer
Transport layer and Application layer
 
Congection control and Internet working
Congection control and Internet workingCongection control and Internet working
Congection control and Internet working
 

Recently uploaded

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 

Recently uploaded (20)

Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 

Arrays in java

  • 1. 3/18/2019 Arrays: Your Guided Course Template https://canvas.instructure.com/courses/1480238/pages/arrays?module_item_id=21012844 1/4 Arrays Array in java Array is a collection of similar type of data. It is fixed in size means that you can't increase the size of array at run time. It is a collection of homogeneous data elements. It stores the value on the basis of the index value. Advantage of Array One variable can store multiple value: The main advantage of the array is we can represent multiple value under the same name. Code Optimization: No, need to declare a lot of variable of same type data, We can retrieve and sort data easily. Random access: We can retrieve any data from array with the help of the index value. Disadvantage of Array The main limitation of the array is Size Limit when once we declare array there is no chance to increase and decrease the size of an array according to our requirement, Hence memory point of view array concept is not recommended to use. To overcome this limitation in Java introduce the collection concept. Types of Array There are two types of array in Java. Single Dimensional Array Multidimensional Array Array Declara on Single dimension array declaration.
  • 2. 3/18/2019 Arrays: Your Guided Course Template https://canvas.instructure.com/courses/1480238/pages/arrays?module_item_id=21012844 2/4 Syntax 1. int[] a; 2. int a[]; 3. int []a; Note: At the time of array declaration we cannot specify the size of the array. For Example int[5] a; this is wrong. 2D Array declaration. Syntax 1. int[][] a; 2. int a[][]; 3. int [][]a; 4. int[] a[]; 5. int[] []a; 6. int []a[]; Array crea on Every array in a Java is an object, Hence we can create array by using new keyword. Syntax int[] arr = new int[10]; // The size of array is 10. or int[] arr = {10,20,30,40,50}; Accessing array elements Access the elements of array by using index value of an elements. Syntax arrayname[n-1]; Access Array Elements int[] arr={10,20,30,40}; System.out.println("Element at 4th place"+arr[2]);
  • 3. 3/18/2019 Arrays: Your Guided Course Template https://canvas.instructure.com/courses/1480238/pages/arrays?module_item_id=21012844 3/4 Example of Array public class ArrayEx { public static void main(String []args) { int arr[] = {10,20,30}; for (int i=0; i < arr.length; i++) { System.out.println(arr[i]); } } } Output 10 20 39 Note: 1) At the time of array creation we must be specify the size of array otherwise get an compile time error. For Example int[] a=new int[]; Invalid. int[] a=new int[5]; Valid 2) If we specify the array size as negative int value, then we will get run-time error, NegativeArraySizeException. 3) To specify the array size the allowed data types are byte, short, int, char If we use other data type then we will get an Compile time error. 4) The maximum allowed size of array in Java is 2147483647 (It is the maximum value of int data type) Difference Between Length and Length() in Java length: It is a final variable and only applicable for array. It represent size of array. Example int[] a=new int[10]; System.out.println(a.length); // 10 System.out.println(a.length()); // Compile time error
  • 4. 3/18/2019 Arrays: Your Guided Course Template https://canvas.instructure.com/courses/1480238/pages/arrays?module_item_id=21012844 4/4 length(): It is the final method applicable only for String objects. It represents the number of characters present in the String. Example String s="Java"; System.out.println(s.length()); // 4 System.out.println(s.length); // Compile time error