How to display the values of an Arraylist separately?

Asked

Viewed 3,237 times

-1

class Principal {
    public static void main(String[] args) {
        Scanner entrada = new Scanner(System.in);
        ArrayList colecao = new ArrayList();
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 2; j++) {
                System.out.println("Insira um nome.");
                colecao.add(entrada.next());
            }
        } for (Object resolucao:colecao) {
            String formato = "| %-15s | %-10s |%n";
            System.out.format("+---------------+----------+%n");
            System.out.printf("| Nome          | Nome 2   |%n");
            System.out.format("+---------------+----------+%n");
            System.out.format(formato, resolucao, resolucao + "%n");
            System.out.format("+---------------+----------+%n");
        }
    }
}

I need to display separately using a for-each and there’s the problem, how to display the right item in the right column?

  • Define "all together". resolucao will always be an object, you want to print a property of it?

  • I want to print the values of Arraylist exemplo using a for-each, e.g.: in the example index 0 has the string "lápis", in index 1 the string "caneta", when you print, you get "lápiscaneta", but I want to print separately, "lápis" and "caneta".

  • Console output.

  • I guess I was wrong to explain, actually, I need to separate a string into two, ex.: "lápiscaneta" which is at index 0 in "lápis", in index 0 and "caneta", in index 1. .

  • That code is exactly what I’m doing, the only difference is that in the this.valores += valores[i][j]: http://answall.com/questions/13488/arraylist-retornando-valores-nulos

  • 1

    Sort this.values += values[i][j] for this.values += values[i][j] + ' n' Or create a separate array and use arrayseparada.add( values[i][j] )

Show 1 more comment

1 answer

1


This answer was for an earlier version that the question already had.

The println should already break the lines, try the variant below if it is a platform difference problem:

ArrayList exemplo = new Arraylist();
for (Object resolucao: exemplo) {
   System.out.println(resolucao);
}

Based on your comment, you can split a string this way, if its source is not an array:

Exemplo = original.split("\\n");

for (Object resolucao: exemplo) {
   System.out.println(resolucao + "\r\n");
}

As for the line breaks:

To learn the line breaking of the platform in use, Java has the

String newLine = System.getProperty("line.separator");

Or from Java 7,

String newLine = System.lineSeparator();
  • I already tried to do it, but it prints all the indexes and only then skips the line.

  • What cool, besides everything they gave me an -1 without explanation.

  • He’ll break the line where there’s one "\n"?

  • @Anyway, I think it’s the case that you edit and fix the question by putting the exact code you’re using, otherwise it gets a lot of mess here on the site, and answers that don’t help anyone.

  • I mean, he’ll split the string where there’s one "\n"?

  • @Patrick yes, but if your string is "banana|monkey", just exchange the " n" for "|", for example. There goes a Regex parameter.

  • 3

    @Itwasn'tme to remember that by moving any post, you take it to the main page, which automatically causes users to see the post (and attracts votes for them on many occasions). Also, if any post already has votes, yours appears below them, which decreases the chance of being noticed. If you really think there is a real problem with the votes, I suggest posting on the [goal] in a well elaborated way the situation, thus brings the debate to the community as a whole. Here by the comments remains only my opinion, which ends up limiting the subject.

Show 2 more comments

Browser other questions tagged

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