简体   繁体   中英

How to pass ArrayList to the constructor?

I'm working in a project class and want to know how to pass an ArrayList to the constructor.

For example, I have an Item class with a child Class named CDRom (inheritance), then CDRom needs to have one or more authors so I thought that every object from CDRom will have to have their own ArrayList inside the object. But this is possible? And how?

At the moment I have this:

public CDRom(int codi, double preu, String titul, Date dataPublicacion, List<String> autors, int stock) {
    super(codi, preu, titul, dataPublicacion, autors);
    this.stock = stock;
}

public static boolean createCdrom() {
    boolean valid = true;
    
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
    LocalDate localDate = LocalDate.now();
    System.out.println(dtf.format(localDate)); 
    
    String[] arr = {"autor1","autor2","autor3"};
    
    //CDRom cdrom = new CDRom(count++, 34.99, "The Beattles", dtf.format(localDate), Arrays.asList("asd1","asd2"),50);
    CDRom cdrom = new CDRom(count++, 34.99, "The Beattles", dtf.format(localDate), Arrays.asList("asdd","asd"), 50);
    return valid;
}

在此处输入图像描述

See the constructor arguments order

public CDRom(int codi, double preu, String titul, Date datePublicacion, ArrayList<String> autors, int stock){

But you are passing arguments in wrong order, and also you are trying to pass something invalid in the place of ArrayList

CDRom cdrom = new CDRom(count++, 34.99, "The Beattles", dtf.format(localDate), 50, autors[]{"asdd", "asd"});

Follow the order

CDRom cdrom = new CDRom(count++, 34.99, "The Beattles", dtf.format(localDate), Arrays.asList("asdd","asd"), 50);

Note Always update the code in the post not the image

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