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

时间:2018-10-21 17:47:55

标签: java arrays sorting object

我正在尝试模拟相册库。但是我也可以通过作者的名字按字母顺序组织图书馆的内容。对如何按字母顺序组织对象数组的内容有帮助吗?

我创建了一个名为Album的类,用于创建对象

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;

    } 

}

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

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;         
    }
}

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

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);       
    }    
}

1 个答案:

答案 0 :(得分:1)

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

 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);

}  

} 而且我在数组的排序元素中添加了方法排序:

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;
            }
        }
    }
}

}

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

        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);  
}