Multidimensional array of different types

Asked

Viewed 1,110 times

2

I have a view in my application database where I account for the user’s name, his sector and the total number of records issued (another table’s count):

Table(View) Totalporusuario

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

I need to store this 3 information in one array and return from model for view and relate in a JTable, but I can’t imagine how to create a arraylist multidimensional. I thought about Map but I don’t know how it works.

My current code is (I could only do 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 there is a less complex way using map?

  • Diego F , use Object

  • @Rodrigosantiago did not understand.

  • Arraylist<Object> can contain any type, and even better Arraylist<Object[]> can contain all values recovered by Resultset. This is even true for primary data, java already converts to Integer, Boolean etc.

  • That I’m already doing, only I have to add one more column, there would be 3 arraylists within the list. I wonder if there is a way to do such a thing with arraylist or map in a more simplified way.

3 answers

4

Diego,

as I saw that you search the database for a view, which leads me to believe that the total relation per user is something very special for you. I believe that this would be the case to create 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 (Totalporusuario).

  • I already have the user model ready, but having to create a parameter and keep making getter and Setter to use a single time in a single method is wasted work.

  • 1

    @Diegof this is Java :)

  • @Diegof, maybe creating this class looks like wasted work, but it helps you keep things logical and easy to maintain, because I’ve already had to maintain code where I had to guess what that meant: lista[0][1][5]. And believe me, your future self will thank you for not using something like.

  • Rubico My present "me" is at this moment hating the "me" of the past for having invented this view hehe... But I’ll see how much I’ll have to change the program if I add it in the user model or using the @bigown tip, then I can opt for the less harmful solution.

3

Usually 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 within another, first puts into a simple data structure, then puts into another that is a list of elements.

Because Java doesn’t allow anonymous types simply, the alternatives would be to create generic classes (tuples) that allow you to allocate these "columns" or do what you’re 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; 
    } 
}

I put in the Github for future reference.

Has 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 I will not speak, as the cited in comment.

  • I even have a user class and another sector, and the user has sector, but the total column is what generates the problem. Only in this method will I use it. This tuple method, by chance, allows a generic third element, applying its same logic?

  • Allows as many as you want, you create as you want, on link have several ready for some quantities. But again, create a class of your own for this would be better.

  • Diego, if you already have the user class, it would not be the case to make a map between the user and the number of sectors?

0


I appreciate the suggestions you’ve made, they’ve taken away the question of how to use it. I chose to add an attribute in the user class, because I would use the data to popular a JTable and I already have one TableModel customized, which can be reused for the class Usuario. This change seemed less costly than implementing list within list or the Tuple.

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

    /** ... */
}

Browser other questions tagged

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