arraylist按顺序排序 [英] arraylist sorting in order

查看:139
本文介绍了arraylist按顺序排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试按字母顺序对arraylist排序,但Collection.sort给我一个错误,我不对它做什么。

Attempting to sort arraylist in alphabetical order but the Collection.sort is giving me an error which i do not what to do with it

import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author user
 */
public class BookShelf {

    ArrayList<Book> listOfBooks = new ArrayList<Book>();

    public void addBook(Book book) {
        listOfBooks.add(book);
    }

    public ArrayList<Book> returnListOfBooks() {

        ArrayList<Book> myBook = new ArrayList<Book>(listOfBooks);  
        Collection.sort(myBook);
        return myBook;
    }

有关如何解决这个问题的任何帮助?非常感谢!

any help on how to fix this? thanks a lot!

编辑*

只需要返回一个数组列表在改变成Collections之后,
,我又遇到了困难。

Just need to return an arraylist of books which is in order, after changing to Collections, I've gotten another difficulty.

我已更改集合导入

import java.util.ArrayList;
import java.util.Collections;


推荐答案


包含sort(...)方法的Class是Collections not Collection

You are using the wrong class. The Class containing the sort(...) method is Collections not Collection

这篇关于arraylist按顺序排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