this is my very first post. This is for Java 7.
Can someone please tell me why I'm not able to access "first.a" in the CompareIpaddress class? (I'm not sure why SOF won't allow me to post this unless I type more stuff...) Is this enough filler?
Thanks, Gordon
import java.util.Comparator;
import java.util.*; // just in case i need it.
import java.util.TreeSet;
public class CompareIpaddress <IPaddress >implements Comparator<IPaddress>
{
/************************************************compare()**/
public int compare (IPaddress first, IPaddress second)
{
if( first.a < second.a )
{
return -1;
}
}
}
import java.util.Comparator;
import java.util.*;
import java.util.TreeSet;
public class IPaddress
{
public int a, b, c, d;
IPaddress (int a, int b, int c, int d)
{
this.a=a;
this.b=b;
this.c=c;
this.d=d;
}
public int getA()
{
return a;
}
}
public class CompareIpaddress <IPaddress >implements Comparator<IPaddress>
You make IPaddress a type parameter of CompareIpaddress, so that is any type, not the class of the same name.
Since not every class has an accessible member a, the compiler cannot find the symbol.
public class CompareIpaddress implements Comparator<IPaddress>
is the correct way.
(You need to return a value also for the other cases, but I suppose you have just cut that down for the question.)
Remove the type parameter IPaddress in public class CompareIpaddress <IPaddress> implements Comparator<IPaddress>. By this you are shadowing the class name IPaddress with a type parameter.
You in fact don't need a type parameter here at all.
Change it to public class CompareIpaddress implements Comparator<IPaddress> and everything is fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With