Arrays
An array can be defined as a set of homogeneous data elements stored in contiguous memory locations.
Declaring an Array
- datatype arrayname[]; Eg: int a[];
- datatype[] arrayname; Eg: int[] a;
Creating an Array
- To create blank array
- arrayname = new datatype[size];
- Eg: ar[] = new int[5];
- To create filled array
- arrayname = {value1, value2, ...};
- Eg: ar = {3, 4, 5};
Array Initializer
An array initializer is a list of comma-separated values surrounded by braces.
Eg: {3, 4, 5, 2, 8}
Getting the size of the Array
We can get the size of the array using its length property.
Eg: int ar[] = new int[10];
int len = ar.length;
System.out.println(len);
Advantages of using Arrays
- We can define as many variables as we want in a single statement.
- We can access each element using the loop. This reduces the size of the program.
Disadvantages of using Arrays
- Once an array is created we cannot change its size.
0 Comments
Please don't enter any spam link in comment box.
Emoji