The problem i am having is that when i run the program it only compares the first two numbers of the ArrayList and does not factor in the rest.
import java.util.ArrayList;
public class JEB_003_001<T extends Number> {
ArrayList<T> al = new ArrayList<T>();
public void add(T number){
al.add(number);
}
public T largest(){
T large = al.get(0);
for (int i = 0; i < al.size(); i++){
if(al.get(i).toString().compareTo(large.toString()) > 0)
large = al.get(i);
}
return large;
}
public T smallest(){
T small = al.get(0);
for(int i=0; i < al.size(); i++){
if(al.get(i).toString().compareTo(small.toString()) < 0)
small = al.get(i);
}
return small;
}
public void display(){
System.out.println(al);
}
}
and my driver is
public class JEB_003_001_002 {
public static void main(String args[]){
JEB_003_001<Number> list = new JEB_003_001<Number>();
list.add(7);
list.add(-9);
list.add(23);
list.add(18);
list.add(12);
System.out.println("The numbers from the list are: ");
list.display();
Number largest = list.largest();
System.out.println();
System.out.println("The largest number from the list is: " + largest);
Number smallest = list.smallest();
System.out.println();
System.out.println("The smallest number in the list is: " + smallest);
}
}

New Topic/Question
Reply


MultiQuote


|