java – Get array values ​​returned from another class

Question:

I need to pass the value of the variable sgEstado to the other class but I can't, the value is in the return when I debug , but I can't get the value out in the other class.

 ArrayList estados;
            try {
                Session session = InitSessionFactory.getInstance().getCurrentSession();
                estados = (ArrayList) session.createCriteria(FilialComplementoTO.class).addOrder(Order.asc("sgEstado")).list();

            } catch (Exception e) {
                Logger.getLogger(this.getClass().getName()).error(e.getMessage());
                throw new IntegrationException(e);
            }
            return estados;
        }

Second code, if the value of sgEstado leaves in the "passed" below, it's great, Grateful.

try {
            ctx.get(FilialComplementoTO.FILIAL_COMPLEMENTO_KEY);
            //FilialComplementoTO filialComplementoTO = (FilialComplementoTO) ctx.get(FilialComplementoTO.FILIAL_COMPLEMENTO_KEY);
            LocalizarLojasCompositeEntity localizarLojasCompositeEntity = new LocalizarLojasCompositeEntity();
            localizarLojasCompositeEntity.findEstadosBySgEstado();
            System.out.println(localizarLojasCompositeEntity.findEstadosBySgEstado());
            System.out.println("passou");
}

Answer:

It seems to me that what you need to do is just scan the array returned by the method that provides the state list:

try {
    ctx.get(FilialComplementoTO.FILIAL_COMPLEMENTO_KEY);
    //FilialComplementoTO filialComplementoTO = (FilialComplementoTO) ctx.get(FilialComplementoTO.FILIAL_COMPLEMENTO_KEY);
    LocalizarLojasCompositeEntity localizarLojasCompositeEntity = new LocalizarLojasCompositeEntity();
    estados = localizarLojasCompositeEntity.findEstadosBySgEstado();
    for(string estado : estados) {
        System.out.println(estado);
    }
    System.out.println("passou");
}

I put it on GitHub for future reference.

I guessed that in the ArrayList of estados there are strings . If not, you would need to change the type to no for in and if the type is another data structure, get the specific element you want to print.

Scroll to Top