![]() |
![]() |
Java Interface | |
Java Class vs Interface | |
Java Multiple Inheritance | |
Java Nested Interface |
Java Packages | |
Java Built in packages | |
Java Package Access Specifiers |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions |
An array is a group of similar or related data items which is having same name for all elements.
Array is useful when we want to store elements of same type without wasting of memery. Without array it results in following frustration to programmer.
Array is required to overcome above problems and increase the effiency of the program
An array with single subscript is called as One Dimensional Array.
Declaration
type arrayName []; or type [] arrayName;
Creating an Array
arrayName = new type [size];
We can also create both of the statements into single statement.
Creation and Declaration in one statement
type arrayName [] = new type [size];
class ArrayDemo { public static void main(String args[]) { int Array[] = new int[6]; } }
Assigning value to Array
Storing values to array is known as initialization of arrays. Java creates array starting with subscript 0 as first index and store the values till arraySize - 1.
There are two methods to assign value
1. Initializing array using subscript
class OneDimen { public static void main(String args[]) { int Array[] = new int[6]; Array[0] = 10; Array[1] = 15; Array[2] = 32; Array[3] = 47; Array[4] = 59; Array[5] = 22; } }
2. Initializing at the time of declaration
class ArrayDimen { public static void main(String args[]) { int Array[] = {10,15,32,47,59,22}; } }
Array elements are always stored from starting index 0 and ends at arraySize - 1.
here in example element 10 is stored at location Array[0] and ended with location 5