Home > Net >  Create a collection class that contains a list
Create a collection class that contains a list

Time:10-20

I have a class that contains information:

class Artists {
    int id;
    String description;
    int year;

    void insert(int i, String d, int y) {
        id = i;
        description = d;
        year = y;
    }

    void display() {
        System.out.println(id   description   year);
    }
}

public class ArtistsRunner {
    public static void main(String[] args) {
        Artists a1 = new Artists();
        Artists a2 = new Artists();
        Artists a3 = new Artists();

        a1.insert(23, "An", 2015);
        a2.insert(13, "Mon", 2089);
        a3.insert(11, "Gla", 2013);

        a1.display();
        a1.display();
        a3.display();
    }
}

I want to make another java class with a collection class of the list of artists. How would I retrieve info from the artists class , make that into a list in my new java class?

I think I have to:

  1. declare a static collection field in my class with the main method
  2. create a new artist, add it to that collection

Is this correct? How would I do this? (still very new to Java)

CodePudding user response:

Declare a List, and put your Artist instances into it.

List<Artists> myArtists = new ArrayList<>();
myArtists.add(a1);
myArtists.add(a2);
myArtists.add(a3);

Since your requirement is to create a "static" list (probably for an assignment because you'd never want to do this in real life), declare the list at the class level and populate it (add statements) in main.

You can add any (reasonable) number of artists to this list using the add method as you create new artists instances.


Beyond that, you've got some basic issues with the design of your classes. While they work, they're not necessarily correct.

Your "Artists" class contains information for only a single artist, so the class should be called Artist (singular.) Further, the insert method is populating that artist, and should be a constructor.

public class Artist {
  private int id;
  private String description;
  private int year;

  public Artist(int id, String description, int year) {
    this.id = id;
    this.description = description;
    this.year = year;
  }

  public void display() {
    System.out.println(id   description   year);
  }
}

Note your display method is a bit weird -- there's no spaces between those variable values.

You'd now create a new artist like this:

Artist a1 = new Artist(1, "Bob", 2010);

This makes the "populate the list" exercise even easier, since you can do it in one line:

List<Artist> myArtists = new ArrayList<>();
myArtists.add(new Artist(1, "Bob", 2010));

Also, unless your assignment specifically asks for a "runner" class, you could just stick the main method right on the Artist class itself.

CodePudding user response:

Your main method could be reduced to this:

record Artist ( int id , String description , Year year ) {}
List < Artist > artists = 
    List.of
    (
        new Artist ( 23, "An", Year.of( 2015 ) ) ,
        new Artist ( … ) ,
        new Artist ( … ) 
    )
;
System.out.println( artists ) ;

A record is a brief way to define a class whose main purpose is to communicate data transparently and immutably. A record can be defined locally, within a method.

java.time.Year is a class for representing a year.

  •  Tags:  
  • java
  • Related