简体   繁体   中英

How to store objects in an array to be used later?

I was curious how to store java objects in an array. 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]; .. 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[] 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...

You can find out more by having a read through the Arrays trail...

you can use ArrayList to store objects in an array.

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

That will create an arraylist of Song object (but with no element currently)

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

now the arraylist arraySong has one Song object. and you can do the same for the rest of the Song objects.

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

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:

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). That would also take care of persistence for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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