Java 基于对象参数值对toString中的对象进行排序
我已设置此toString方法Java 基于对象参数值对toString中的对象进行排序,java,logic,Java,Logic,我已设置此toString方法 public String toString (){ String result; result = "Ideal Family"; result += "\n"+person1.toString(); result += "\n"+person2.toString(); result += "\n"+person3.toString(); result += "\n"+person4.toString();
public String toString (){
String result;
result = "Ideal Family";
result += "\n"+person1.toString();
result += "\n"+person2.toString();
result += "\n"+person3.toString();
result += "\n"+person4.toString();
result += "\n"+pet1.toString();
result += "\n"+car1.toString();
result += "\n"+car2.toString();
result += "\n"+homeAddress.toString();
return result;
}
在其中,我按希望输出的顺序列出对象。是否有方法根据对象中的值对列表重新排序。
特别是在Person类中,根据实例变量int对Person对象重新排序
Person类接受这些参数
public Person (String personName,String personGender,String personSS,int personAge){
name = personName;
ss = personSS;
age = personAge;
gender = personGender;
}
首先,您应该使用一个集合,而不是四个变量。这会更容易 但对于您要查找的内容,请创建一个
人员的列表
List<Person> personList = Arrays.asList(person1, person2, person3, person4);
首先,您应该使用一个集合,而不是四个变量。这会更容易
但对于您要查找的内容,请创建一个人员的列表
List<Person> personList = Arrays.asList(person1, person2, person3, person4);
是的,但您首先需要一个列表
或数组
List<Person> family;
然后,您可以使用或重新排序该系列以进行打印。是的,但首先需要一个列表或数组
List<Person> family;
然后,您可以使用或重新排序族以进行打印
Person[] family;