How to concatenate strings of a 3-in-3 vector in Java?

0

I need to concatenate the elements of a 3-by-3 vector.

Here's the sample I need:

package ex;

public class prog {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String[] vetor = {"aaa", "aaa", "aaa", "bbb", "bbb", "bbb", "ccc", "ccc", "ccc", "ddd", "ddd", "ddd"};

String[] novo_vetor; // vetor que terá as strings "aaaaaaaaa", "bbbbbbbbb", "ccccccccc", "ddddddddd");
    }

}
    
asked by anonymous 21.12.2016 / 03:05

1 answer

3

You can do this:

class HelloWorld {
    public static void main(String[] args) {
        String[] vetor = {"aaa", "aaa", "aaa", "bbb", "bbb", "bbb", "ccc", "ccc", "ccc", "ddd", "ddd", "ddd"};
        String[] novoVetor = new String[vetor.length / 3];
        for (int i = 0; i < vetor.length; i += 3) {
            novoVetor[i / 3] = vetor[i] + vetor[i + 1] + vetor[i + 2];
        }
        for (String item : novoVetor) {
            System.out.println(item);
        }
    }
}

See working on ideone and on CodingGround .

    
21.12.2016 / 03:23