SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
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 (20)

ODP
Multithreading In Java
parag
 
PPSX
ADO.NET
Farzad Wadia
 
PPTX
Multiple inheritance possible in Java
Kurapati Vishwak
 
PPTX
linked list
Mohaimin Rahat
 
PDF
javascript objects
Vijay Kalyan
 
PDF
Queue as data_structure
eShikshak
 
PPTX
Chapter 3 servlet & jsp
Jafar Nesargi
 
PPT
ADO .Net
DrSonali Vyas
 
PPTX
Command line arguments
Ashok Raj
 
PPTX
Java package
CS_GDRCST
 
PPTX
Linked List
Ashim Lamichhane
 
PPT
Collection Framework in java
CPD INDIA
 
PPTX
Super Keyword in Java.pptx
KrutikaWankhade1
 
PPT
Working with color and font
myrajendra
 
PDF
Servlet and servlet life cycle
Dhruvin Nakrani
 
PPSX
Dr. Rajeshree Khande :Introduction to Java AWT
DrRajeshreeKhande
 
PPTX
Access modifiers in java
Madishetty Prathibha
 
PPTX
SQL Joins.pptx
Ankit Rai
 
PDF
STL in C++
Surya Prakash Sahu
 
Multithreading In Java
parag
 
ADO.NET
Farzad Wadia
 
Multiple inheritance possible in Java
Kurapati Vishwak
 
linked list
Mohaimin Rahat
 
javascript objects
Vijay Kalyan
 
Queue as data_structure
eShikshak
 
Chapter 3 servlet & jsp
Jafar Nesargi
 
ADO .Net
DrSonali Vyas
 
Command line arguments
Ashok Raj
 
Java package
CS_GDRCST
 
Linked List
Ashim Lamichhane
 
Collection Framework in java
CPD INDIA
 
Super Keyword in Java.pptx
KrutikaWankhade1
 
Working with color and font
myrajendra
 
Servlet and servlet life cycle
Dhruvin Nakrani
 
Dr. Rajeshree Khande :Introduction to Java AWT
DrRajeshreeKhande
 
Access modifiers in java
Madishetty Prathibha
 
SQL Joins.pptx
Ankit Rai
 
STL in C++
Surya Prakash Sahu
 

Similar to Arrays in java (20)

PPTX
OOPs with java
AAKANKSHA JAIN
 
PPTX
Array.pptx
ssuser8698eb
 
PPTX
ARRAYS.pptx
akila m
 
PDF
Arrays in Java
Naz Abdalla
 
PPTX
the array, which stores a fixed-size sequential collection of elements of the...
Kavitha S
 
PPTX
Arrays in java.pptx
Nagaraju Pamarthi
 
PPTX
Chapter6 (4) (1).pptx plog fix down more
mohammadalali41
 
PPT
17-Arrays en java presentación documento
DiegoGamboaSafla
 
PPTX
Arrays in programming
TaseerRao
 
DOCX
Java R20 - UNIT-3.docx
Pamarthi Kumar
 
PPT
Core java day2
Soham Sengupta
 
PPTX
6_Array.pptx
shafat6712
 
PPTX
Introduction-to-Arrays-in-Java . Exploring array
AbdulSamad264371
 
PPT
ch11.ppt
kavitamittal18
 
PPT
Arrays in java programming language slides
ssuser5d6130
 
PDF
Java chapter 6 - Arrays -syntax and use
Mukesh Tekwani
 
PPTX
javaArrays.pptx
AshishNayyar11
 
PPTX
Java Programming
Nanthini Kempaiyan
 
PPTX
Java Array String
Manish Tiwari
 
OOPs with java
AAKANKSHA JAIN
 
Array.pptx
ssuser8698eb
 
ARRAYS.pptx
akila m
 
Arrays in Java
Naz Abdalla
 
the array, which stores a fixed-size sequential collection of elements of the...
Kavitha S
 
Arrays in java.pptx
Nagaraju Pamarthi
 
Chapter6 (4) (1).pptx plog fix down more
mohammadalali41
 
17-Arrays en java presentación documento
DiegoGamboaSafla
 
Arrays in programming
TaseerRao
 
Java R20 - UNIT-3.docx
Pamarthi Kumar
 
Core java day2
Soham Sengupta
 
6_Array.pptx
shafat6712
 
Introduction-to-Arrays-in-Java . Exploring array
AbdulSamad264371
 
ch11.ppt
kavitamittal18
 
Arrays in java programming language slides
ssuser5d6130
 
Java chapter 6 - Arrays -syntax and use
Mukesh Tekwani
 
javaArrays.pptx
AshishNayyar11
 
Java Programming
Nanthini Kempaiyan
 
Java Array String
Manish Tiwari
 
Ad

More from TharuniDiddekunta (17)

PDF
String class
TharuniDiddekunta
 
PDF
Exception handling basic
TharuniDiddekunta
 
PDF
Creating your own exception
TharuniDiddekunta
 
PDF
Built in exceptions
TharuniDiddekunta
 
PDF
Packages access protection, importing packages
TharuniDiddekunta
 
PDF
Interfaces in java
TharuniDiddekunta
 
PDF
Inheritance used in java
TharuniDiddekunta
 
PDF
Operators, control statements represented in java
TharuniDiddekunta
 
