gpt4 book ai didi

java - 按对象中的不同参数对 ArrayList 进行排序

转载 作者:行者123 更新时间:2023-11-30 10:02:31 26 4
gpt4 key购买 nike

<分区>

我有一个类“Person”,它包含一个带有 3 个参数的构造函数。现在我想要一个排序选项,我需要它按 ID、名称或地址升序和降序排序

public class Person
{
private int id;
private String name;
private String addr;


public Person()
{
id = MyTools.getInteger("ID: ");
name = MyTools.getString("Name: ");
addr = MyTools.getString("Address: ");

}

这里是我要排序的地方:

public static void main(String[] args) throws FileNotFoundException
{
boolean loop = true;

System.out.println("Choose:");
System.out.println("\t" + "0 = Exit programm");
System.out.println("\t" + "1 = Add new");
System.out.println("\t" + "2 = Show");
System.out.println("\t" + "3 = Sort");
System.out.println("\t" + "4 = Delete");
System.out.println("\t" + "5 = Save");
System.out.println("\t" + "6 = Help");

TextFile tf = new TextFile("AdressVerwaltung.txt", 'o');
List<Person> adressen = new ArrayList<Person>();
int listSize = 0;
while (loop)
{

int choice = MyTools.getInteger("Please choose which operation you want
to execute (6 = Help): "+"\n");

switch (choice)
{
case 0:
System.out.println("Programmende");
for (int k = 0; k < listSize; k++)
{
tf.printLine(adressen.get(k).toString());
}
tf.close();
loop = false;
break;

case 1:


Person p = new Person();
adressen.add(p);
System.out.println("Data is put in Array!");
listSize = adressen.size();

break;
case 2:

break;
case 3:
break;

现在我希望能够选择是按 ID、按名称还是按地址排序

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com