SlideShare a Scribd company logo
1
Java Array
Handled By
Dr.T.Abirami
Associate Professor
Department of IT
Kongu Engineering College
Perundurai
Array
 used to store multiple values in a single
variable, instead of declaring separate
variables for each value.
 To declare an array, define the variable type
with square brackets []
 an array is a collection of similar types of
data.
 For example, if we want to store the names
of 100 people then we can create an array
of the string type that can store 100 names.
2
Types of Array in java
There are two types of array.
 Single Dimensional Array
 Multidimensional Array
3
Define an Array Variable in Java
Syntax:
datatype[] identifier; //preferred way
or
datatype identifier[];
Example:
char refVar[];
char[] refVar;
int[] refVar;
short[] refVar;
long[] refVar; 4
Initialize an array
 By using new operator array can be initialized
dataType[] arrayRefVar = new dataType[arraySize];
int[] age = new int[5]; //5 is the size of array.
int age[5]={22,25,30,32,35};
5
 String[] cars = {"Volvo", "BMW", "Ford",
"Mazda"};
 int[] myNum = {10, 20, 30, 40};
6
Access the Elements of an Array
 access an array element by referring to
the index number.
String[] cars = {"Volvo", "BMW", "Ford",
"Mazda"};
System.out.println(cars[0]);
// Outputs Volvo
7
Array Length
String[] cars = {"Volvo", "BMW", "Ford",
"Mazda"};
System.out.println(cars.length);
// Outputs 4
8
Loop Through an Array
 the array elements with the for loop, and
use the length property to specify how
many times the loop should run.
String[] cars = {"Volvo", "BMW", "Ford",
"Mazda"};
for (int i = 0; i < 4; i++)
{
System.out.println(cars[i]);
}
9
//Java Program to illustrate the use of declaration, instantiation
//and initialization of Java array in a single line
class Testarray1{
public static void main(String args[]){
int a[]={33,3,4,5};//declaration, instantiation and initialization
//printing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}}
10
Multidimensional Arrays
 containing one or more arrays.
 add each array within its own set of curly
braces:
dataType[][] arrayRefVar; (or)
dataType [][]arrayRefVar; (or)
dataType arrayRefVar[][]; (or)
dataType []arrayRefVar[];
11
int[][] arr=new int[3][3];
//3 row and 3 column
arr[0][0]=1;
arr[0][1]=2;
arr[0][2]=3;
arr[1][0]=4;
arr[1][1]=5;
arr[1][2]=6;
arr[2][0]=7;
arr[2][1]=8;
arr[2][2]=9;
12
class Testarray3{
public static void main(String args[]){
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}} 13
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
int x = myNumbers[1][2];
System.out.println(x); // Outputs 7
14
class Testarray5{
public static void main(String args[]){
//creating two matrices
int a[][]={{1,3,4},{3,4,5}};
int b[][]={{1,3,4},{3,4,5}};
//creating another matrix to store the sum of two matrices
int c[][]=new int[2][3];
//adding and printing addition of 2 matrices
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j];
System.out.print(c[i][j]+" ");
}
System.out.println();//new line
}
}}
15
addition of two matrices in Java
import java.util.Scanner;
class ExString
{
public static void main(String ar[])
{
int[] a=new int[2];
Scanner sc=new Scanner(System.in);
System.out.println("Enter the elements");
for(int i=0;i<2;i++)
{
a[i]=sc.nextInt();
}
System.out.println("Print the elements");
for(int i=0;i<2;i++)
{
System.out.println(a[i]);
}
}
}
16
import java.util.Scanner;
class ExString
{
public static void main(String ar[])
{
int[][] a=new int[2][2];
Scanner sc=new Scanner(System.in);
System.out.println("Enter the elements");
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Print the elements");
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
System.out.println(a[i][j]);
}
}
}
}
17
Jagged Array in Java
 arrays are of different size
 it is an array of arrays with different
number of columns.
Example
class Main
{
public static void main(String[] args)
{
// Declare a 2-D array with 3 rows
int myarray[][] = new int[3][];
// define and initialize jagged array
myarray[0] = new int[]{1,2,3};
myarray[1] = new int[]{4,5};
myarray[2] = new int[]{6,7,8,9,10};
// display the jagged array
System.out.println("Two dimensional Jagged Array:");
for (int i=0; i<myarray.length; i++)
{
for (int j=0; j<myarray[i].length; j++)
System.out.print(myarray[i][j] + " ");
System.out.println();
}
}
}
19
declaring a 2D array with different
columns
int arr[][] = new int[3][];
arr[0] = new int[3];
arr[1] = new int[4];
arr[2] = new int[2];
 int myarray[][] = new int[][]{
 new int[] { 1, 2, 3 };
 new int[] { 4, 5, 6, 7 };
 new int[] { 8, 9 };
 };
