Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Java Array

Uploaded by

Dhanunjay Rao
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Java Array

Uploaded by

Dhanunjay Rao
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Java Array

Array is a collection of similar type of


elements that have contiguous
memory location.
• Java array is an object the contains elements
of similar data type. It is a data structure
where we store similar elements. We can
store only fixed set of elements in a java array.
• Array in java is index based, first element of
the array is stored at 0 index.
-Advantage of Java Array
• Code Optimization: It makes the code
optimized, we can retrieve or sort the data
easily.
• Random access: We can get any data located
at any index position.
Disadvantage of Java Array

• Size Limit: We can store only fixed size of


elements in the array. It doesn't grow its size
at runtime. To solve this problem, collection
framework is used in java.
Types of Array in java

• There are two types of array.


-Single Dimensional Array
-Multidimensional Array
Single Dimensional Array in java

• Syntax to Declare an Array in java


dataType[] arr; (or) // Ex: int[] sno;
dataType []arr; (or) // Ex: int []sno;
dataType arr[]; // Ex: int sno[];
• Syntax to Create Array in java
Array-Name=new dataType[size];
Ex: sno = new int[size];
• Syntax:
dataType Array-Name[] = new dataType[size];
Ex: int sno[]=new int[5];
Declaration, Instantiation and
Initialization of Java Array
We can declare, instantiate and initialize the java array together by:

int a[]={33,3,4,5};//declaration, instantiation and initialization


Let's see the simple example to print this array.
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]);
}
}
Multidimensional array in java
-Array contain one index that is 1D Array
-Array contain two indexes that is 2D Array
-Array contain three indexes that is 3D Array
.
.
-Array contain n indexes that is N-D Array
• Syntax to Declare 2D Array in java
dataType[][] arrayName; //Ex: int[][] subject;
dataType [][]arrayNaem; //Ex: int [][]subject;
dataType arrayRefVar[][]; //Ex: int subject[][];
dataType []arrayName[]; //Ex: int[] subject [];

int[][] arr=new int[m][n];//m row and n column


int arr[]={10,20,30,40,50};
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};

You might also like