As per Wiki "In computer science, an array data structure, or simply an array, is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key. An array is stored so that the position of each element can be computed from its index tuple by a mathematical formula.The simplest type of data structure is a linear array, also called one-dimensional array."
As per above image, the elements starting from 0-9 are called indices and elements are represented below each index.
Applications:
- Arrays are used to implement data structures, such as lists, Heap, Hash Tables, Stacks, Queues, Strings.
- Arrays are used to implement mathematical vectors or Matrices, and other kinds of rectangular tables. Many Databases, (Small and large), consist of (or include) One-dimensional arrays whose elements are treated as records.
- Arrays are Indexed in 3 ways
0(Zero-based Indexing): First element of arrays is indexed by 0.
Sample Code: (Iteration starting from 0 (Zero))
for(int i=0 ;i<100; i++){ //code }
1(One- based Indexing): First element of arrays is indexed by 1.
Sample Code: (Iteration starting from 1 (One))
for(int i=1 ;i<100; i++){ //code }
N(N-based Indexing): The base of index of an array can be chosen your wish. This type of indexing based of the requirement. Eg: In binary search algorithm, we change index, for every iteration in order to search an element in an array.
Types of Arrays: One- Dimensional, Two-Dimensional, Multi-Dimensional
One- Dimensional Array Declaration: (In java)
Arrays in java can be declared in 3 ways:
1. Syntax: datatype [] arrayName = new datatype[size];
Sample Code: int[] arr= new int [10];
2. Syntax: datatype [] arrayName = {value, value… N};
Sample Code: int[] arr= {1,2,3,4};
3. Syntax: datatype [] arrayName = new datatype[]{value, value};
Sample Code: int[] arr= new int []{1,2,3};
Two- Dimensional Array Declaration: (In java)
Arrays in java can be declared in following preferred ways
1. Syntax: datatype [][] arrayName = new datatype[][];
Sample Code: int[][] arr= new int[10][10];
2. Syntax: datatype [][] arrayName ={{value,value}, { value,value }};
Sample Code: int[][] arr= {{1,2}, {3,4}};
No comments:
Post a Comment