//initializing a jagged array
int count = 0;
for (int i=0; i<arr.length; i++)
for(int j=0; j<arr[i].length; j++)
arr[i][j] = count++;
differences between print() and
println().
22
Sr.
No.
Key print() println()
1
Implementation print method is implemented
as it prints the text on the
console and the cursor remains
at the end of the text at the
console.
On the other hand, println method
is implemented as prints the text
on the console and the cursor
remains at the start of the next line
at the console and the next
printing takes place from next
line.
2
Nature The prints method simply print
text on the console and does
not add any new line.
While println adds new line after
print text on console.
3
Arguments print method works only with
input parameter passed
otherwise in case no argument
is passed it throws syntax
exception.
println method works both with
and without parameter and do
not throw any type of exception.
import java.io.*;
class JavaTester {
public static void main(String[] args){
System.out.print("Hello");
System.out.print("World");
}
}
23
Output
HelloWorld
import java.io.*;
class JavaTester {
public static void main(String[] args)
{
System.out.println("Hello");
System.out.println("World");
}
}
24
Output
Hello
World

More Related Content

PPT
Java Arrays
PDF
intorduction to Arrays in java
PPTX
Java arrays
PPTX
Array in C# 3.5
PPT
Lec 25 - arrays-strings
PPTX
Array lecture
PPTX
Java arrays
Java Arrays
intorduction to Arrays in java
Java arrays
Array in C# 3.5
Lec 25 - arrays-strings
Array lecture
Java arrays

What's hot (20)

PPTX
Arrays in java
PPTX
Array in c#
PPTX
Arrays in java language
PDF
PDF
Arrays in Java | Edureka
PDF
Array and Collections in c#
PDF
Class notes(week 4) on arrays and strings
DOCX
Class notes(week 4) on arrays and strings
PPT
Array in Java
PDF
An Introduction to Programming in Java: Arrays
PPT
Csharp4 arrays and_tuples
PPT
Java: Introduction to Arrays
PPT
PPTX
Chap1 array
PPTX
Computer programming 2 Lesson 13
PDF
Java chapter 6 - Arrays -syntax and use
PPTX
Arrays in Java
PDF
Java Arrays
PPTX
Java arrays
PPTX
Arrays basics
Arrays in java
Array in c#
Arrays in java language
Arrays in Java | Edureka
Array and Collections in c#
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
Array in Java
An Introduction to Programming in Java: Arrays
Csharp4 arrays and_tuples
Java: Introduction to Arrays
Chap1 array
Computer programming 2 Lesson 13
Java chapter 6 - Arrays -syntax and use
Arrays in Java
Java Arrays
Java arrays
Arrays basics
Ad

Similar to Array (20)

PPTX
Arrays in Java with example and types of array.pptx
PPT
ARRAYS.ppt
PPT
Array and 2D Array and with syntax working
PPT
Arrays are used to store multiple values in a single variable, instead of dec...
PPT
ARRAYS.ppt
PPT
ARRAYS.ppt
PPT
ARRAYS.ppt
PPT
ARRAYS in java with in details presentation.ppt
PPT
ARRAYSinJavaProgramming_Introduction.ppt
PPTX
arrays in c# including Classes handling arrays
PPTX
SessionPlans_f3efa1.6 Array in java.pptx
PPT
Multi dimensional arrays
PPTX
Unit-2.Arrays and Strings.pptx.................
PPTX
Arrays in java.pptx
PPT
Fp201 unit4
PPT
L10 array
PPTX
6 arrays injava
PPSX
dizital pods session 6-arrays.ppsx
PPTX
dizital pods session 6-arrays.pptx
DOC
Arrays In General
Arrays in Java with example and types of array.pptx
ARRAYS.ppt
Array and 2D Array and with syntax working
Arrays are used to store multiple values in a single variable, instead of dec...
ARRAYS.ppt
ARRAYS.ppt
ARRAYS.ppt
ARRAYS in java with in details presentation.ppt
ARRAYSinJavaProgramming_Introduction.ppt
arrays in c# including Classes handling arrays
SessionPlans_f3efa1.6 Array in java.pptx
Multi dimensional arrays
Unit-2.Arrays and Strings.pptx.................
Arrays in java.pptx
Fp201 unit4
L10 array
6 arrays injava
dizital pods session 6-arrays.ppsx
dizital pods session 6-arrays.pptx
Arrays In General
Ad

