Arrays In JAVA

Simple variables that hold one value are useful but are not sufficient for many applications. A program that plays a game of cards would want a number of Card objects it could manipulate as a whole. To meet this need, you use arrays.

An array is a collection of variables all of the same type. The components of an array are accessed by simple integer indexes. In a card game, a Deck object might look like this:

public class Deck
{
public static final int DECK_SIZE = 52;
private Card[] cards = new Card[DECK_SIZE];
public void print() {
for (int i = 0; i < cards.length; i++)
System.out.println(cards[i]);
}
// ...
}

First we declare a constant called DECK_SIZE to define the number of cards in a deck. This constant is public so that anyone can find out how many cards are in a deck. Next we declare a cards field for referring to all the cards. This field is declared private, which means that only the methods in the current class can access itthis prevents anyone from manipulating our cards directly. The modifiers public and private are access modifiers because they control who can access a class, interface, field, or method.

We declare the cards field as an array of type Card by following the type name in the declaration with square brackets [ and ]. We initialize cards to a new array with DECK_SIZE variables of type Card. Each Card element in the array is implicitly initialized to null. An array's length is fixed when it is created and can never change.

The println method invocation shows how array components are accessed. It encloses the index of the desired element within square brackets following the array name.

You can probably tell from reading the code that array objects have a length field that says how many elements the array contains. The bounds of an array are integers between 0 and length-1, inclusive. It is a common programming error, especially when looping through array elements, to try to access elements that are outside the bounds of the array. To catch this sort of error, all array accesses are bounds checked, to ensure that the index is in bounds. If you try to use an index that is out of bounds, the runtime system reports this by throwing an exception in your programan ArrayIndexOutOfBoundsException.

An array with length zero is an empty array. Methods that take arrays as arguments may require that the array they receive is non-empty and so will need to check the length. However, before you can check the length of an array you need to ensure that the array reference is not null. If either of these checks fail, the method may report the problem by throwing an IllegalArgumentException.

For example, here is a method that averages the values in an integer array:

static double average(int[] values)
{
if (values == null)
throw new IllegalArgumentException();
else
if (values.length == 0)
throw new IllegalArgumentException();
else {
double sum = 0.0;
for (int i = 0; i < values.length; i++)
sum += values[i];



return sum / values.length;
}

}

This code works but the logic of the method is almost completely lost in the nested ifelse statements ensuring the array is non-empty. To avoid the need for two if statements, we could try to test whether the argument is null or whether it has a zero length, by using the boolean inclusive-OR operator (|):

if (values == null | values.length == 0)
throw new IllegalArgumentException();

Unfortunately, this code is not correct. Even if values is null, this code will still attempt to access its length field because the normal boolean operators always evaluate both operands. This situation is so common when performing logical operations that special operators are defined to solve it. The conditional boolean operators evaluate their right-hand operand only if the value of the expression has not already been determined by the left-hand operand. We can correct the example code by using the conditional-OR (||) operator:

if (values == null || values.length == 0)
throw new IllegalArgumentException();

Now if values is null the value of the conditional-OR expression is known to be true and so no attempt is made to access the length field.

The binary boolean operatorsAND (&), inclusive-OR (|), and exclusive-OR (^)are logical operators when their operands are boolean values and bitwise operators when their operands are integer values. The conditional-OR (||) and conditional-AND (&&) operators are logical operators and can only be used with boolean operands.

We Are Founder..