【问题标题】:Java: How to sort array upon return [duplicate]Java:如何在返回时对数组进行排序[重复]
【发布时间】:2014-05-02 22:13:01
【问题描述】:

如何按字母或数字(pg nums)对 IF 语句中的数组进行排序?请注意,Arrays.sort() 在使用时不会编译。我正在尝试对返回的值进行排序,而不是对初始输入进行排序。

import java.util.Arrays;
import java.util.Scanner;

public class LibraryBookSort
{
    public static void main(String[] args)
    {
        String title, author;

        int x;
        int pages, sortBy;

        LibraryBook[] titleArray = new LibraryBook[5];
        LibraryBook[] authorArray = new LibraryBook[5];
        LibraryBook[] pagesArray = new LibraryBook[5];

        for (x = 0; x < 5; x++)
        {
            titleArray[x] = new LibraryBook();
            authorArray[x] = new LibraryBook();
            pagesArray[x] = new LibraryBook();
        }
        //User Input
        Scanner input = new Scanner(System.in);

        for (x = 0; x < 5; x++)
        {
            //TitleInput
            System.out.print("Enter the title of a book: ");
            title = input.nextLine();
            titleArray[x].setBook(title);

            //Author Input
            System.out.print("Enter the author of this book: ");
            author = input.nextLine();
            authorArray[x].setAuthor(author);

            //Page Input
            System.out.print("Enter the number of pages for this book: ");
            pages = input.nextInt();
            pagesArray[x].setPages(pages);

            input.nextLine();
        }

        //Book Organize input
        System.out.println("How would you like to organize your values?");
        System.out.println("Sort by title > Enter 1: ");
        System.out.println("Sort by author's last name > Enter 2: ");
        System.out.print("Sort by page count > Enter 3: ");
        sortBy = input.nextInt();



        // Sort by title
        if(sortBy == 1)
        {
            for(x = 0; x < 5; x++)
            {

                System.out.println();
                System.out.println("Book ");
                System.out.println("Title: " + titleArray[x].getBook());
                System.out.println("Author: " + authorArray[x].getAuthor());
                System.out.println("Page Count: " + pagesArray[x].getPages());
            }
        }
        // Sort by author
        else
        if(sortBy == 2)
        {
            for(x = 0; x < 5; x++)
            {
                System.out.println();
                System.out.println("Book ");
                System.out.println("Author: " + authorArray[x].getAuthor());
                System.out.println("Title: " + titleArray[x].getBook());
                System.out.println("Page Count: " + pagesArray[x].getPages());
            }
        }
        // Sort by page count
        else
        if(sortBy == 3)
        {
            for(x = 0; x < 5; x++)
            {
                System.out.println();
                System.out.println("Book ");
                System.out.println("Page Count: " + pagesArray[x].getPages());
                System.out.println("Title: " + titleArray[x].getBook());
                System.out.println("Author: " + authorArray[x].getAuthor());
            }
        }
    }
}

【问题讨论】:

  • “Arrays.sort() 使用时不会编译”是什么意思? Java API 通常不包含“使用时不会编译”的方法。您是如何尝试使用它的?
  • 不要一次又一次地问同样的问题。编辑您的原始问题以使其清楚,并回答您在 cmets 中遇到的问题。
  • 例如 System.out.println("作者:" + Arrays.sort(authorArray[x].getAuthor()));不会编译,因为它是无效的。
  • @ewbrowning 看来你不知道Arrays.sort 是如何工作的。你为什么要Arrays.sort(authorArray[x].getAuthor())?你这样做的目的是什么?
  • 通过回答您在 cmets 中遇到的问题。有人告诉您,对字符串进行排序没有任何意义,并且有人问您要达到什么目的。而你没有给出任何答案。

标签: java arrays sorting


【解决方案1】:

如果Arrays.sort 无法编译,则说明你做错了。在任何情况下,您都需要定义自定义 Comparator&lt;LibraryBook&gt; 对象,每个对象都会比较相关字段,然后使用这些对象进行排序。如果您无论如何要保留多个数据结构,不妨将它们设为SortedSets 而不是普通数组。

【讨论】:

    猜你喜欢
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    相关资源
    最近更新 更多