Home > Mobile >  How do I insert an ClassName[] Array into an arraylist of the same ClassName?
How do I insert an ClassName[] Array into an arraylist of the same ClassName?

Time:09-02

Im trying to add an Array of Classname[] into an ArrayList. But i get this error: "The method add(FacturacionClass) in the type ArrayList is not applicable for the arguments (FacturacionClass[])"

Here is the ArrayList and the insertion constructor ive tried.

ArrayList<FacturacionClass> FacturacionData = new ArrayList<FacturacionClass>();

Constructor:

FacturacionClass(String clientName, String clientID, FacturacionClass[] array, int cantProd){
    this.clientName = clientName;
    this.clientId = clientID;
    FacturacionData.add(array);
}

CodePudding user response:

You've specified FacturacionData to hold elements of type FacturacionClass, so naturally you cannot add an array of FacturacionClass to it.

Rather you probably want ArrayList.addAll.

FacturacionData.addAll(Arrays.asList(array));

CodePudding user response:

FacturacionData.add(new ArrayList<>(Arrays.asList(array))

Arrays.asList() method produces an unmodifiable list, so you will not be able to add any new element to it.

  •  Tags:  
  • java
  • Related