Monday 1 August 2011

ARRAYS

An array is a group of variables (called elements or components) containing values that all have the same type. Recall that types are divided into two categoriesprimitive types and reference types. Arrays are objects, so they are considered reference types. As you will soon see, what we typically think of as an array is actually a reference to an array object in memory. The elements of an array can be either primitive types or reference types . To refer to a particular element in an array, we specify the name of the reference to the array and the position number of the element in the array. The position number of the element is called the element's index or subscript.
Java Arrays (Single Dimensional Arrays)
  • An array is a sequence of variables of the same data type (homogenous).
  • The data type can be any of Java�s primitive types or a class (user-defined as well).
  • Each variable in the array is an element.
  • We use an index to specify the position of each element in the array.
  • Arrays are useful for many applications, including calculating statistics or representing the state of a game.
  • Arrays are always homogeneous, or of the same data type.
  • Array indexes always begin with zero (0).
  • Declaring and instantiating arrays
  • In Java Arrays are objects, so they need to be declared and instantiated
  • Types get default values inside the array, just as they do during normal instantiation. Examples:.   Java Text Books
    int[] array;
    int[] array = new int[9];
    int[] array = {1,2,3,4,5};
    Auto[] array = {new Auto()}; 

     
Declaring arrays:
double [] dailyTemps; // elements are doubles
String [] cdTracks; // elements are Strings
boolean [] answers; // elements are booleans
Auto [] cars; // elements are Auto references
int [] cs101, bio201; // two integer arrays

Instantiating these arrays:
dailyTemps = new double[365]; // 365 elements
cdTracks = new String[15]; // 15 elements
int numberOfQuestions = 30;
answers = new boolean[numberOfQuestions];
cars = new Auto[3]; // 3 elements
cs101 = new int[5]; // 5 elements
bio201 = new int[4]; // 4 elements

Initialization List
The initialization list can only be used when instantiating the array, trying to do so after initialization will cause a compilation error. Example: int[] array = {1,2,3,4,5};
Aggregate Array Operations
- You can perform the same operations on arrays as we do on a series of input values.
- To perform an operation on all elements in an array, we use a for loop to perform the operation on each element.
in turn. For Example tp print all elements of an array:
for(int i=0; i<array.length;i+) {
System.out.println(cellBills[i]);
}

Reading Items Into Array
import java.util.Scanner;
public class GetArrayValues
{
public static void main( String [] args )
{
Scanner scan = new Scanner( System.in );
double [] scores = new double[6];
for ( int i = 0; i < scores.length; i++ )
{
System.out.println( "Enter Score: "
+ ( i + 1 ));
scores[i] = scan.nextDouble( ); // read current bill
}
}
}

No comments:

Post a Comment