简体   繁体   English

如何将对象存储在数组中以备后用?

[英]How to store objects in an array to be used later?

I was curious how to store java objects in an array. 我很好奇如何将Java对象存储在数组中。 I am confused, because my teacher does not know himself, and we have not gone over object arrays, only arrays and objects separately. 我很困惑,因为我的老师不认识他自己,我们还没有遍历对象数组,只讨论了数组和对象。

My code looks like such: 我的代码如下所示:

public class SongDriver
    {
    public static void main( String[] args )
        {
        Song a = new Song();
        a.title = "Freebird";
        a.artist = "Lynyrd Skynryd";

        Song b = new Song();
        b.title = "Sweet Home Alabama";
        b.artist = "Lynyrd Skynryd";

        Song c = new Song();
        c.title = "Black or White";
        c.artist = "Michael Jackson";

        Song d = new Song();
        d.title = "Smooth Criminal";
        d.artist = "Michael Jackson";

        }
    }

This is exactly how my objects are supposed to be according to the teacher. 这正是我的老师认为应该达到的目的。

My other class called song is: 我的另一类歌是:

public class Song
    {
    String title;
    String artist; 
    }

I was trying things such as String myPod = new Song[4]; 我正在尝试诸如String myPod = new Song [4]; .. But nothing is working, and my knowledge is basic. ..但是没有任何效果,我的知识很基础。

An array is a defined container of a given type, for example... 数组是给定类型的已定义容器,例如...

{type}[] {variable};

So, based on the fact that you are trying to create an array of Song , you need to define the array as a type of Song , for example... 因此,基于您试图创建一个Song数组的事实,您需要将该数组定义为Song类型,例如...

Song[] songs;

Of course, you should also initialise it, so based on your example, you could use something like... 当然,您也应该初始化它,因此根据您的示例,您可以使用类似...

Song[] songs = new Song[4];
songs[0] = new Song();
songs[0].title = "Freebird";
songs[0].artist = "Lynyrd Skynryd";

songs[1] = new Song();
songs[1].title = "Sweet Home Alabama";
songs[1].artist = "Lynyrd Skynryd";

songs[2] = new Song();
songs[2].title = "Black or White";
songs[2].artist = "Michael Jackson";

songs[3] = new Song();
songs[3].title = "Smooth Criminal";
songs[3].artist = "Michael Jackson";

Remember, an array is a fixed length container. 请记住,数组是固定长度的容器。 It can only contain up to the number of elements you specify. 它最多只能包含您指定的元素数。

You can also assign values to an element, for example... 您还可以为元素分配值,例如...

Song a = new Song();
a.title = "Freebird";
a.artist = "Lynyrd Skynryd";

songs[0] = a;

Which would be the same as using... 这与使用...相同

Song a = new Song();
songs[0] = a;
songs[0].title = "Freebird";
songs[0].artist = "Lynyrd Skynryd";

or 要么

songs[0] = new Song();
songs[0].title = "Freebird";
songs[0].artist = "Lynyrd Skynryd";

The 0 element shares the same reference as a , so changing the properties of either a or songs[0] would effect the same object, until either reference was changed... 0元素与a共享相同的引用,因此更改asongs[0]的属性将影响同一对象,直到其中一个引用被更改为止。

You can find out more by having a read through the Arrays trail... 您可以通过阅读Arrays足迹来了解更多信息。

you can use ArrayList to store objects in an array. 您可以使用ArrayList将对象存储在数组中。

ArrayList<Song> arraySong = new ArrayList<Song>();

That will create an arraylist of Song object (but with no element currently) 这将创建一个Song对象的数组列表(但当前没有任何元素)

Song a = new Song();
a.title = "Freebird"
a.artist = "Lynyrd Skynryd"
arraySong.add(a);

now the arraylist arraySong has one Song object. 现在arraylist arraySong有一个Song对象。 and you can do the same for the rest of the Song objects. 您可以对其余Song对象执行相同的操作。

Don't use an array unless you can know or derive the number of elements at compile time. 除非您可以在编译时知道或导出元素数量,否则不要使用数组。

A collection of album tracks is a collection, so I'd use a Collection. 专辑曲目的集合是一个集合,所以我会使用一个集合。 Because there is no natural association between an album track and an index, I would use a Set rather than an ArrayList (but you could also use an ArrayList). 因为专辑曲目和索引之间没有自然的关联,所以我将使用Set而不是ArrayList(但是您也可以使用ArrayList)。

If you wrote your Song class to honor the equals() contract and to have a usable hashcode() function (... write your Song class to be immutable, too), then it would be convenient to represent your data as: 如果您编写Song类是为了履行equals()合同并具有可用的hashcode()函数(...也将Song类编写为不可变的),那么将数据表示为以下内容将很方便:

Set<Song> mySongs = new HashSet<>();

Even better than this, though, is to create a normalized set of data tables and put this data into a database via JDBC (with Song as your data model object). 但是,比这更好的方法是创建一组标准化的数据表,并通过JDBC(将Song作为数据模型对象)将这些数据放入数据库中。 That would also take care of persistence for you. 那也将为您保持持久性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM