How to fill an array of another class in JAVA

Asked

Viewed 1,265 times

0

Guys, I created a method within a class that searches RS in the database, it works perfectly, but when I use it in another class, it does not do what you want, 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);

3 answers

1

cidades.add(rs.getInt("id_cidade")); is adding an integer within the city array, it is right to add a city.

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

You must have a builder in the city class who gets a whole.

and it would be more convenient to create a method called listingCities instead of fillingArrayCities

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

0

Seeing the signature of your method:

public void preencherArrayCidades(ArrayList acidades)  

he gets a Arraylist to be completed with the result query however what you are doing is creating a new array to receive this result.

Change the 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"));
    }  
}

That way is the Arraylist that is past that is filled.

Another way is to return the method Arraylist filled:

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

0

There is something subtle happening and has relation with the concepts of passing parameter by reference and passing parameter by value. Note that although it may seem confusing, Java only has parameter-by-value passage, either the primitive type parameter or an object.

The method below is receiving a reference for the Arraylist type.

public void preencherArrayCidades(ArrayList cidades)

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

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

So Java copies the variable reference cities maid outside the fillArrayCities for the parameter cities stated in this method.

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

In other words, by doing this, your variable cities external to the method continues pointing to an empty Arraylist (created outside the method). And the parameter cities method is now pointing to another Arraylist, this filled with data.

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

If you don’t want to change, then just don’t instantiate an Arraylist within the method fillArrayCities. Remember that the reference copied to the parameter cities is a newly created Arraylist outside the method fillArrayCities.

Browser other questions tagged

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