gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:57:29 26 4
gpt4 key购买 nike

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

我创建了一个名为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);
}
}

最佳答案

我必须更改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);

}

}我添加了方法 sort 来对数组元素进行排序:

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

关于java - 如何在 Java 中按字母顺序组织对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52918180/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com