import java.util.ArrayList; import java.util.Collections; public class Catalog { //felt-variable private static ArrayList<CD> archive; //constructor public Catalog() { archive = new ArrayList<CD>(); } // En masse mellemliggende kode uden nogen relevans for spørgsmålet :P //main-metoden der demonstrerer vores kode public static void main(String[] args) { //Opret objekter af typen Catalog og af typen CD. ArrayList<String> katalog = new ArrayList<String>(); } }
import java.util.ArrayList; import java.util.Collections; public class Catalog { //felt-variable public ArrayList<CD> archive; //constructor public Catalog() { archive = new ArrayList<CD>(); } // En masse mellemliggende kode uden nogen relevans for spørgsmålet :P //main-metoden der demonstrerer vores kode public static void main(String[] args) { //Opret objekter af typen Catalog og af typen CD. ArrayList<Catalog> katalog = new ArrayList<Catalog>(); katalog.add(new Catalog()); katalog.get(0).archive.add(new CD("Min Første CD")); } }
import java.util.ArrayList; import java.util.concurrent.TimeUnit; public class Catalog { private ArrayList<CD> archive = new ArrayList<CD>(); public void addCD(CD cd){ this.archive.add(cd); } public int getSize(){ return this.archive.size(); } public CD getCDWithAlbum(String album){ for(CD c: archive){ if(c.getAlbum().equals(album)){ return c; } } return null; } public ArrayList<CD> getCDsWithArtist(String artist){ ArrayList<CD> results = new ArrayList<CD>(); for(CD c : archive){ if(c.getArtist().equals(artist)){ results.add(c); } } return results; } public static void main(String[] args) { Catalog archive = new Catalog(); //Der bliver tilføjet en CD til Kataloget CD cd = new CD("IMMORTAL", "Michael Jackson"); //Der bliver tilføjet en sang til CD'en cd.addSong(new Song("Workin' Day and Night",TimeUnit.MINUTES.toMillis(5)+TimeUnit.SECONDS.toMillis(14))); //CD'en bliver nu tilføjet til kataloget archive.addCD(cd); System.out.println("CD arkivet er nu oppe på " + archive.getSize() + " CD'er"); System.out.println("CD'erne med Michael Jackson hedder : " + archive.getCDsWithArtist("Michael Jackson")); System.out.println("Antallet af sange på albumet IMMORTAL : " + archive.getCDWithAlbum("IMMORTAL").getSize()); } }
import java.util.ArrayList; public class CD { private String album, artist; private ArrayList<Song> songs = new ArrayList<Song>(); public CD(String album, String artist){ this.album = album; this.artist = artist; } public void addSong(Song song){ this.songs.add(song); } public Song getSongWithName(String name){ for(Song s: songs){ if(s.name.equals(name)){ return s; } } return null; } public String getAlbum(){ return this.album; } public String getArtist(){ return this.artist; } public int getSize(){ return this.songs.size(); } @Override public String toString() { return this.album + " by : " + this.artist; } }
public class Song { //in miliseconds long duration; String name; public Song(String name, long duration){ this.duration = duration; this.name = name; } public String getName(){ return this.name; } public long getDuration(){ return this.duration; } }
ArrayList<String> list = new ArrayList<String>(); list.add(new String("Hej")); list.add("test"); System.out.println(Arrays.toString(list.toArray()));