【问题标题】:Instatiate Object inside for在内部实例化对象
【发布时间】:2018-11-20 23:25:09
【问题描述】:

我在 For 之外创建一个对象并像这样改变值:

NotaCaract dadosNota = aux.getListaNotasInicial().createNewNotaCaract();

    for(int i=0; i< saida.getListaCompletaProds().size(); i++){

        seqIdNota = saida.getListaCompletaProds().getDadosCompletosProd(i).getIdNota().getIntValue();
        dadosNota.getIdNota().setIntValue(seqIdNota);
        aux.getListaNotasInicial().addNotaCaract(dadosNota);

        seqNotaDesFornecedor = saida.getListaCompletaProds().getDadosCompletosProd(i).getProdutosLeasing().getSeqNotaDesFornecedor().getIntValue();
        dadosNota.getIdNota().setIntValue(seqNotaDesFornecedor);
        aux.getListaNotasInicial().addNotaCaract(dadosNota);

        seqNotaDesMorada = saida.getListaCompletaProds().getDadosCompletosProd(i).getProdutosLeasing().getSeqMorada().getIntValue();
        dadosNota.getIdNota().setIntValue(seqNotaDesMorada);
        aux.getListaNotasInicial().addNotaCaract(dadosNota);

        seqNotaMoradaFornecedor = saida.getListaCompletaProds().getDadosCompletosProd(i).getProdutosLeasing().getSeqNotaMoradaFornecedor().getIntValue();
        dadosNota.getIdNota().setIntValue(seqNotaMoradaFornecedor);
        aux.getListaNotasInicial().addNotaCaract(dadosNota);

        seqFinalidadeFinanciamento = saida.getListaCompletaProds().getDadosCompletosProd(i).getProdutosLeasing().getSeqFinalidadeFinanciamento().getIntValue();
        dadosNota.getIdNota().setIntValue(seqFinalidadeFinanciamento);
        aux.getListaNotasInicial().addNotaCaract(dadosNota);

        seqJustificacao = saida.getListaCompletaProds().getDadosCompletosProd(i).getProdutosLeasing().getSeqJustificacao().getIntValue();
        dadosNota.getIdNota().setIntValue(seqJustificacao);
        aux.getListaNotasInicial().addNotaCaract(dadosNota);

        seqObservacaoAmbitoProposta = saida.getListaCompletaProds().getDadosCompletosProd(i).getProdutosLeasing().getSeqObservacaoAmbitoProposta().getIntValue();
        dadosNota.getIdNota().setIntValue(seqObservacaoAmbitoProposta);
        aux.getListaNotasInicial().addNotaCaract(dadosNota);

    }

假设只有第一个实例的值为 1,所有其他值均为 0。 我的最终列表 aux.getListaNotasInicial() 将全为 0。

但是当我这样做时:

    for(int i=0; i< saida.getListaCompletaProds().size(); i++){

        NotaCaract dadosNota = aux.getListaNotasInicial().createNewNotaCaract();
        seqIdNota = saida.getListaCompletaProds().getDadosCompletosProd(i).getIdNota().getIntValue();
        dadosNota.getIdNota().setIntValue(seqIdNota);
        aux.getListaNotasInicial().addNotaCaract(dadosNota);

        NotaCaract dadosNotaDesFornecedor = aux.getListaNotasInicial().createNewNotaCaract();
        seqNotaDesFornecedor = saida.getListaCompletaProds().getDadosCompletosProd(i).getProdutosLeasing().getSeqNotaDesFornecedor().getIntValue();
        dadosNotaDesFornecedor.getIdNota().setIntValue(seqNotaDesFornecedor);
        aux.getListaNotasInicial().addNotaCaract(dadosNotaDesFornecedor);

        NotaCaract dadosNotaDesMorada = aux.getListaNotasInicial().createNewNotaCaract();
        seqNotaDesMorada = saida.getListaCompletaProds().getDadosCompletosProd(i).getProdutosLeasing().getSeqMorada().getIntValue();
        dadosNotaDesMorada.getIdNota().setIntValue(seqNotaDesMorada);
        aux.getListaNotasInicial().addNotaCaract(dadosNotaDesMorada);

        NotaCaract dadosNotaMoradaFornecedor = aux.getListaNotasInicial().createNewNotaCaract();
        seqNotaMoradaFornecedor = saida.getListaCompletaProds().getDadosCompletosProd(i).getProdutosLeasing().getSeqNotaMoradaFornecedor().getIntValue();
        dadosNotaMoradaFornecedor.getIdNota().setIntValue(seqNotaMoradaFornecedor);
        aux.getListaNotasInicial().addNotaCaract(dadosNotaMoradaFornecedor);

        NotaCaract dadosNotaFinalidadeFinanciamento = aux.getListaNotasInicial().createNewNotaCaract();
        seqFinalidadeFinanciamento = saida.getListaCompletaProds().getDadosCompletosProd(i).getProdutosLeasing().getSeqFinalidadeFinanciamento().getIntValue();
        dadosNotaFinalidadeFinanciamento.getIdNota().setIntValue(seqFinalidadeFinanciamento);
        aux.getListaNotasInicial().addNotaCaract(dadosNotaFinalidadeFinanciamento);

        NotaCaract dadosNotaJustificacao = aux.getListaNotasInicial().createNewNotaCaract();
        seqJustificacao = saida.getListaCompletaProds().getDadosCompletosProd(i).getProdutosLeasing().getSeqJustificacao().getIntValue();
        dadosNotaJustificacao.getIdNota().setIntValue(seqJustificacao);
        aux.getListaNotasInicial().addNotaCaract(dadosNotaJustificacao);

        NotaCaract dadosNotaObservacaoAmbitoProposta = aux.getListaNotasInicial().createNewNotaCaract();
        seqObservacaoAmbitoProposta = saida.getListaCompletaProds().getDadosCompletosProd(i).getProdutosLeasing().getSeqObservacaoAmbitoProposta().getIntValue();
        dadosNotaObservacaoAmbitoProposta.getIdNota().setIntValue(seqObservacaoAmbitoProposta);
        aux.getListaNotasInicial().addNotaCaract(dadosNotaObservacaoAmbitoProposta);

    }

我的最终列表 aux.getListaNotasInicial() 将具有我希望它具有的值。为什么我必须在循环内实例化多个对象才能使用它们的值,而我只能实例化 1 并根据需要更改他的值?

PS:我的平台只支持 Java 6 类型

【问题讨论】:

  • 在第一种情况下,您将多个对 same 对象的引用存储在列表中。而这个唯一的对象只能保存一个值,1 或 0。

标签: java loops object for-loop new-object


【解决方案1】:

当您实例化一个对象时,您会保留一个内存位置并将该对象(连同其值)存储在其中。例如,在第一种情况下,您只实例化一个对象,并为其保留一个内存位置。当您只实例化一个对象并为其更改值时,每次更改值时,您都会覆盖该值。

此外,当您只声明一个对象实例并将其多次添加到数组中时,您实质上是添加了同一个对象(内存位置)。因此,您的数组将仅包含对对象的最后一次已知修改,因为所有先前的数据都已被覆盖。这就是为什么您必须声明对象的多个实例以便保留多个内存位置并保存所有不同值的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-06
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多