ARRAY – Seminar – 1.1.0 in Java – Payilagam – Part – I This blog deals with ARRAY – Seminar – 1.1.0 .Some of them are discussed below.
Array
1.Array
2.Array Creation
3.Default Values Of Array Types
4. Array Types Based on Dimension
5.Pattern Program in Two Dimension Array
6.Understanding Pattern program Using java Debugging with Eclipse ++++++++++++++++++++++++++++++
1.Array
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.
An array of 10 elements. Each item in an array is called an element, and each element is accessed by its numerical index.
As shown in the preceding illustration, numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8.
2.Array Creation Syntax:
1.Declaring Array
type[ ] arrayname = new type[size] ;
type can be primitive type or Object class or sub class of Object class or user defined class.
2. Initializing Array arrayname[0 th index] = value; // Late initialization
.
.
arrayname[n th index] = value; // Late initialization
n th index=arraysize-1. n th index=arraylength-1
3.Creating Array with initialization
Syntax:
type[ ] arrayname ={value1,value2,….,value n};
type can be primitive type or Object class or sub class of Object class or user defined class.
The following program, ArrayDemo, creates an array of integers, puts some values in the array, and prints each value to standard output.
class ArrayDemo {
public static void main(String[] args) {
// declares an array of integers
int[] anArray;
// allocates memory for 10 integers
anArray = new int[10];
// initialize first element
anArray[0] = 100;
// initialize second element
anArray[1] = 200;
// and so forth
anArray[2] = 300;
anArray[3] = 400;
anArray[4] = 500;
anArray[5] = 600;
anArray[6] = 700;
anArray[7] = 800;
anArray[8] = 900;
anArray[9] = 1000;
System.out.println(“Element at index 0: “
+ anArray[0]);
System.out.println(“Element at index 1: “
+ anArray[1]);
System.out.println(“Element at index 2: “
+ anArray[2]);
System.out.println(“Element at index 3: “
+ anArray[3]);
System.out.println(“Element at index 4: “
+ anArray[4]);
System.out.println(“Element at index 5: “
+ anArray[5]);
System.out.println(“Element at index 6: “
+ anArray[6]);
System.out.println(“Element at index 7: “
+ anArray[7]);
System.out.println(“Element at index 8: “
+ anArray[8]);
System.out.println(“Element at index 9: “
+ anArray[9]);
}
}
The output from this program is:
Element at index 0: 100
Element at index 1: 200
Element at index 2: 300
Element at index 3: 400
Element at index 4: 500
Element at index 5: 600
Element at index 6: 700
Element at index 7: 800
Element at index 8: 900
Element at index 9: 1000