How to show values of an Array in a single message?

Asked

Viewed 171 times

0

package Algoritmos;

import javax.swing.*;

public class testinho {

    public static void main(String[] args) {
        int[][] numero_conta = new int[3][3];
        numero_conta[0][0] = 111;
        numero_conta[0][1] = 222;
        numero_conta[0][2] = 333;
        numero_conta[1][0] = 444;
        numero_conta[1][1] = 555;
        numero_conta[1][2] = 666;
        numero_conta[2][0] = 777;
        numero_conta[2][1] = 888;
        numero_conta[2][2] = 999;
        JOptionPane.showMessageDialog(null, numero_conta);
    }
}

I would like to know how to show all the values of my matrix without using one for to show a value at a time, and even if I need to tell exactly the position of the value (ex: numero_conta[1][0]), I want everything in the matrix to be shown at once.

2 answers

2


You can use the method deepToString of Arrays. This method transforms multidimensional arrays into strings.

Example:

String numeroConta = Arrays.deepToString(numero_conta);

JOptionPane.showMessageDialog(null, numeroConta);

1

Why don’t you want to wear one for? Loops are basic structures of the language, and one of its uses is to walk through a list or array, to do something with its elements.

You can even use the method Arrays.deepToString, as suggested in reply from @renanzin, but remember that this method also iterates through the array (i.e., you will be using for indirectly - just because you haven’t written a loop, doesn’t mean there isn’t one being used). Another detail is that in this case you have no control over the output format, and will have to accept what the method returns, which in this case is this String:

[[111, 222, 333], [444, 555, 666], [777, 888, 999]]

If you want to change the format in which the data is returned, there is no way, have to use a loop and mount the string the way you need it. Example:

StringBuilder sb = new StringBuilder();
for (int[] linha : numero_conta) {
    for (int elemento : linha) {
        sb.append(elemento).append(' ');
    }
    sb.append('\n');
}
JOptionPane.showMessageDialog(null, sb.toString());

I use a StringBuilder to go riding the String, for he is more efficient for making concatenations of strings in a loop. I also used the syntax of foreach, so does not need the indexes (because in this case I am only interested in the elements).

In this case, I used a space to separate the elements, and added a line break (\n) to separate rows from the matrix (but you can change the format the way you think best). The output is:

111 222 333 
444 555 666 
777 888 999 

The detail is that this code adds a space at the end of each line (after the last element), plus a line break after the last line. In the JOptionPane you may not be able to understand this, but if you want to be more detailed and not put these spaces and this line more, then you need the indexes to know when to put them or not:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < numero_conta.length; i++) {
    for (int j = 0; j < numero_conta[i].length; j++) {
        sb.append(numero_conta[i][j]);
        if (j != numero_conta[i].length - 1) // não é o último elemento da linha
            sb.append(' ');
    }
    if (i != numero_conta.length - 1) // não é a última linha
        sb.append('\n');
}

Now the space is not added at the end of the line, and the line break is not added after the last line.

  • But what if I had 3 matrices and needed to show all values with an order, the order would be (numbered, number, balance)? The reason I can’t use a for is that I need to show all the values at once.

  • @Then you’re changing the question, because you don’t have any of that in your code. But basically you loop these matrices and concatenate the strings with Stringbuilder the way you need it. If that’s the case, ask another question giving all the details (how are these matrices, how do you want the output, etc)

  • I need to wait 3 more days before asking another question, but I’ll wait to ask, thank you

Browser other questions tagged

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