Problem when trying to add an array of objects within another java array

Asked

Viewed 112 times

-3

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

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();
        }

2 answers

1

First, we see that the expression listaNotas.get(i).getProdutos() is repeated. Let’s put this into a variable to avoid repeating and also simplify your code a little more 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 instruction p.toArray(new Notas[p.size()]); is quite strange. It means you will convert a list of Produtos on a list of Notas.

Stranger still is the instruction that follows listaInsercaoXml[i] = arrayListaNotas;. It means that you are placing an array in a single position of listaInsercaoXml, which implies that listaInsercaoXml is a matrix with at least two dimensions, which is probably not the case.

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

Ah, remark: Don’t use FileWriter, because this class uses the standard machine encoding rather than having a user-defined encoding, leading to encoding incompatibility problems. There are even discussions/suggestions on the Oracle mailing lists to mark it as @Deprecated because of this. In her place, use OutputStreamWriter passing the Charset desired in the builder.

0


I’m not sure I understand the problem.

To convert from a List for 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 the object specification Notas... eventually will have to create an object for the representation of Notas in XML

  • to generate the xml I am using the Xstream API. I also already have a java object for notes that receives a list of product objects (I already have). But when I try to convert to array, I can’t recover the list of Products that each note has.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.