Java Array Creation And Initialize
Chapter:
Miscellaneous
Last Updated:
13-02-2017 06:37:26 UTC
Program:
/* ............... START ............... */
public class JavaArrayCreationAndInitialization {
public static void main(String[] args) {
// Creating an array
int a[] = new int[5];
// Adding elements to array by accessing its index
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
a[4] = 5;
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
/* ............... END ............... */
Output
Notes:
-
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
- Each item in an array is called an element, and each element is accessed by its numerical index.
- Advantage of Java Array is below.
- Code Optimization: It makes the code optimized, we can retrieve or sort the data easily.
- Random access: We can get any data located at any index position.
- Disadvantage of Java Array is below.
- Size Limit: We can store only fixed size of elements in the array. It doesn't grow its size at runtime. To solve this problem, collection framework is used in java.
Tags
Array Creation And Initialize, Java, Miscellaneous