More from Kongu Engineering College, Perundurai, Erode (20)

PPTX
Event Handling -_GET _ POSTimplementation.pptx
PPTX
Introduction to Generative AI refers to a subset of artificial intelligence
PPTX
Introduction to Microsoft Power BI is a business analytics service
PPTX
Connect to NoSQL Database (MongoDB) using Node JS & Connect Node.js with NoSQ...
PPTX
concept of server-side JavaScript / JS Framework: NODEJS
PPTX
Node.js web-based Example :Run a local server in order to start using node.js...
PPT
Concepts of Satellite Communication and types and its applications
PPT
Concepts of Mobile Communication Wireless LANs, Bluetooth , HiperLAN
PPTX
Web Technology Introduction framework.pptx
PPTX
Computer Network - Unicast Routing Distance vector Link state vector
PPT
Android SQLite database oriented application development
PPT
Android Application Development Programming
PPTX
Introduction to Spring & Spring BootFramework
PPTX
A REST API (also called a RESTful API or RESTful web API) is an application p...
PPTX
SOA and Monolith Architecture - Micro Services.pptx
PPTX
Connect to NoSQL Database using Node JS.pptx
PPTX
Event Handling -_GET _ POSTimplementation.pptx
Introduction to Generative AI refers to a subset of artificial intelligence
Introduction to Microsoft Power BI is a business analytics service
Connect to NoSQL Database (MongoDB) using Node JS & Connect Node.js with NoSQ...
concept of server-side JavaScript / JS Framework: NODEJS
Node.js web-based Example :Run a local server in order to start using node.js...
Concepts of Satellite Communication and types and its applications
Concepts of Mobile Communication Wireless LANs, Bluetooth , HiperLAN
Web Technology Introduction framework.pptx
Computer Network - Unicast Routing Distance vector Link state vector
Android SQLite database oriented application development
Android Application Development Programming
Introduction to Spring & Spring BootFramework
A REST API (also called a RESTful API or RESTful web API) is an application p...
SOA and Monolith Architecture - Micro Services.pptx
Connect to NoSQL Database using Node JS.pptx

Recently uploaded (20)

PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Geodesy 1.pptx...............................................
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Sustainable Sites - Green Building Construction
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
web development for engineering and engineering
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
OOP with Java - Java Introduction (Basics)
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Geodesy 1.pptx...............................................
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
additive manufacturing of ss316l using mig welding
Sustainable Sites - Green Building Construction
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
R24 SURVEYING LAB MANUAL for civil enggi
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
web development for engineering and engineering
Foundation to blockchain - A guide to Blockchain Tech
OOP with Java - Java Introduction (Basics)
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf

