7 Replies - 2284 Views - Last Post: 05 October 2012 - 05:31 PM Rate Topic: -----

#1 JavaNoob123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 29-February 12

Comparing Generic objects in ArrayList trouble

Posted 05 October 2012 - 12:55 PM

So I am pretty confuzzled when it comes to generics and in this program I am trying to compare a generic type that extends Comparable, so Strings, ints ... etc. in an arrayList. If anyone could point out where i am going wrong that would be very helpfull. The main problem i am having is returing a generic type from my largest method.
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);
	
}
		
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Comparing Generic objects in ArrayList trouble

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,311
  • Joined: 21-June 11

Re: Comparing Generic objects in ArrayList trouble

Posted 05 October 2012 - 01:24 PM

What's the problem you have with returning a generic from your largest method? If you're getting an error message, please post it verbatim.

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

Was This Post Helpful? 0
  • +
  • -

#3 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Comparing Generic objects in ArrayList trouble

Posted 05 October 2012 - 01:25 PM

For one thing this program will not even compile for reasons that have nothing to do with your question.. I'm not quite sure what you are asking help with.

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

Was This Post Helpful? 1
  • +
  • -

#4 JavaNoob123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 29-February 12

Re: Comparing Generic objects in ArrayList trouble

Posted 05 October 2012 - 02:00 PM

Well i know the program is incomplete i am just trying to pin down my largest method before continuing. And to answer your question Kinaces I am only trying to compare Strings together and so on
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
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,311
  • Joined: 21-June 11

Re: Comparing Generic objects in ArrayList trouble

Posted 05 October 2012 - 02:09 PM

View PostJavaNoob123, on 05 October 2012 - 11:00 PM, said:

and sepp2k how would i define a generic return type in my largest method header, this is were i am having some difficulty


T. The return type should be T.
Was This Post Helpful? 1
  • +
  • -

#6 JavaNoob123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 29-February 12

Re: Comparing Generic objects in ArrayList trouble

Posted 05 October 2012 - 03:10 PM

Ok here is my modified largest method.
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.
Was This Post Helpful? 0
  • +
  • -

#7 JavaNoob123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 29-February 12

Re: Comparing Generic objects in ArrayList trouble

Posted 05 October 2012 - 04:53 PM

Here is my complete code. I am beating my head against the wall trying to fix this so any help would be greatly appreciated.
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.


Was This Post Helpful? 0
  • +
  • -

#8 JavaNoob123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 29-February 12

Re: Comparing Generic objects in ArrayList trouble

Posted 05 October 2012 - 05:31 PM

Never Mind i found a work around *resolved*
only after many hours of head banging

This post has been edited by JavaNoob123: 05 October 2012 - 05:31 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1