How to fill an array of another class in JAVA

0

I have created a method inside a class that looks for RS in the database, it works perfectly, but when I use it in another class, it does not do the desired thing, see the method:

    public void preencherArrayCidades(ArrayList acidades) throws SQLException{  
con = Conectar ();

 String sqlStmt = "SELECT * FROM Trabalho01.cidades ORDER BY nome_cidade";  
 PreparedStatement stmt =con.prepareStatement(sqlStmt);
 ResultSet rs = stmt.executeQuery();
 stmt.execute(); 
    ArrayList cidades = new ArrayList(); 
 while(rs.next()){  
cidades.add(rs.getInt("id_cidade"));

}  

}

To get these values, I'm using this code:

    CidadeController cidadecontroller = new CidadeController();
Cidade cidade = new Cidade();
 ArrayList cidades = new ArrayList(); 
        try {
           cidadecontroller.preencherArrayCidades(cidades);
        } catch (SQLException ex) {
            Logger.getLogger(CidadeView.class.getName()).log(Level.SEVERE, null, ex);
        }

        JOptionPane.showMessageDialog(rootPane, cidades);
    
asked by anonymous 31.08.2015 / 17:40

3 answers

1

cidades.add(rs.getInt("id_cidade")); is adding an integer into the city array, so add a city.

Cidade cidade = new Cidade(rs.getInt("id_cidade"));
cidades.add(cidade);

You must have a constructor in the city class that receives an integer.

and it would be more convenient to create a method called listCharts instead of populatingArrayCities

public List<Cidade> listarCidades(){
   List<Cidade> cidades = new ArrayList();
   //lista as cidades adiocionando na lista de cidades
   return cidades
}
    
31.08.2015 / 17:54
0

Viewing your method signature:

public void preencherArrayCidades(ArrayList acidades)  

It gets an ArrayList that should be populated with the query result however what you are doing is creating a new array to receive that result.

Change method like this:

public void preencherArrayCidades(ArrayList acidades) throws SQLException{  
     con = Conectar ();

     String sqlStmt = "SELECT * FROM Trabalho01.cidades ORDER BY nome_cidade";  
     PreparedStatement stmt =con.prepareStatement(sqlStmt);
     ResultSet rs = stmt.executeQuery();
     stmt.execute(); 
     while(rs.next()){  
         acidades.add(rs.getInt("id_cidade"));
    }  
}

This is the ArrayList that is passed that is filled in.

Another way is to return the ArrayList method populated:

public ArrayList preencherArrayCidades() throws SQLException{  
     con = Conectar ();

     String sqlStmt = "SELECT * FROM Trabalho01.cidades ORDER BY nome_cidade";  
     PreparedStatement stmt =con.prepareStatement(sqlStmt);
     ResultSet rs = stmt.executeQuery();
     stmt.execute(); 
     ArrayList cidades = new ArrayList(); 
     while(rs.next()){  
         cidades.add(rs.getInt("id_cidade"));
    }  
    return cidades;
}

Then you can use it like this:

CidadeController cidadecontroller = new CidadeController();
Cidade cidade = new Cidade();
ArrayList cidades;
try {
    cidades = cidadecontroller.preencherArrayCidades();
} catch (SQLException ex) {
        Logger.getLogger(CidadeView.class.getName()).log(Level.SEVERE, null, ex);
}

JOptionPane.showMessageDialog(rootPane, cidades);
    
31.08.2015 / 17:57
0

There is something subtle going on and is related to the concepts of passing from parameter to reference and passing from parameter to value. Note that although it may look confusing, Java only has parameter passing by value, either the primitive type parameter or an object.

The method below is getting a reference to the type ArrayList.

public void preencherArrayCidades(ArrayList cidades)

When calling it, Java makes a copy of the foreign variable reference to the method for the cities parameter of the method. See the code below:

ArrayList cidades = new ArrayList();
cidadecontroller.preencherArrayCidades(cidades);

Therefore, Java copies the cities variable reference created outside the fillArrayCities parameter for the cities parameter declared in this method.

However, within the fillArrayCities method, you create another ArrayList, overriding the reference that the cities parameter was pointing to. That's the problem, since this reference will not be reflected in the cities variable outside of the fillArrayCities method. Remember that a copy of the reference was made.

In other words, when you do this, your cities variable outside the method continues to point to an empty ArrayList (created outside the method). And the cities parameter of the method is now pointing to another ArrayList, which is populated with the data.

Do as suggested by @ramaral and @Skywalker. Make a method that returns an ArrayList and not a method that receives an ArrayList as a parameter.

If you do not want to change, then do not instantiate an ArrayList within the fillArrayCities method. Remember that the reference copied to the cities parameter is from a newly created ArrayList outside the fillArrayCities method.

    
31.08.2015 / 18:05