假设我有一个包含三个字段的类:
class Component {
private String color;
private int weight;
private TreeSet<Component> components = new TreeSet<Component>(new CustomComparator());
}
Run Code Online (Sandbox Code Playgroud)
有没有办法创建CustomComparator,以便它根据颜色将组件分类到TreeSet中,当且仅当所有组件具有相同的权重(否则,它根据其重量对它们进行排序),而无需创建其他比较器或方法?
伙计们,抱歉这个无聊的问题充斥着这个网站,但即使在阅读了关于C#的文档并查看他们的例子之后,我似乎也无法找到解释.
也就是说,我有这样的代码:
String s = "Hello";
String n = "World";
String s1 = "LALALALALALALA";
String n1 = "heyy";
String s2 = "sffsdfsfdsfsd";
String n2 = "dsfsdfdsfdsfsdsd";
String z = String.Format("{0,-20}{1,-10}", s, n);
String z1 = String.Format("{0,-20}{1,-10}", s1, n1);
String z2 = String.Format("{0,-20}{1,-10}", s2, n2);
Run Code Online (Sandbox Code Playgroud)
(此代码仅用于测试)
现在,当我使用:
Console.WriteLine(z);
Console.WriteLine(z1);
Console.WriteLine(z2);
Run Code Online (Sandbox Code Playgroud)
将输出写入控制台,它按照我的预期工作; 为字符串参数{0}分配了20个空格,并将下一个空格分配给字符串参数{1}.但是,当我在表单中运行完全相同的代码时(作为MessageBox输出,甚至是TextBox文本,我得到了混乱的结果.参数{0}没问题,但是参数{1}从距离结尾看似随机的距离开始{0}.
有没有理由发生这种情况?我该如何解决?
谢谢你的回答!