IntBagArray
can only hold integers. Generic programming provides a better approach than having 8 (the number of primitive data types) different classes, each of a different type.
Object
and a variable of that type
can hold a reference to any kind of object.String s = new String("Objection overruled!");
Object obj;
obj = s;
String s = new String("Objection overruled!");
Object obj;
obj = s;
s = new String("Make it so.");
...
s = (String) obj;
int i = 42;
int j;
Integer example;
example = new Integer(i); // Boxing
example = i; // Autoboxing
j = example.intValue( ); // Unboxing
j = example; // Auto-unboxing
IntBagArray
vs DoubleBagArray
you could use Object
as the data type.
public static Object middle( Object [] data) {
if (data.length == 0)
{ // The array has no elements, so return null.
return null;
}
else
{ // Return an element near the center of the array.
return data[data.length/2];
}
}
Why Not Just Do This? - Well, using objects instead of generics is not quite a useful as
you have to make sure to cast your variables to and from objects in order for everything
to work and some casts may not work at runtime causing errors you can't catch as you compile.
public static <T> T middle(T[] data) {
if (data.length == 0)
{ // The array has no elements, so return null.
return null;
}
else
{ // Return an element near the center of the array.
return data[data.length/2];
}
}
Generic Type Parameter - <T> is a generic type parameter and can eventually be
supplied by the programmer when calling the method and supplied a variable of any class type.
The generic type parameter should be one character and should always be included with its angle
brackets before the return type of the same letter in the method. This syntax indicates to the
Java compiler that anywhere else in the method the generic type is replaced with that single letter.
static <T> T middle(T[] data)...
The generic data type is often named with a single capital letter, such as T for “type.” The name T can then be used in the rest of the method implementation as if it were a real class name (with a few exceptions). // i is an integer; ia is an Integer array; c is a Character
i = middle(ia); // This is fine
c = middle(ia); // Compile-time type error
public class ArrayBag<E> implements Cloneable
{
// Invariant of the ArrayBag<E> generic class:
// 1. The number of elements in the bag is in the instance variable manyItems.
// 2. For an empty bag, we do not care what is stored in any of data;
// for a non-empty bag, the elements in the bag are stored in data[0]
// through data[manyItems-1], and we don't care what's in the rest of the data.
//
//
private Object[ ] data;
private int manyItems;
private Object[ ] data;
return (E) data[i]; // From the end of the grab method
addMany
method of a Bag
class. A proper way of suppressing compiler warnings for generic typecast and varity method warnings is to include the following directive in your code prior to a method that would generate the warning:@SuppressWarnings("unchecked")
.equals()
method for determining equality at the object level. If converting a regular collection class
that stores int values to a generic class, your code may include several ==
operators that must be changes to the .equals()
method.null
in your code so that the Java runtime may free up the memory for
that object (garbage collection).