Array in Java
Transcript of Array in Java
ArrayBY ASHAH HOUSE OF SOFTWARE
ASHAH HOUSE OF SOFTWARE
OutlineWhat is Array ?
Array Declaration
Array Allocation
Array Initialization
Two dimensional Array
Array Types( Symmetrical & Asymmetrical)
For each loop
ASHAH HOUSE OF SOFTWARE
What is Array?•An array is a container object . That holds a fixed number of values of a single type.
•Array is complex type of variable
•Array is also object.
•It is data structure.
•Contain a fixed number of values of same type .
•Each item in an array is called an element
•Element can be accessed through index number
ASHAH HOUSE OF SOFTWARE
Array Declaration
•an array declaration has two components:• Array type
• Array name
•Array can be declared as:• int arr[];
• int []arr;
• int[] arr; //preferred way
• int is a data type of array “arr” is the name of Array “[]” is for index.
•Brackets [] are special symbols indicating that this variable holds an array.
ASHAH HOUSE OF SOFTWARE
Array Allocation
•Array Allocation means size of array.
•Array can be allocated as:• arr[]=new int[10];
•Array is fixed can not be resize after allocation.
•New allocate memory for Array.
•Length Property:• it gives the length/size of the array.
• Syntax: • arrayName.length
ASHAH HOUSE OF SOFTWARE
Array Initialization
•Array can be initialized by individual index• arr[0]=1;
• arr[1]=2;
•Array can be declare , allocate and initialize in single statement as• int arr[]=new int[]{1,2,3,4,5,6,7,8,9,10};
• Int arr[]={1,2,3,4,5,6,7,9,10};
ASHAH HOUSE OF SOFTWARE
Two Dimensional Array•Array can be more than one dimension
• Two dimensional array or we can say Array into Array or Nested Array
•Two Dimensional Array can be Declare as:• int arr[][];• int[][] arr;• int [][]arr;
•Allocate as:• int arr[][]=new arr[2][3];• Int arr[][]=new arr[2][];
•Initialized as:• arr[0][0]=1;• arr[0][1]=2;• arr[1][0]=3;• arr[1][1]=4;
ASHAH HOUSE OF SOFTWARE
Two Dimensional Array Cont…•Two Dimensional Array can be declare, allocate and initialized in one statement.• int arr[][]=new int[][]{ {1,2} , {3,4} };
• int arr[][]={ {1,2} , {3,4} };
ASHAH HOUSE OF SOFTWARE
Array Types( Symmetrical & Asymmetrical)
•Array can be Symmetrical or Asymmetrical.
•Symmetrical Array:• All nested array have same size
• As : int arr[][]=new int[2][3];
•Asymmetrical Array:• Nested array can be vary in size
• As: int arr[][]=new int[2][];
• Not need to give size to nested array.
ASHAH HOUSE OF SOFTWARE
For Each loop•Also known as enhanced for loop.
•Foreach is also loop specially used for array.
•Enables to traverse the complete array sequentially without using an index variable.
•Syntax:Int arr[]={1,,2,3,4,5};
For(int v : arr ){
System.out.println(v);
}
ASHAH HOUSE OF SOFTWARE
Copy Array•The System class have “arraycopy” method.
•Syntax:• Public static void arraycopy(object srcArr, int startingSrc,object destArr, int startingDest, int length)
•Calling:• System.arraycopy(srcArr,5,destArr,0,5);
•For copy an array first you have to declare and allocate array.
ASHAH HOUSE OF SOFTWARE
Array Class•The java.util.Arrays class contains various static methods.
•Sorting:• public static void sort(Object[] a)• Sort into ascending
•Searching:• public static int binarySearch(Object[] a, Object key)• Return: index
•Comparing:• public static boolean equals(long[] a, long[] a2)• Return : true
•Filling:• public static void fill(int[] a, int val)• Assign value to specified.
•Note: These methods are overloaded for all primitive types.
ASHAH HOUSE OF SOFTWARE