PDF
Classes, objects, methods, constructors, this keyword in java
TharuniDiddekunta
 
PPTX
Software Metrics (Testing)
TharuniDiddekunta
 
PPTX
unit 3 Design 1
TharuniDiddekunta
 
PPTX
Unit 4 testing
TharuniDiddekunta
 
PPTX
risk managment and quality
TharuniDiddekunta
 
PPTX
Design
TharuniDiddekunta
 
PPT
Network layer
TharuniDiddekunta
 
PPTX
Transport layer and Application layer
TharuniDiddekunta
 
PPT
Congection control and Internet working
TharuniDiddekunta
 
String class
TharuniDiddekunta
 
Exception handling basic
TharuniDiddekunta
 
Creating your own exception
TharuniDiddekunta
 
Built in exceptions
TharuniDiddekunta
 
Packages access protection, importing packages
TharuniDiddekunta
 
Interfaces in java
TharuniDiddekunta
 
Inheritance used in java
TharuniDiddekunta
 
Operators, control statements represented in java
TharuniDiddekunta
 
Classes, objects, methods, constructors, this keyword in java
TharuniDiddekunta
 
Software Metrics (Testing)
TharuniDiddekunta
 
unit 3 Design 1
TharuniDiddekunta
 
Unit 4 testing
TharuniDiddekunta
 
risk managment and quality
TharuniDiddekunta
 
Network layer
TharuniDiddekunta
 
Transport layer and Application layer
TharuniDiddekunta
 
Congection control and Internet working
TharuniDiddekunta
 
Ad

Recently uploaded (20)

PDF
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
PDF
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
 
PDF
Our Guide to the July 2025 USPS® Rate Change
Postal Advocate Inc.
 
PDF
Quiz Night Live May 2025 - Intra Pragya Online General Quiz
Pragya - UEM Kolkata Quiz Club
 
PPTX
week 1-2.pptx yueojerjdeiwmwjsweuwikwswiewjrwiwkw
rebznelz
 
PPTX
Comparing Translational and Rotational Motion.pptx
AngeliqueTolentinoDe
 
PPTX
How to Configure Refusal of Applicants in Odoo 18 Recruitment
Celine George
 
PPT
21st Century Literature from the Philippines and the World QUARTER 1/ MODULE ...
isaacmendoza76
 
PPTX
Parsing HTML read and write operations and OS Module.pptx
Ramakrishna Reddy Bijjam
 
PPTX
The Gift of the Magi by O Henry-A Story of True Love, Sacrifice, and Selfless...
Beena E S
 
PPTX
How Physics Enhances Our Quality of Life.pptx
AngeliqueTolentinoDe
 
PDF
Andreas Schleicher_Teaching Compass_Education 2040.pdf
EduSkills OECD
 
PDF
Cooperative wireless communications 1st Edition Yan Zhang
jsphyftmkb123
 
PPTX
Connecting Linear and Angular Quantities in Human Movement.pptx
AngeliqueTolentinoDe
 
PPTX
ESP 10 Edukasyon sa Pagpapakatao PowerPoint Lessons Quarter 1.pptx
Sir J.
 
PPTX
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
PDF
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
PPTX
Practice Gardens and Polytechnic Education: Utilizing Nature in 1950s’ Hu...
Lajos Somogyvári
 
PPTX
PLANNING FOR EMERGENCY AND DISASTER MANAGEMENT ppt.pptx
PRADEEP ABOTHU
 
PPTX
SYMPATHOMIMETICS[ADRENERGIC AGONISTS] pptx
saip95568
 
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
 
Our Guide to the July 2025 USPS® Rate Change
Postal Advocate Inc.
 
Quiz Night Live May 2025 - Intra Pragya Online General Quiz
Pragya - UEM Kolkata Quiz Club
 
week 1-2.pptx yueojerjdeiwmwjsweuwikwswiewjrwiwkw
rebznelz
 
Comparing Translational and Rotational Motion.pptx
AngeliqueTolentinoDe
 
How to Configure Refusal of Applicants in Odoo 18 Recruitment
Celine George
 
21st Century Literature from the Philippines and the World QUARTER 1/ MODULE ...
isaacmendoza76
 
Parsing HTML read and write operations and OS Module.pptx
Ramakrishna Reddy Bijjam
 
The Gift of the Magi by O Henry-A Story of True Love, Sacrifice, and Selfless...
Beena E S
 
How Physics Enhances Our Quality of Life.pptx
AngeliqueTolentinoDe
 
Andreas Schleicher_Teaching Compass_Education 2040.pdf
EduSkills OECD
 
Cooperative wireless communications 1st Edition Yan Zhang
jsphyftmkb123
 
Connecting Linear and Angular Quantities in Human Movement.pptx
AngeliqueTolentinoDe
 
ESP 10 Edukasyon sa Pagpapakatao PowerPoint Lessons Quarter 1.pptx
Sir J.
 
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
Practice Gardens and Polytechnic Education: Utilizing Nature in 1950s’ Hu...
Lajos Somogyvári
 
PLANNING FOR EMERGENCY AND DISASTER MANAGEMENT ppt.pptx
PRADEEP ABOTHU
 
SYMPATHOMIMETICS[ADRENERGIC AGONISTS] pptx
saip95568
 

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