Common Array Algorithms

This chapter discusses how a for loop can be used to iterate over the elements of a one-dimensional array. In addition, it also discusses enhanced for loops. The chapter demonstrates the use of arrays to solve common problems such as finding sum, average, maximum, and minimum values of numbers stored in array. Pay attention to some of the common programming errors one can make while using arrays.

25. Various equals() Methods


Answer:

A: NOT Equal

    The two arrays are different lengths.

B: NOT Equal

    The elements are not in the same order.

C: Equal

    The array is compared with itself, and found to equal.

Various equals() Methods

The method used in the above examples is this:

static boolean Array.equals( int[] a, int[] a2 )

The class Arrays has a variety of equals() methods to handle different types. But in all cases, both arrays are of the same type.

static boolean 	equals(boolean[] a, boolean[] a2)

static boolean equals(byte[] a, byte[] a2)

static boolean equals(char[] a, char[] a2)

static boolean equals(double[] a, double[] a2)

static boolean equals(float[] a, float[] a2)

static boolean equals(int[] a, int[] a2)

static boolean equals(long[] a, long[] a2)

static boolean equals(Object[] a, Object[] a2)

static boolean equals(short[] a, short[] a2)

Question 25:

Could you write your own equals() method.