In this section, you'll learn what arrays are, and how to declare them.
Introduction:
So far, you have been working with variables that hold only one value. The integer variables you have set up have held only one number, and the string variables just one long string of text.
int number0 = 0,number1= 1,number2= 2,..........................;
String one = "One",two = "Two",.....................;
let's assume we have hundred's of int variables and we have been declared hundred's of variables in our program.If we think of readability and maintenance of code then how hard it will be ,because of this sun people introduced array concept.
Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables.
Example:
int number0 = 0,number1= 1,number2= 2,........int nightyNine = 99;
or
int[] numbers = new int[100];
numbers[0]=1;
numbers[1]=2;
numbers[99]=100;
An array is a flexible structure for storing a sequence of values all of the same type.
Array
A structure that holds multiple values of the same type.
The values stored in an array are called elements. The individual elements are
accessed using an integer index.
Index
An integer indicating the position of a value in a data structure.
Arrays follows Zero based indexing.
Zero-Based Indexing
A numbering scheme used throughout Java in which a sequence of values
is indexed starting with 0 (element 0, element 1, element 2, and so on).
Definition : Array- an array is an indexed collection of fixed number homogeneous data elements.
Limitations of Array:
Array Declarations:
1.single dimensional array declaration :
int[] a;
int []a;
int a[];
First one is recommended to use because name and type are cleary separated.
Note: At the time of declaration we can't specify the size of array, otherwise we will get compile time error.
int[6] a; //compile time error
int[] a;
2. Two dimensional array declaration:
Example:
int[][] a;
int [][]a;
int a[][]; All are valid.
int[] []a;
int[] a[];
int []a[];
3. Three dimensional array declaration:
Example:
int[][][] a;
int [][][]a;
int a[][][];
int[] [][]a;
int[] a[][]; All are valid.
int[] []a[];
int[][] []a;
int[][] a[];
int []a[][];
int [][]a[];
Which of the following declarations are valid?
1) int[] a1,b1;//a-1,b-1(valid) (I use the a-1 symbol to denote the single dimension,a-2 to denote two dimension)
2) int[] a2[],b2;//a-2,b-1(valid)
3) int[] []a3,b3;//a-2,b-2(valid)
4) int[] a,[]b;//C.E:<identifier> expected(invalid)
• If we want to specify the dimension before the variable that rule is applicable only for the 1st variable. Second variable onwards we can’t apply in the same declaration.
Introduction:
So far, you have been working with variables that hold only one value. The integer variables you have set up have held only one number, and the string variables just one long string of text.
int number0 = 0,number1= 1,number2= 2,..........................;
String one = "One",two = "Two",.....................;
let's assume we have hundred's of int variables and we have been declared hundred's of variables in our program.If we think of readability and maintenance of code then how hard it will be ,because of this sun people introduced array concept.
Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables.
Example:
int number0 = 0,number1= 1,number2= 2,........int nightyNine = 99;
or
int[] numbers = new int[100];
numbers[0]=1;
numbers[1]=2;
numbers[99]=100;
An array is a flexible structure for storing a sequence of values all of the same type.
Array
A structure that holds multiple values of the same type.
The values stored in an array are called elements. The individual elements are
accessed using an integer index.
Index
An integer indicating the position of a value in a data structure.
Arrays follows Zero based indexing.
Zero-Based Indexing
A numbering scheme used throughout Java in which a sequence of values
is indexed starting with 0 (element 0, element 1, element 2, and so on).
Definition : Array- an array is an indexed collection of fixed number homogeneous data elements.
Limitations of Array:
- Arrays can hold only homogeneous elements.
- Arrays are fixed in size. Once we created an array there is no chance of increasing or decreasing size based on our requirement.
- Hence to use array concept compulsory we should know size in advance which may not possible always.
- To over come this limitations sun people introduced Collection concept.
Array Declarations:
1.single dimensional array declaration :
int[] a;
int []a;
int a[];
First one is recommended to use because name and type are cleary separated.
Note: At the time of declaration we can't specify the size of array, otherwise we will get compile time error.
int[6] a; //compile time error
int[] a;
2. Two dimensional array declaration:
Example:
int[][] a;
int [][]a;
int a[][]; All are valid.
int[] []a;
int[] a[];
int []a[];
3. Three dimensional array declaration:
Example:
int[][][] a;
int [][][]a;
int a[][][];
int[] [][]a;
int[] a[][]; All are valid.
int[] []a[];
int[][] []a;
int[][] a[];
int []a[][];
int [][]a[];
Which of the following declarations are valid?
1) int[] a1,b1;//a-1,b-1(valid) (I use the a-1 symbol to denote the single dimension,a-2 to denote two dimension)
2) int[] a2[],b2;//a-2,b-1(valid)
3) int[] []a3,b3;//a-2,b-2(valid)
4) int[] a,[]b;//C.E:<identifier> expected(invalid)
• If we want to specify the dimension before the variable that rule is applicable only for the 1st variable. Second variable onwards we can’t apply in the same declaration.
No comments:
Post a Comment