SlideShare a Scribd company logo
Arrays
 Array is a collection of similar type of elements that have contiguous
memory location.
 In java, array is an object the contains elements of similar data type.
 Array is index based, first element of the array is stored at 0 index.
 Types of Array
 Single Dimensional Array
 Multidimensional Array
Declaration of Arrays – 3 steps
 Declare the array
 Create storage area in primary memory.
 Put values into the array (i.e., Memory location)
Declaration of Arrays: 1 step
Form 1: Type arrayname[]
Form 2: Type [] arrayname;
 Creation of arrays: 2 step
arrayname = new type[size]; // create a memory
 Initialization of arrays:
arrayname [index/subscript] = value; or {list of values};
 Example:
int [] students = new int[7];
int [] students = {1,2,3,4,5};
 Two Dimensional Arrays:
 array with two subscript operator – [] []
datatype [] [] array_name=new datatype [row][column];
int [] [] student=new int[2][2];
int tableA[2][3] = {{10, 15, 30}, {14, 30, 33}};
int tableA[][] = {{10, 15, 30}, {14, 30, 33}};
4
Variable Size Arrays
 Java treats multidimensional arrays as “arrays of arrays”. It is
possible to declare a 2D arrays as follows:
– int a[][] = new int [3][];
– a[0]= new int [3];
– a[1]= new int [2];
– a[2]= new int [4];
import java.io.*;
class B{
public static void main(String args[]){
try{
//int[] a={1,2,3,4,5,6,7,8,9,10};
int a[]={1,2,3,4,5,6,7,8,9,10};//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]);
int b[]=new int[50];
DataInputStream dis=new DataInputStream(System.in);
System.out.println("Enter the no");
int c=Integer.parseInt(dis.readLine());
for(int j=0;j<c;j++)//length is the property of array
{b[j]=Integer.parseInt(dis.readLine());}
System.out.println("Entered Numbers are:");
for(int j=0;j<c;j++)//length is the property of array
{System.out.println(b[j]);}}
catch(Exception e)
{
}}}
import java.io.*;
class B1{
public static void main(String args[]){
try{
int arr[][]={{1,2,3},{2,4,5},{4,4,5}}; //declaring and initializing 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(); }
int [][] f=new int[10][10];
DataInputStream dis=new DataInputStream(System.in);
System.out.println("Enter the row and column values");
int c=Integer.parseInt(dis.readLine());
int d=Integer.parseInt(dis.readLine());
for(int i=0;i<c;i++){
for(int j=0;j<d;j++){
f[i][j]=Integer.parseInt(dis.readLine());}}
System.out.println("2D-array");
for(int i=0;i<c;i++){
for(int j=0;j<d;j++){
System.out.print(f[i][j]+ " " );}
System.out.println(); }}
catch(Exception e)
{}}}

More Related Content

What's hot

Event handling
Event handlingEvent handling
Event handling
swapnac12
 
Wrapper class
Wrapper classWrapper class
Wrapper class
kamal kotecha
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
Spotle.ai
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
imtiazalijoono
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
RahulAnanda1
 
Java Tokens
Java  TokensJava  Tokens
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
Spotle.ai
 
Java threads
Java threadsJava threads
Java threads
Prabhakaran V M
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
Hitesh Kumar
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
Elizabeth alexander
 
Method overloading
Method overloadingMethod overloading
Method overloading
Lovely Professional University
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
java token
java tokenjava token
java token
Jadavsejal
 
Applets
AppletsApplets
Array in Java
Array in JavaArray in Java
Array in Java
Shehrevar Davierwala
 
Java string handling
Java string handlingJava string handling
Java string handling
Salman Khan
 

What's hot (20)

Event handling
Event handlingEvent handling
Event handling
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java Tokens
Java  TokensJava  Tokens
Java Tokens
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Java input
Java inputJava input
Java input
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
 
Java threads
Java threadsJava threads
Java threads
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Java I/O
Java I/OJava I/O
Java I/O
 
java token
java tokenjava token
java token
 
Applets
AppletsApplets
Applets
 
Array in Java
Array in JavaArray in Java
Array in Java
 
Java string handling
Java string handlingJava string handling
Java string handling
 

Similar to Java arrays

L10 array
L10 arrayL10 array
L10 array
teach4uin
 
javaArrays.pptx
javaArrays.pptxjavaArrays.pptx
javaArrays.pptx
AshishNayyar11
 
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
Kuntal Bhowmick
 
Array
ArrayArray
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
Kuntal Bhowmick
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
HEMAHEMS5
 
dizital pods session 6-arrays.ppsx
dizital pods session 6-arrays.ppsxdizital pods session 6-arrays.ppsx
dizital pods session 6-arrays.ppsx
VijayKumarLokanadam
 
dizital pods session 6-arrays.pptx
dizital pods session 6-arrays.pptxdizital pods session 6-arrays.pptx
dizital pods session 6-arrays.pptx
VijayKumarLokanadam
 
Lecture 15 - Array
Lecture 15 - ArrayLecture 15 - Array
Lecture 15 - Array
Md. Imran Hossain Showrov
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
surajthakur474818
 
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
KirubelWondwoson1
 
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
 
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...
ssuser6478a8
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
coding9
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
ssuser99ca78
 