Array

  • 1. 1 Java Array Handled By Dr.T.Abirami Associate Professor Department of IT Kongu Engineering College Perundurai
  • 2. Array  used to store multiple values in a single variable, instead of declaring separate variables for each value.  To declare an array, define the variable type with square brackets []  an array is a collection of similar types of data.  For example, if we want to store the names of 100 people then we can create an array of the string type that can store 100 names. 2
  • 3. Types of Array in java There are two types of array.  Single Dimensional Array  Multidimensional Array 3
  • 4. Define an Array Variable in Java Syntax: datatype[] identifier; //preferred way or datatype identifier[]; Example: char refVar[]; char[] refVar; int[] refVar; short[] refVar; long[] refVar; 4
  • 5. Initialize an array  By using new operator array can be initialized dataType[] arrayRefVar = new dataType[arraySize]; int[] age = new int[5]; //5 is the size of array. int age[5]={22,25,30,32,35}; 5
  • 6.  String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};  int[] myNum = {10, 20, 30, 40}; 6
  • 7. Access the Elements of an Array  access an array element by referring to the index number. String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; System.out.println(cars[0]); // Outputs Volvo 7
  • 8. Array Length String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; System.out.println(cars.length); // Outputs 4 8
  • 9. Loop Through an Array  the array elements with the for loop, and use the length property to specify how many times the loop should run. String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; for (int i = 0; i < 4; i++) { System.out.println(cars[i]); } 9
  • 10. //Java Program to illustrate the use of declaration, instantiation //and initialization of Java array in a single line class Testarray1{ public static void main(String args[]){ int a[]={33,3,4,5};//declaration, instantiation and initialization //printing array for(int i=0;i<a.length;i++)//length is the property of array System.out.println(a[i]); }} 10
  • 11. Multidimensional Arrays  containing one or more arrays.  add each array within its own set of curly braces: dataType[][] arrayRefVar; (or) dataType [][]arrayRefVar; (or) dataType arrayRefVar[][]; (or) dataType []arrayRefVar[]; 11
  • 12. int[][] arr=new int[3][3]; //3 row and 3 column arr[0][0]=1; arr[0][1]=2; arr[0][2]=3; arr[1][0]=4; arr[1][1]=5; arr[1][2]=6; arr[2][0]=7; arr[2][1]=8; arr[2][2]=9; 12
  • 13. class Testarray3{ public static void main(String args[]){ //declaring and initializing 2D array int arr[][]={{1,2,3},{2,4,5},{4,4,5}}; //printing 2D array for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ System.out.print(arr[i][j]+" "); } System.out.println(); } }} 13
  • 14. int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} }; int x = myNumbers[1][2]; System.out.println(x); // Outputs 7 14
  • 15. class Testarray5{ public static void main(String args[]){ //creating two matrices int a[][]={{1,3,4},{3,4,5}}; int b[][]={{1,3,4},{3,4,5}}; //creating another matrix to store the sum of two matrices int c[][]=new int[2][3]; //adding and printing addition of 2 matrices for(int i=0;i<2;i++){ for(int j=0;j<3;j++){ c[i][j]=a[i][j]+b[i][j]; System.out.print(c[i][j]+" "); } System.out.println();//new line } }} 15 addition of two matrices in Java
  • 16. import java.util.Scanner; class ExString { public static void main(String ar[]) { int[] a=new int[2]; Scanner sc=new Scanner(System.in); System.out.println("Enter the elements"); for(int i=0;i<2;i++) { a[i]=sc.nextInt(); } System.out.println("Print the elements"); for(int i=0;i<2;i++) { System.out.println(a[i]); } } } 16
  • 17. import java.util.Scanner; class ExString { public static void main(String ar[]) { int[][] a=new int[2][2]; Scanner sc=new Scanner(System.in); System.out.println("Enter the elements"); for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { a[i][j]=sc.nextInt(); } } System.out.println("Print the elements"); for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { System.out.println(a[i][j]); } } } } 17
  • 18. Jagged Array in Java  arrays are of different size  it is an array of arrays with different number of columns.
  • 19. Example class Main { public static void main(String[] args) { // Declare a 2-D array with 3 rows int myarray[][] = new int[3][]; // define and initialize jagged array myarray[0] = new int[]{1,2,3}; myarray[1] = new int[]{4,5}; myarray[2] = new int[]{6,7,8,9,10}; // display the jagged array System.out.println("Two dimensional Jagged Array:"); for (int i=0; i<myarray.length; i++) { for (int j=0; j<myarray[i].length; j++) System.out.print(myarray[i][j] + " "); System.out.println(); } } } 19
  • 20. declaring a 2D array with different columns int arr[][] = new int[3][]; arr[0] = new int[3]; arr[1] = new int[4]; arr[2] = new int[2];  int myarray[][] = new int[][]{  new int[] { 1, 2, 3 };  new int[] { 4, 5, 6, 7 };  new int[] { 8, 9 };  };
  • 21. //initializing a jagged array int count = 0; for (int i=0; i<arr.length; i++) for(int j=0; j<arr[i].length; j++) arr[i][j] = count++;
  • 22. differences between print() and println(). 22 Sr. No. Key print() println() 1 Implementation print method is implemented as it prints the text on the console and the cursor remains at the end of the text at the console. On the other hand, println method is implemented as prints the text on the console and the cursor remains at the start of the next line at the console and the next printing takes place from next line. 2 Nature The prints method simply print text on the console and does not add any new line. While println adds new line after print text on console. 3 Arguments print method works only with input parameter passed otherwise in case no argument is passed it throws syntax exception. println method works both with and without parameter and do not throw any type of exception.
  • 23. import java.io.*; class JavaTester { public static void main(String[] args){ System.out.print("Hello"); System.out.print("World"); } } 23 Output HelloWorld
  • 24. import java.io.*; class JavaTester { public static void main(String[] args) { System.out.println("Hello"); System.out.println("World"); } } 24 Output Hello World