import java.util.*;
public class Test
{
public static void main(String[] args)
{
SortedSet<A> parts = new TreeSet<A>();
parts.add(new A("rst", 789));
parts.add(new A("efg", 456));
parts.add(new A("abc", 123));
System.out.println(parts);
SortedSet<A> sortname = new TreeSet<A>
(new
Comparator<A>()
{
public int compare(A a, A b)
{
String x = a.getName();
String y = b.getName();
return x.compareTo(y);
}
}
);
sortname.addAll(parts);
System.out.println(sortname);
}
}
class A implements Comparable<A>
{
public A(String name, int number)
{
this.name = name;
this.number = number;
}
public String getName()
{
return name;
}
public int getNumber()
{
return number;
}
public int compareTo(A other)
{
}
private String name;
private int number;
}
------ Solution ------------------------------------- -------
is to follow what you shoot ?
by digital
return x.number-y.number;
------ Solution -------------------------- ------------------
by string arrangements.
Even numeric row, I want to ask is class A compareTo in how to write , not in the main function compare . Please provide further guidance.
same
public int compareTo (A other)
{
return this.name.compareTo (other.name) ;
}
------ Solution ------------------- -------------------------
landlord overridden in A class about toString () method .
------ Solution ---------------------------------------- ----
are to be compared by name , sequential course as it
------ Solution --------------------- -----------------------
TreeSet is sorted in ascending order by default , so as
------ For reference only - -------------------------------------
by string arrangements.
Even numeric row, I want to ask is class A compareTo in how to write , not in the main function compare . Please provide further guidance.
------ For reference only ---------------------------------- -----
by string arrangements.
Even numeric row, I want to ask is class A compareTo in how to write , not in the main function compare . Please provide further guidance.
same
public int compareTo (A other)
{
return this.name.compareTo (other.name) ;
}
The results obtained are how
moderator , why not have a screenshot function CSDN own way , every time I open the QQ now shots are saved , and then select the upload, good trouble .
------ For reference only -------------------------------------- -
would not have a problem with Comparable
------ For reference only -------------------------- -------------
indeed, as you said, plus toString in class A in just fine. I want to think about why ?
------ For reference only -------------------------------------- -
I actually use on the third floor moderators bit puzzled ,
public int compareTo(A other)
{
return this.name.compareTo(other.name);
}
compareTo method name , how they use the methods inside the compareTo?
------ For reference only -------------------- -------------------
Tieshanglai I put together a good program , but I have a question:
import java.util.*;
public class Test
{
public static void main(String[] args)
{
SortedSet<A> parts = new TreeSet<A>();
parts.add(new A("rst", 789));
parts.add(new A("opq", 589));
parts.add(new A("efg", 456));
parts.add(new A("ttt", 621));
parts.add(new A("abc", 123));
System.out.println(parts);
SortedSet<A> sortname = new TreeSet<A>
(new
Comparator<A>()
{
public int compare(A a, A b)
{
String x = a.getName();
String y = b.getName();
return x.compareTo(y);
}
}
);
sortname.addAll(parts);
System.out.println(sortname);
}
}
class A implements Comparable<A>
{
public A(String name, int number)
{
this.name = name;
this.number = number;
}
public String getName()
{
return name;
}
public int getNumber()
{
return number;
}
public String toString()
{
return name;
}
public int compareTo(A other)
{
// return this.number-other.number;
return this.name.compareTo(other.name);
}
private String name;
private int number;
}
I think System.out.println (parts); with System.out.println (sortname); should not be the same as the order is displayed , but the result seems to be the same.
------ For reference only ----- ----------------------------------
are to be compared by name , sequential course as it
System.out.println (parts); should be sorted before the output fishes Yeah , why the output sorted the same?
------ For reference only -------------------------------------- -
this way , thank you.
没有评论:
发表评论