Single Dimensional Array
public class Array {
public static void main(String[] args) {
String[] myArray; // declares an array of Strings
myArray = new String[10]; // allocates memory for 10 Strings
myArray[0] = "aa"; // initialize first element
myArray[1] = "bb"; // initialize second element
myArray[2] = "cc"; // initialize third element
System.out.println("Element at index 0 of myArray: "+ myArray[0]);
System.out.println("Element at index 1 of myArray: "+ myArray[1]);
System.out.println("Element at index 2 of myArray: "+ myArray[2]);
//or we can also initialize an array as
String[] newArray={"dd","ee","ff"};
System.out.println("Element at index 0 of newArray: "+ newArray[0]);
System.out.println("Element at index 1 of newArray: "+ newArray[1]);
System.out.println("Element at index 2 of newArray: "+ newArray[2]);
}
}
Output:
Element at index 0 of myArray: aa
Element at index 1 of myArray: bb
Element at index 2 of myArray: cc
Element at index 0 of newArray: dd
Element at index 1 of newArray: ee
Element at index 2 of newArray: ff