首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Java中按字母顺序组织对象数组

如何在Java中按字母顺序组织对象数组
EN

Stack Overflow用户
提问于 2018-10-22 01:47:55
回答 1查看 139关注 0票数 1

我正在尝试模拟一个唱片库。但我也可以按作者姓名的字母顺序组织图书馆的内容。如何按字母顺序组织对象数组的内容有什么帮助吗?

我已经创建了一个名为Album的类,我使用它来创建我的对象

代码语言:javascript
复制
public class Album  {

    private String author;
    private String name;
    private String year;

    public Album(String a, String n, String y) { // constructor

    author = a;
    name = n;
    year = y;

    }

    public String toString()
    {

        return  author +","+ name + "," + year;

    } 

}

类集合用于将对象存储到数组中

代码语言:javascript
复制
public class AlbumCollection {


    public  Album collection[]= new Album[10]; 

    private int numAlbums = 0;

    public void add (Album a){

        if (numAlbums >= collection.length){

            Album newcollection[]= new Album [collection.length * 2];

            for (int n = 0; n < numAlbums; n ++){

                newcollection[n] = collection[n];
            }

            newcollection = collection;

        }     

        collection[numAlbums] = a;

        numAlbums = numAlbums + 1;

    }

    public String toString()
    {
         String details = "";

                for ( int p = 0; p < collection.length ; p ++)

                {
                    details = details + collection[p] +  "\n" ;

                    }

                details += "\n";         

         return details;         
    }
}

这是我用来创建相册对象的类

代码语言:javascript
复制
public class TestCollection {

    public static void main(String[] args) {

        AlbumCollection c = new AlbumCollection();

        c.add( new Album("DaftPunk","Discovery","2001"));
        c.add( new Album ("Pink Floid","The Dark Side Of The Moon","1973"));
        c.add( new Album( "The Clash", "London Calling", "1979"));

        System.out.print(c);       
    }    
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-22 02:28:14

我不得不将compareTo方法更改为按作者排序。

代码语言:javascript
复制
 public class Album  {
private String author;
private String name;
private String year;

public Album(String a, String n, String y) { // constructor

author = a;
name = n;
year = y;

}

public String toString()
{

    return  author +","+ name + "," + year;

} 

public int compareTo(Album a) {
    // usually toString should not be used,
    // instead one of the attributes or more in a comparator chain
    return author.compareTo(a.author);

}  

},我给数组的元素添加了排序方法:

代码语言:javascript
复制
public class Collection {


public  Album collection[]= new Album[10]; 

private int numAlbums = 0;

public void Add (Album a){

    if (numAlbums >= collection.length){

        Album newcollection[]= new Album [collection.length * 2];

        for (int n = 0; n < numAlbums; n ++){

            newcollection[n] = collection[n];
        }

        newcollection = collection;

    }     

    collection[numAlbums] = a;

    numAlbums = numAlbums + 1;

}

public String toString()
{
     String details = "";

            for ( int p = 0; p < numAlbums ; p ++)

            {
                details = details + collection[p] +  "\n" ;

                }

            details += "\n";         

     return details;         
}
public void sort(){
    for(int i=0;i<numAlbums;i++){
        for(int j=i;j<numAlbums-1;j++){
            if(collection[j].compareTo(collection[j+1])>0){
                Album tmp =collection[j];
                collection[j]=collection[j+1];
                collection[j+1]=tmp;
            }
        }
    }
}

}

如果存储作者的数量,则不能使用数组的长度,因为将打印空值

代码语言:javascript
复制
        public static void main(String[] args) {
    Collection c = new Collection();

    c.Add( new Album("DaftPunk","Discovery","2001"));
    c.Add( new Album ("Pink Floid","The Dark Side Of The Moon","1973"));
    c.Add( new Album( "The Clash", "London Calling", "1979"));
    c.sort();
    System.out.print(c);  
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52918180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档