为了数组列表排序 [英] arraylist sorting in order

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

问题描述

试图以按字母顺序排列的ArrayList但Collection.sort是给我的错误,我不什么用它做哪些

 进口的java.util.ArrayList;
进口java.util.Collection中;/ **
 *
 * @author用户
 * /
公共类书架{    ArrayList的<图书> listOfBooks =新的ArrayList<图书>();    公共无效addBook(书书){
        listOfBooks.add(书);
    }    公众的ArrayList<图书> returnListOfBooks(){        ArrayList的<图书> myBook =新的ArrayList<图书>(listOfBooks);
        Collection.sort(myBook);
        返回myBook;
    }

在这里输入的形象描述

这是如何解决这一问题的任何帮助吗?非常感谢!

修改*

只是需要回到书是为了一个ArrayList,
更改为集合后,我已经得到了另一个困难。

在这里输入的形象描述

我已经改变收集进口以及

 进口的java.util.ArrayList;
进口java.util.Collections中;


解决方案

您使用了错误的类。
含排序(...)方法的类是收藏不收藏

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!

Edit*

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

I've change collection import as well

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

解决方案

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

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

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