import java.util.ArrayList;
public class McMyList<T extends Comparable>
{
// creates array list with type T
ArrayList <T> list = new ArrayList<T>();
private T compare1;
private T compare2;
// add method that accepts T and sends it to list
public void add(T x)
{
// add x to list
list.add(x);
}
//return largest value from the ArrayList
public static <T> largest(ArrayList<T> list2)
{
for ( int index = 0; index < list.size(); index ++)
{
for ( int index2 = index+1; index2 < list.size(); index2 ++)
{
x = list.get(index);
y = list.get(index2);
if (x.compareTo(y) > 0)
return x;
else
return y;
}
}
}
//return smallest value from the ArrayList
public T smallest(T j)
{
return compare2;
}
public static void main (String args[])
{
String name1 = "Bob";
String name2 ="Jim";
McMyList<Integer> myList = new McMyList<Integer>(1);
}
}
Comparing Generic objects in ArrayList trouble
Page 1 of 17 Replies - 427 Views - Last Post: 05 October 2012 - 05:31 PM
#1
Comparing Generic objects in ArrayList trouble
Posted 05 October 2012 - 12:55 PM
Replies To: Comparing Generic objects in ArrayList trouble
#2
Re: Comparing Generic objects in ArrayList trouble
Posted 05 October 2012 - 01:24 PM
The main problem I see is that largest's type parameter T does not extend Comparable.
Edit: Oh, now I see it. Your largest method is missing a return type.
This post has been edited by sepp2k: 05 October 2012 - 01:25 PM
#3
Re: Comparing Generic objects in ArrayList trouble
Posted 05 October 2012 - 01:25 PM
EDIT: Ah..Now I think I understand...How exactly do you plan on measuring two generic objects? How is a integer bigger than a String and vice versa? It all depends on how you want to measure your objects.
This post has been edited by Kinaces: 05 October 2012 - 01:29 PM
#4
Re: Comparing Generic objects in ArrayList trouble
Posted 05 October 2012 - 02:00 PM
so ill create an instance of McMyList with strings and another with integers just to test. Measuring the object is not of much importance just as long as i have a way to determine a largest and smallest in the array list.
and sepp2k how would i define a generic return type in my largest method header, this is were i am having some difficulty
#5
Re: Comparing Generic objects in ArrayList trouble
Posted 05 October 2012 - 02:09 PM
#6
Re: Comparing Generic objects in ArrayList trouble
Posted 05 October 2012 - 03:10 PM
public <T> T largest(T comp1, T comp2)
{
for ( int index = 0; index < list.size(); index ++)
{
for ( int index2 = index+1; index2 < list.size(); index2 ++)
{
comp1 = list.get(index);
comp2 = list.get(index2);
if (comp1.compareTo(comp2) > 0)
return comp1;
else
return comp2;
}
}
}
The errors i am getting are:
javac "McMyList.java" (in directory: /home/mcclain/Documents/Java Test/Chap 17 - Generics)
McMyList.java:52: incompatible types
found : T
required: T
comp1 = list.get(index);
^
McMyList.java:53: incompatible types
found : T
required: T
comp2 = list.get(index2);
^
McMyList.java:55: cannot find symbol
symbol : method compareTo(T)
location: class java.lang.Object
if (comp1.compareTo(comp2) > 0)
^
3 errors
Compilation failed.
#7
Re: Comparing Generic objects in ArrayList trouble
Posted 05 October 2012 - 04:53 PM
import java.util.ArrayList;
public class McMyList<T extends Comparable>
{
// creates array list with type T
ArrayList <T> list = new ArrayList<T>();
// add method that accepts T and sends it to list
public void add(T x)
{
// add x to list
list.add(x);
}
//return largest value from the ArrayList
public <T> T largest (T comp1, T comp2 )
{
for ( int index = 0; index < list.size(); index ++)
{
for ( int index2 = index+1; index2 < list.size(); index2 ++)
{
if (list.get(index).compareTo(list.get(index2)) > 0)
{
comp1 = list.get(index);
return comp1;
}
else
{
comp2=list.get(index2);
return comp2;
}
}
}
}
//return smallest value from the ArrayList
public T smallest(T j)
{
}
public static void main (String args[])
{
String name1 = "Bob";
String name2 ="Jim";
int test1 = 1;
McMyList<Integer> test = new McMyList<Integer>();
test.add(1);
}
}
and my errors:
javac "McMyList.java" (in directory: /home/mcclain/Documents/Java Test/Chap 17 - Generics) McMyList.java:57: incompatible types found : T required: T comp1 = list.get(index); ^ McMyList.java:63: cannot find symbol symbol : variable equals location: class java.lang.Object comp2.equals.list.get(index2); ^ Note: McMyList.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 2 errors Compilation failed.
#8
Re: Comparing Generic objects in ArrayList trouble
Posted 05 October 2012 - 05:31 PM
only after many hours of head banging
This post has been edited by JavaNoob123: 05 October 2012 - 05:31 PM
|
|

New Topic/Question
Reply



MultiQuote




|