Array


Array

Array is one of the most concept in any programming language. It is used to store homogeneous elements. They are of fixed length. The index of the array variable starts with 0. Object is the superclass of the array type. Allocation of arrays is dynamic.
There are two types of arrays:

  • Single Dimension Array

Syntax:

data_type[] arrayname=new data_type[];

Example: int[] arr=new int[5];

  • Multi Dimension Array

Syntax:

data_type[][] arrayname=new data_type[][];

Example: int [][] arr=new int[2][3];

Example No.1:

Com.knowledge2life public class Main { public static void main(String[] args) { String[] Website = {"Knowledge2life1", "Knowledge2life2", "Knowledge2life3", "Knowledge2life4"}; System.out.println(Website[1]); } }

OUTPUT:

Knowledge2life2

Example No.2:

Com.knowledge2life public class Main { public static void main(String[] args) { String[] website = {"Knowledge2life1", "Knowledge2life2", "Knowledge2life3", "Knowledge2life4"}; website[0] = "Knowledge2life5"; System.out.println(website[0]); } }

OUTPUT:

Knowledge2life2