【问题标题】:how do i sort my array of objects in ascending order of a string object?如何按字符串对象的升序对对象数组进行排序?
【发布时间】:2020-05-24 11:01:00
【问题描述】:

我必须接受用户输入(int id、String title、String 文件夹、int pages)。我必须按字符串标题(字典)的升序获取输出。我已经写了代码,但是我的输出是完全不同的。

package zzz;
import java.util.Scanner;
import java.util.Arrays;
import java.util.Collections;

class Book{
    int id;
    String title;
    String folder;
    int pages;
}


public class practice {

    public static void main(String[] args)throws Exception {
        // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);
    Book b1 = new Book();
    Book b2 = new Book();
    Book b3 = new Book();
    Book b[]= {b1,b2,b3};
    for(int i=0;i<b.length;i++) {
        b[i].id=sc.nextInt();
        sc.nextLine();
    b[i].title=sc.next();
        sc.nextLine();
        b[i].folder=sc.next();
        b[i].pages=sc.nextInt();
    }
    Book temp = null;
    for(int i=0;i<b.length;i++) {
        for(int j=0;j<b.length-1-i;j++) {
            if(b[i].title.compareTo(b[j].title)<0) {
             temp =b[j];
            b[j]=b[j+1];
            b[j+1]=temp;
        }}
    }
    for(int i=0;i<b.length;i++) {
        System.out.println(b[i].id+" "+b[i].title+" "+b[i].folder+" "+b[i].pages);
    }

    }}

【问题讨论】:

标签: java arrays string class sorting


【解决方案1】:

我会删除开头的类并在main方法之后添加类:

static class Book implements Comparable<Book>{
    int id;
    String title;
    String folder;
    int pages;

    @Override
    public int compareTo(Book other) {
        //If this is backword then switch it to -> other.title.compareTo(this.title);
        return this.title.compareTo(other.title);
     }
}

同:Implements Comparable to get alphabetical sort with Strings

然后你就可以获取书籍数组并使用Arrays.sort(book_arr);

为你:

package zzz;
import java.util.Scanner;
import java.util.Arrays;
import java.util.Collections;


public class practice {

    public static void main(String[] args)throws Exception {
        // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);
    Book b1 = new Book();
    Book b2 = new Book();
    Book b3 = new Book();
    Book b[]= {b1,b2,b3};
    for(int i=0;i<b.length;i++) {
        b[i].id=sc.nextInt();
        sc.nextLine();
        b[i].title=sc.next();
        sc.nextLine();
        b[i].folder=sc.next();
        b[i].pages=sc.nextInt();
    }

    //Sort!
    Arrays.sort(b);

    for(int i=0;i<b.length;i++) {
        System.out.println(b[i].id+" "+b[i].title+" "+b[i].folder+" "+b[i].pages);
    }

    }
    static class Book implements Comparable<Book>{
        int id;
        String title;
        String folder;
        int pages;

        @Override
         public int compareTo(Book other) {
             return this.title.compareTo(other.title);
         }
     }
}

【讨论】:

  • 没听懂,请您修改我的代码并帮助我
  • 非常感谢。我以不同的方式修改了我的代码,但输出不同。它应该来 aa, cc, ss 但它来了 aa ss cc。请看一下
  • 已修复!这是新代码。我在 eclipse 中测试过,效果很好!
【解决方案2】:

您收到 NullPointerException 是因为 b[i].title=sc.next(); 已被注释掉。

在不使 Book Comparable 的情况下这样做的一种方法是对 Stream 进行排序:

Stream.of(b)
    .sorted((thisBook,anotherBook) -> thisBook.title.compareTo(anotherBook.title))
    .forEach(bk -> System.out.println(bk.id+" "+bk.title+" "+bk.folder+" "+bk.pages));
}

【讨论】:

    【解决方案3】:

    您可以在 java 8 中使用stream 简单地处理您的问题,因此您可以使用List 而不是array,然后您必须它,init,最后根据 title 对书籍进行排序

    夏天

    1- 创建 Book 对象(与您的相同)

    2- 创建图书列表(而不是数组)

    3- Stream bookList,初始化每个书本对象,然后根据标题排序它们

    4- 打印结果

    我将您的场景编码如下

    public static void main(String[] args)  {
    
            //Create Book Objects
            Scanner sc = new Scanner(System.in);
            Book b1 = new Book();
            Book b2 = new Book();
            Book b3 = new Book();
    
            //Create BookList
            List<Book> bookList = new ArrayList<>();
            bookList.add(b1);
            bookList.add(b2);
            bookList.add(b3);
    
            //Stream bookList, Init Books, Sort them based on the title
            bookList.stream().peek(book -> {
    
                //Init book objects (id , folder , title , pages)
                book.id = sc.nextInt();
                sc.nextLine();
                book.folder = sc.next();
                sc.nextLine();
                book.title = sc.next();
                sc.nextLine();
                book.pages = sc.nextInt();          
    
            }).sorted(Comparator.comparing(book -> book.title)).collect(Collectors.toList());
    
    
            //Print results
            bookList.forEach(book -> {
                System.out.println(book.title);
            });
        }
    

    【讨论】:

      猜你喜欢
      • 2014-07-10
      • 1970-01-01
      • 2019-11-24
      • 2019-12-17
      • 1970-01-01
      • 2011-05-16
      相关资源
      最近更新 更多