append a class array
in
Programming Questions
•
2 years ago
First i will explain why i have this structure in mind, i got a list of songs that recharted the top100 during the years.
So 'Frank Sinatra' with the song 'white christmas' appears 3 times for example, 1944, 1945, 1946.
(all this is to draw a visual graphic in the end, in case it matters).
I have a class Song, it holds the title and the artist name and a class array charted which holds the unique information for every year the song recharted.
The class charted:
Now here everything gets together, it works apart for the if line,
There the charted info needs to be added.
In the else i use this:
s.charted[0] = new Charted(weeksCharted, dateEntered);
which works fine but afcose only works once.
For the if part i tried to append the array but i doens work with the syntax i'm trying.
So 'Frank Sinatra' with the song 'white christmas' appears 3 times for example, 1944, 1945, 1946.
(all this is to draw a visual graphic in the end, in case it matters).
I have a class Song, it holds the title and the artist name and a class array charted which holds the unique information for every year the song recharted.
- class Song {
String title;
String artist;
Charted[] charted = new Charted[1];
Song(String title, String artist) {
this.title = title;
this.artist = artist;
}
}
The class charted:
- class Charted {
int weeksCharted;
Date dateEntered;
// empty constructor
Charted () {
}
Charted(int weeksCharted, Date dateEntered) {
this.weeksCharted = weeksCharted;
this.dateEntered = dateEntered;
}
}
Now here everything gets together, it works apart for the if line,
There the charted info needs to be added.
In the else i use this:
s.charted[0] = new Charted(weeksCharted, dateEntered);
which works fine but afcose only works once.
For the if part i tried to append the array but i doens work with the syntax i'm trying.
- public void setupSongs() {
for (int i = 1; i < lines.length; i++) { //skip first, first one are the column names
String[] tokens = splitTokens(lines[i], "~");
String title = tokens[TITLE];
String artist = tokens[ARTIST];
String combined = artist+title;
int weeksCharted = int(tokens[CH]);
Date dateEntered = stringToDate(tokens[DATE_ENTERED]);
if (songs_hm.containsKey(combined)) {
// Get the object and add the charted info
Song s = (Song) songs_hm.get(combined); - //s.charted = append(s.charted, new Charted(weeksCharted, dateEntered)); //not working
}
else {
// Otherwise make a new song and add the charted info
Song s = new Song(title, artist);
s.charted[0] = new Charted(weeksCharted, dateEntered);
//s.charted = append(s.charted, new Charted(weeksCharted, dateEntered)); //not working
// add to the HashMap
songs_hm.put(combined,s);
}
}
}
1