maximum method for UnorderedArrayList

I have created the following method in UnorderedArrayList but am having problems calling it from a test class to show that the method works.

publicclass UnorderedArrayList<T>extends ArrayListClass<T>

{

public <Textends Comparable><T> >int maximum()

{

int maxLoc = 0;

if (length == 0)

System.out.println("No items in the list.");//return null; // if no items have been put in list, return null

else

{

for (int loc = 1; loc < length - 1; loc++)// loop through items in list

{

if (larger((T)list[maxLoc], (T)list[loc])== (T)list[loc])// if larger returns the current item, set maxLoc to current location

maxLoc = loc;//max = (T)list[loc];

}

return maxLoc;

}

}

publicclass test

{

UnorderedArrayList<Integer> UAL3 =new UnorderedArrayList<Integer>(5);

UAL3.insertAt(0, 30);

UAL3.insertAt(1, 3);

UAL3.insertAt(2, 70);

UAL3.insertAt(3, 65);

UAL3.insertAt(4, 34);

System.out.println("Original list:");

UAL3.print();

System.out.println("Max: " + UAL3.list[UAL3.maximum()]);

On this second part, I am receiving the error:

test.java:56: incompatible types; inferred type argument(s) java.lang.Object do not conform to bounds of type variable(s) T

found: <T>int

required: int

System.out.println("Max: " + UAL3.list[UAL3.maximum()]);

If I take out the <T extends Comparable><T> > in maximum, the call from test works but then the call to larger does not.

Any help would be appreciated. Thank you

[2613 byte] By [p_m_spauldinga] at [2007-11-26 18:49:44]
# 1

> ...

> If I take out the < T extends Comparable<T> > in

> maximum, the call from test works but then the call

> to larger does not.

You'll have to remove one return type. Either return a T (without the extends stuff behind it) or return an int.

You could try this (untested!) code:public class UnorderedArrayList< T extends Comparable <T> > extends ArrayListClass<T> {

public T maximum() {

if (length == 0) {

return null;

}

T max = list[0];

for (int i = 1; i < length-1; i++) {

T temp = list[i];

if (max.compareTo(temp) < 0) {

max = temp;

}

}

return max;

}

}

Checkout this tutorial on generics:

http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

prometheuzza at 2007-7-9 6:23:46 > top of Java-index,Java Essentials,Java Programming...