Java 语言中对数组列表进行排序

发布于 2024-08-25 00:18:28 字数 622 浏览 11 评论 0 原文

我设置了一个数组列表。我也设置了输入指令,以便用户可以输入一个字符串,然后输入一个整数,然后输入一个字符串(名字、年龄和姓氏)。

我需要按姓氏对数组列表进行排序。到目前为止我输入的代码都在 main 方法下:-

public static void main(String[] args) {
Name Name[] = new Name[50];
int count = 0;

for (int i=0; i<50; i++)
  NewName[i] = new Name();

//ADD NEW TO ARRAYLIST NAME
String FName = JOptionPane.showInputDialog("first name");
int age = Integer.parseInt(JOptionPane.showInputDialog("age"));
String LName = JOptionPane.showInputDialog("last name");
          NewName[count] = new Name(FName, age, LName);
count = count++;
}

//ITEMS SORT BY LAST NAME
//CODE FOR SORT GOES HERE

I have an arraylist set up. I have input instuctions set up too, so that the user can enter one string, then one integer, then one string (the first name, the age, and the last name).

I need to sort the arraylist by the last name. The code I have entered so far is all under the main method:-

public static void main(String[] args) {
Name Name[] = new Name[50];
int count = 0;

for (int i=0; i<50; i++)
  NewName[i] = new Name();

//ADD NEW TO ARRAYLIST NAME
String FName = JOptionPane.showInputDialog("first name");
int age = Integer.parseInt(JOptionPane.showInputDialog("age"));
String LName = JOptionPane.showInputDialog("last name");
          NewName[count] = new Name(FName, age, LName);
count = count++;
}

//ITEMS SORT BY LAST NAME
//CODE FOR SORT GOES HERE

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_畞蕅 2024-09-01 00:18:28

看看比较比较器Arrays.sort集合.排序

import java.util.Arrays;


class Name implements Comparable<Name> {

    private String lastName;
    //Other fields

    public Name(String lastName){
        this.lastName = lastName;
    }

    public int compareTo(Name o) {
        //Null checks etc
        return lastName.compareTo(o.lastName);
    }
    public String getLastName(){
        return lastName;
    }
    //Getter and setter methods
}

public class Test{
    public static void main(String[] args) {
        Name[] arr = new Name[]{new Name("AAC"), new Name("AAD"), new Name("AAA"),new Name("ABC"), new Name("AADDE")};
        Arrays.sort(arr);
        for(Name nm:arr){
            System.out.println(nm.getLastName());
        }
    }
}

Take a look at Comparable, Comparator, Arrays.sort and Collections.sort

import java.util.Arrays;


class Name implements Comparable<Name> {

    private String lastName;
    //Other fields

    public Name(String lastName){
        this.lastName = lastName;
    }

    public int compareTo(Name o) {
        //Null checks etc
        return lastName.compareTo(o.lastName);
    }
    public String getLastName(){
        return lastName;
    }
    //Getter and setter methods
}

public class Test{
    public static void main(String[] args) {
        Name[] arr = new Name[]{new Name("AAC"), new Name("AAD"), new Name("AAA"),new Name("ABC"), new Name("AADDE")};
        Arrays.sort(arr);
        for(Name nm:arr){
            System.out.println(nm.getLastName());
        }
    }
}
怎言笑 2024-09-01 00:18:28

这并不是要冒犯,但我建议您在继续使用 Swing 之前学习基础知识。

This isn't meant to be offensive but I would suggest you learn the basics before you move on to Swing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文