Aug 26, 2011

Arrays in Java

      Java provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
   Instead of declaring individual variables, such as student0, student2, ..., and student99, you declare one array variable such as students and use student s[0], student s[1], and ..., student s[99] to represent individual variables.

Declaring Arrays
              Arrays are declared with a data type, an array name, and square brackets [] specifying that you are declaring an array.
        datatype arrayName[]; OR datatype[] arrayName;
        int students[]; can also be declared as int[] students;
After an array is declared, an array object has to be assigned to it using the new keyword as well as the length of the array - specified in the square brackets. The length of the array denotes how many elements an array can hold.
        arrayName = new datatype[numElementsInArray];
students= new int[10];
The above example declares an array named students of data type int which has a length of 10, therefore it can store 10 elements.

Adding values to an Array
            Add values to an array by referring to the appropriate index of the array and assigning it a value.
       arrayName[index] = value;
       evenNumbers[3] = 20;
The above example will assign the value 20 to the 4th element in the evenNumbers array.
Array indexes begin at 0, so the first element of an array is at index 0, the second element of an array is at index 1, and so on.
As an alternative to declaring an array and then assigning values to its elements, you can do both tasks together.
       dataType[] arrayName = {value, value, value, value, etc.};
       char[] vowels = {'A', 'E', 'I', 'O', 'U'};
The above example declares an array named vowels of data type char which stores five elements.
Accessing an arrays elements

You can access an arrays elements by referring to the array by its name and the appropriate index number of the element you wish to access.
       char[] vowels = {'A', 'E', 'I', 'O', 'U'};
       System.out.println("Here is a vowel: " + vowels[3]);
       Here is a vowel: O 

Modifying an arrays elements
You can modify an arrays elements by referring to the array by its name and the appropriate index number of the element you wish to modify.
     arrayName[index] = newValue;
     char  vowels[] = new int[1];
     vowels[0] = ‘A’; 
     vowels[1] = ‘E’; 
     System.out.println("vowels[0]: " + vowels[0]);
     System.out.println("vowels[1]: " + vowles[1]);
    //Now change some array values
     System.out.println("new value of vowels[0]: " + vowels[0]); 
     vowels[0]: A
     vowels[2]: E
     new value of vowels[0]: I

How to Find the length of an array
        The length of an array is the number of elements in it. To get the length of an array, use the length property of the Array object with the array whose length you want to find out.
       int oddNumbers[] = new int[2]; 
       oddNumbers[0] = 1;
       oddNumbers[1] = 3;
       System.out.print("The length  of the oddNumbers array is " + oddNumbers.length);
      The length of the oddNumbers array is 2


Post a Comment