[英]how do i sort my array of objects in ascending order of a string object?
I have to take user input of (int id, String title, String folder, int pages).我必须接受用户输入(整数 ID、字符串标题、字符串文件夹、整数页面)。 I have to get the output in ascending order of string title(lexicographical).我必须按字符串标题(字典序)的升序获取输出。 I have written the code, but i output is quite different.我已经编写了代码,但我的输出完全不同。
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);
}
}}
I would remove the class at the beginning and add the class after the main method as:我会在开头删除该类并在 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);
}
}
Same as: Implements Comparable to get alphabetical sort with Strings与: 实现 Comparable 以获取字符串的字母排序
Then you can just get the array of books and use Arrays.sort(book_arr);
然后你就可以得到书的数组并使用Arrays.sort(book_arr);
For you:为你:
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);
}
}
}
You are getting the NullPointerException because b[i].title=sc.next();
你得到 NullPointerException 因为b[i].title=sc.next();
is commented out.被注释掉了。
A way of doing this without making Book Comparable is to sort a Stream:在不使 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));
}
I have to take user input of (int id, String title, String folder, int pages).我必须接受用户输入(整数ID,字符串标题,字符串文件夹,整数页面)。 I have to get the output in ascending order of string title(lexicographical).我必须以字符串标题(字典顺序)的升序获取输出。 I have written the code, but i output is quite different.我已经编写了代码,但是我的输出却大不相同。
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);
}
}}
You can simply handle your problem using stream
in java 8, so instead of using array
, you can use List
, then you must stream it, init it, and finally sort books based on their title
.您可以使用 java 8 中的stream
简单地处理您的问题,因此您可以使用List
而不是使用array
,然后您必须流式传输它,初始化它,最后根据title
对书籍进行排序。
1- Create Book objects (the same as yours) 1- 创建 Book 对象(与您的相同)
2- Create Book list (Instead of an array) 2- 创建图书列表(而不是数组)
3- Stream bookList, Init each book object then Sort them based on the title 3- Stream bookList,初始化每个书籍对象,然后根据标题对它们进行排序
4- Print the result 4- 打印结果
I coded your scenario as follows我将您的场景编码如下
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);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.