OOPs with java
OOPs with javaOOPs with java
OOPs with java
AAKANKSHA JAIN
 
Lecture 6
Lecture 6Lecture 6
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arraysAseelhalees
 

Similar to Java arrays (20)

Array
ArrayArray
Array
 
L10 array
L10 arrayL10 array
L10 array
 
javaArrays.pptx
javaArrays.pptxjavaArrays.pptx
javaArrays.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
 
Array
ArrayArray
Array
 
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
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
 
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
 
Lecture 15 - Array
Lecture 15 - ArrayLecture 15 - Array
Lecture 15 - Array
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 
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
 
Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................
 
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
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 
OOPs with java
OOPs with javaOOPs with java
OOPs with java
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
 

More from BHUVIJAYAVELU

Eprojectprojectfinalreportgsmmonitoringcontrollingofdevicesusinggsm 090811012...
Eprojectprojectfinalreportgsmmonitoringcontrollingofdevicesusinggsm 090811012...Eprojectprojectfinalreportgsmmonitoringcontrollingofdevicesusinggsm 090811012...
Eprojectprojectfinalreportgsmmonitoringcontrollingofdevicesusinggsm 090811012...BHUVIJAYAVELU
 
Java exception handling
Java exception handlingJava exception handling
Java exception handlingBHUVIJAYAVELU
 
Flow control and error control
Flow control and error controlFlow control and error control
Flow control and error controlBHUVIJAYAVELU
 
3 2--power-aware-cloud
3 2--power-aware-cloud3 2--power-aware-cloud
3 2--power-aware-cloudBHUVIJAYAVELU
 

More from BHUVIJAYAVELU (9)

Eprojectprojectfinalreportgsmmonitoringcontrollingofdevicesusinggsm 090811012...
Eprojectprojectfinalreportgsmmonitoringcontrollingofdevicesusinggsm 090811012...Eprojectprojectfinalreportgsmmonitoringcontrollingofdevicesusinggsm 090811012...
Eprojectprojectfinalreportgsmmonitoringcontrollingofdevicesusinggsm 090811012...
 
Lecture no1
Lecture no1Lecture no1
Lecture no1
 
Hybrid m-a-t
Hybrid m-a-tHybrid m-a-t
Hybrid m-a-t
 
Java interface
Java interfaceJava interface
Java interface
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Java packages
Java packagesJava packages
Java packages
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Flow control and error control
Flow control and error controlFlow control and error control
Flow control and error control
 
3 2--power-aware-cloud
3 2--power-aware-cloud3 2--power-aware-cloud
3 2--power-aware-cloud
 

Java arrays

  • 1. Arrays  Array is a collection of similar type of elements that have contiguous memory location.  In java, array is an object the contains elements of similar data type.  Array is index based, first element of the array is stored at 0 index.
  • 2.  Types of Array  Single Dimensional Array  Multidimensional Array Declaration of Arrays – 3 steps  Declare the array  Create storage area in primary memory.  Put values into the array (i.e., Memory location) Declaration of Arrays: 1 step Form 1: Type arrayname[] Form 2: Type [] arrayname;
  • 3.  Creation of arrays: 2 step arrayname = new type[size]; // create a memory  Initialization of arrays: arrayname [index/subscript] = value; or {list of values};  Example: int [] students = new int[7]; int [] students = {1,2,3,4,5};  Two Dimensional Arrays:  array with two subscript operator – [] [] datatype [] [] array_name=new datatype [row][column]; int [] [] student=new int[2][2]; int tableA[2][3] = {{10, 15, 30}, {14, 30, 33}}; int tableA[][] = {{10, 15, 30}, {14, 30, 33}};
  • 4. 4 Variable Size Arrays  Java treats multidimensional arrays as “arrays of arrays”. It is possible to declare a 2D arrays as follows: – int a[][] = new int [3][]; – a[0]= new int [3]; – a[1]= new int [2]; – a[2]= new int [4];
  • 5. import java.io.*; class B{ public static void main(String args[]){ try{ //int[] a={1,2,3,4,5,6,7,8,9,10}; int a[]={1,2,3,4,5,6,7,8,9,10};//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]); int b[]=new int[50]; DataInputStream dis=new DataInputStream(System.in); System.out.println("Enter the no"); int c=Integer.parseInt(dis.readLine()); for(int j=0;j<c;j++)//length is the property of array {b[j]=Integer.parseInt(dis.readLine());} System.out.println("Entered Numbers are:"); for(int j=0;j<c;j++)//length is the property of array {System.out.println(b[j]);}} catch(Exception e) { }}}
  • 6. import java.io.*; class B1{ public static void main(String args[]){ try{ int arr[][]={{1,2,3},{2,4,5},{4,4,5}}; //declaring and initializing 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(); } int [][] f=new int[10][10]; DataInputStream dis=new DataInputStream(System.in); System.out.println("Enter the row and column values"); int c=Integer.parseInt(dis.readLine()); int d=Integer.parseInt(dis.readLine()); for(int i=0;i<c;i++){ for(int j=0;j<d;j++){ f[i][j]=Integer.parseInt(dis.readLine());}} System.out.println("2D-array"); for(int i=0;i<c;i++){ for(int j=0;j<d;j++){ System.out.print(f[i][j]+ " " );} System.out.println(); }} catch(Exception e) {}}}