Multidimensional array of different types

3

I have a view in the bank of my application where I count the user's name, his sector and the total number of records issued (tale from another table):

  

TotalPorUsuario (View) Table

     

Columns: name (string), sector (String), total (int)

I need to store these 3 information in an array and return from model to view and relate to a JTable , but I can not imagine how to create a% multi-dimensional%. I thought about Map but I do not know how it works.

My current code is (I could only do it with 2 columns):

 public List getTotal() {

        String query = "select nome,setor,total from TotalPorUsuario";
        List<ArrayList> lista1 = new ArrayList<>();
        ArrayList<String> l2 = new ArrayList<>();
        ArrayList<Integer> l3 = new ArrayList<>();

        try {
            this.con = ConnectionFactory.createConnection();
            this.pstm = this.con.prepareStatement(query);
            this.r = this.pstm.executeQuery();

            while (this.r.next()) {
                l2.add(this.r.getString("nome"));
                l3.add(this.r.getInt("total"));
            }
            lista1.add(l2);
            lista1.add(l3);

        } catch (SQLException ex) {
            ex.printStackTrace();
            throw new ExcecaoPadrao(ex.getMessage());
        } finally {
            try {
                ConnectionFactory.closeConnection(con, pstm, r);
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return lista1;
    }

What is the recommended way to return the three columns: in 3 arraylist or is there a less complex form using Arraylist ?

    
asked by anonymous 15.03.2016 / 15:20

3 answers

0

Thank you for the suggestions you have given me, which made me doubt about which way to use. I ended up opting to add an attribute in the user class because I would use the data to pop a JTable and already have a% custom_default, which can be reused for the TableModel class. This change seemed to me less expensive than implementing list within list or o Usuario .

public class Usuario{

    private int id;
    private String nome;
    private Setor setor;
    private boolean estaAtivo = true;
    private int totalDeRegistros;

    /** ... */

    public int getTotalDeRegistros() {
        return this.totalDeRegistros;
    }

    public void setTotalDeRegistros(int total) {
        this.totalDeRegistros = total;
    }

    /** ... */
}
    
16.03.2016 / 11:14
4

Diego,

As I saw you search the database for a view, which leads me to believe that the total ratio per user is something very special for you. I believe this would be the case of creating a class to represent your view in the application.

Something like:

class TotalPorUsuario{
    private Integer total;
    private Usuario usuario;
    //o resto que você precisa
    ...
}

and then simply make a list of objects that represent your view.

    
15.03.2016 / 15:52
3

Normally this is solved by creating a class to contain these two columns and then you put objects of these class in the list. It is a container inside another, first put in a simple data structure, then put in another which is a list of elements.

Since Java does not allow anonymous types in a simple way, the alternatives would be to create generic classes (tuples) that allow them to allocate these "columns" or do what they are doing. Example of tuple with 2 elements:

public class Tuple<X, Y> { 
    public final X x; 
    public final Y y; 
    public Tuple(X x, Y y) { 
        this.x = x; 
        this.y = y; 
    } 
}

You have something ready .

I think the tuple solution should be avoided in a language like Java. This works best in languages with less ceremony.

There are other gambiarras that I do not recommend and will not even mention, like the one quoted in comment.

    
15.03.2016 / 15:40