Problem trying to add an array of objects inside another array in java

-3

Hello, I have a list of Notes objects and need to save them in xml files. I'm trying to convert this list to arrays to add them to the xml file. However I am not able to do the conversion properly, because it is a list of Notes that receive another list of Products. What is the best solution?

List<Notas> listaNotas;
Notas[] arrayListaNotas;

for(int i=0; i<listaNotas.size();i++) {
            arrayListaNotas = 
            listaNotas.get(i).getProdutos().toArray(new 
            Notas[listaNotas.get(i).getProdutos().size()]);
            listaInsercaoXml[i] =arrayListaNotas;// Aqui acontece o erro na hora de tentar atribuir
}

    int qtdeNotasXml=5;         
        for(int i=0; i<listaInsercaoXml.length;i++) {
            stringXml = xstream.toXML(Arrays.copyOfRange(listaInsercaoXml, i, Math.min(listaInsercaoXml.length, i+qtdeNotasXml)));
            file = new PrintWriter(new BufferedWriter(new FileWriter("C:\notas"+i+".xml")));
            file.println(stringXml);
            file.close();
        }
    
asked by anonymous 16.08.2018 / 16:40

2 answers

0

I do not know if I got the problem right.

To convert from a List to an array is simple:

List<Notas> listaNotas = ...;
Notas[] arrayListaNotas;

arrayListaNotas = listaNotas.toArray(new Notas[0]);

But as it is to create XML, I think it will get more complicated - it depends on the framework you are using to create XML, and specifying the object Notas ... will eventually have to create an object for the representation of Notas in XML

    
16.08.2018 / 17:00
1

First, we see that the expression listaNotas.get(i).getProdutos() is repeated. Let's put this in a variable to avoid repeating and also simplifying your code a bit, by dividing long expressions into several short expressions stored in intermediate variables:

List<Notas> listaNotas;
Notas[] arrayListaNotas;

for (int i = 0; i < listaNotas.size(); i++) {
    Notas n = listaNotas.get(i);
    Produtos p = n.getProdutos();
    Notas[] arrayListaNotas = p.toArray(new Notas[p.size()]);
    listaInsercaoXml[i] = arrayListaNotas; // Erro
}

int qtdeNotasXml = 5;         
for (int i = 0; i < listaInsercaoXml.length; i++) {
    int m = Math.min(listaInsercaoXml.length, i + qtdeNotasXml);
    Object[] range = Arrays.copyOfRange(listaInsercaoXml, i, m);
    String stringXml = xstream.toXML(range);
    String nomeArquivo = "C:\notas" + i + ".xml";
    try (PrintWriter file = new PrintWriter(new BufferedWriter(new FileWriter(nomeArquivo)))) {
        file.println(stringXml);
    }
}

The p.toArray(new Notas[p.size()]); statement is quite strange. This means that you will convert a list of Produtos into a list of Notas .

Even stranger is the following statement listaInsercaoXml[i] = arrayListaNotas; . It means that you are putting an array in a single listaInsercaoXml position, which implies that listaInsercaoXml is an array with at least two dimensions, which is probably not the case.

Maybe you should replace these two lines with listaInsercaoXml[i] = arrayListaNotas; .

Ah, note: Do not use FileWriter , because this class uses the default encoding of the machine rather than having a user-defined encoding, leading to encoding incompatibility problems. There are even discussions / suggestions on Oracle's mailing lists to mark it as @Deprecated because of this. Instead, use OutputStreamWriter passing the Charset you want in the constructor.

    
16.08.2018 / 21:03