Does neone know the code instruction to store an array of CD objects. Each entry in the array has to be a single object describing a CD.

i.e: Artist Name , Album Name, Number of tracks

i've written the program out but i cannot seem to get the above right.
if you want to have a look at the full program its in my previous thread....


pleez pleez someone help me out i've been frustrated with this problem for two weeks now just cant seem to get it:

Dani AI

Generated

Short, practical approach (no repeat of code already posted): define a small CD class to hold artist/title/tracks, keep instances in a collection while you build them, and only convert to an array if you truly need a fixed-size structure. This avoids manual indexing headaches and the common null-reference trap.

A minimal CD class:

public class CD {
    private final String artist;
    private final String title;
    private final int tracks;

    public CD(String artist, String title, int tracks) {
        this.artist = artist;
        this.title = title;
        this.tracks = tracks;
    }

    public String getArtist() { return artist; }
    public String getTitle()  { return title; }
    public int getTracks()    { return tracks; }

    @Override
    public String toString() {
        return artist + " - " + title + " (" + tracks + ")";
    }
}

Collecting CDs and using them:

List<CD> collection = new ArrayList<>();
collection.add(new CD("Artist A", "Album A", 10));
collection.add(new CD("Artist B", "Album B", 12));

// Use the list directly or convert to an array when needed:
CD[] array = collection.toArray(new CD[0]);

Notes and troubleshooting:

  • As hinted, object references need actual instances before use; attempting to call methods on a null slot causes NullPointerException. Using a List removes the need to pre-allocate and manually fill slots.
  • For user input, build CD objects with parsed values and add() them to the collection rather than juggling indices.
  • Consider immutability (shown above) or add setters if you need editable objects. Implement equals/hashCode if you plan to search or de-duplicate.
  • If you must support persistence, serialize or write a simple CSV line per CD and parse it back into CD instances.

For : start with the CD class above and try the ArrayList approach — it simplifies storage and iteration while you get the rest of the program working.

Recommended Answers

All 2 Replies

Everything you need has been explained to you several times already. I don't see how regurgitating the same knowledge again will make any difference.

Suppose you want to create an array of <type> with size 1555.
<type> can stand for int, double, or String

The syntax is:

<type>[] nameOfTheArray = new <type>[1555];

This creates the 1555 empty slots. The actual contents depends on the type.

For example: an int array always starts with all 0's, a boolean array always starts with all false, a String arrray starts with all null. More generally, any Object array starts with all null.

//To make a CD object array of size 1555 write:
CD[] nameOfTheArray = new CD[1555];

//Then you must fill this array with some objects (right now its all null).
for(int i =0; i < nameOfTheArray.length; i++)
{
	//put a new CD object at position i
	nameOfTheArray[i] = new CD();
}

For more help,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.