Array in Java

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Array in Java

 array is object type which is store similar type of element or similar data type of element
 array would store set of size of element
 it is a index based object

Advantages
 Code Optimization [we can retrieve the data and sort data]
 random access [according to index position]

Disadvantages
 it is size limitation

Types of Array in java [there are two types of array in java]


1. single Dimensional array
2. Multi Dimensional array

1.Single Dimensional array

Syntax to Declare an Array in Java


dataType[] arr; (or)
dataType []arr; (or)
dataType arr[];

Program :

import java.util.Scanner;

public class Practise


{
public static void main(String[] args) {
int patient[]=new int[5];//declaration and instantiation
patient[0]=10;//initialization
patient[1]=20;
patient[2]=70;
patient[3]=40;
patient[4]=50;

for (int i= 0; i<patient.length;i++)


{
System.out.println("i am print here"+patient[i]);
}
}
}

Way of instantiation in java array with concept clearing

 int[] array;
 int []array;
 int array[];

Way of declaration in java array with concept clearing


 = new int[4]
 = new int[]{34,3,42,4,425,5};
 = {34,23,34,35,34,534,5,45,34}

program for the direct declaration:

import java.util.Scanner;

public class Practise


{
public static void main(String[] args) {
int patient[] ={12,32,22};//declaration, instantiation and initialization
for (int i= 0; i<patient.length;i++)
{
System.out.println("i am print here"+patient[i]);
}
}

What is for each loop in java array :


 For each loop applied condition like mostly it is use for array the syntax given below
For each loop
syntax
for(data_type variable_name:name of array)
{
}
Expaination:
Data_type
This is the return type like integer , String , float , char, Boolean, so on …..
What is the Multi Dimensional Array in Java

You might also like