Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

i have problem how to split my array to make zone

my array is :

            1,   2,  3,  4,  5,  6
            7,   8,  9, 10, 11, 12
            13, 14, 15, 16, 17, 18
            19, 20, 21, 22, 23, 24
            25, 26, 27, 28, 29, 30
            31, 32, 33, 34, 35, 36

if i set row=3 and column 3, result must:

 1  2  3
 7  8  9
13 14 15
 4  5  6
10 11 12
16 17 18
19  20  21
25  26  27
31  32  33
22 23 24
28 29 30
34 35 36

my problem is, only get

 1  2  3
 7  8  9
13 14 15

this is my source code

    int dataArray[][] = new int[][]{
        {1, 2, 3, 4, 5, 6},
        {7, 8, 9, 10, 11, 12},
        {13, 14, 15, 16, 17, 18},
        {19, 20, 21, 22, 23, 24},
        {25, 26, 27, 28, 29, 30},
        {31, 32, 33, 34, 35, 36}
    };


    int row = 3;
    int column = 3;

    int zone = 4;
    int copyArray[][] = new int[row][column];


    int countJ = 0, countK = 0;

    for (int j = 0; j < row; j++) {
        for (int k = 0; k < column; k++) {
            copyArray[countJ][countK] = dataArray[j][k];
            copyArray[countK][countJ] = dataArray[k][j];
            countK++;
        }
        countK = 0;
        countJ++;
    }

    for (int i = 0; i < zone; i++) {
        System.out.println(Arrays.deepToString(copyArray));
    }

}

Thank you Advance

share|improve this question
    
Ehm what is the question? – Milkmaid Mar 25 at 7:48
    
You say what is the actual result but what is expected? – Sergey Pauk Mar 25 at 7:49
    
Doesn't your source code give your the correct result? In case I gives wrong result what's the problem. Please explain your problem. – Razib Mar 25 at 7:49
    
i dont know how to get the all result, just 1 zone i can.. another zone still failed – Jhohannes Purba Mar 25 at 7:51
    
Your algorythm is totally wrong. This problem is little bit more complex than you thought. take a look at this sub Matrix – Milkmaid Mar 25 at 8:01

the case had been solve..

  int dataArray[][] = new int[][]{
        {1, 2, 3, 4, 5, 6},
        {7, 8, 9, 10, 11, 12},
        {13, 14, 15, 16, 17, 18},
        {19, 20, 21, 22, 23, 24},
        {25, 26, 27, 28, 29, 30},
        {31, 32, 33, 34, 35, 36}};

    int baris = 3;
    int kolom = 3;
    int lebarZona = 2;
    int tinggiZona = 2;
    int counterTinggi = 0;
    ArrayList<int[][]> daftarZona = new ArrayList<int[][]>();
    counterTinggi = 0;
    int counterBaris = 0;
    int counterKolom = 0;

    for (int i = 0; i < tinggiZona; i++) {
        for (int j = 0; j < lebarZona; j++) {
            int copyArray[][] = new int[baris][kolom];

            for (int k = 0; k < baris; k++) {
                System.arraycopy(dataArray[counterBaris], counterKolom, copyArray[k], 0, kolom);
                counterBaris++;
            }

            daftarZona.add(copyArray);
        }
        counterBaris = 0;
        counterKolom += kolom;
    }

    for (int i = 0; i < daftarZona.size(); i++) {
        System.out.println(Arrays.deepToString(daftarZona.get(i)));
    }



    /*
     * Result : 
     * [[1, 2, 3], [7, 8, 9], [13, 14, 15]]
     *[[19, 20, 21], [25, 26, 27], [31, 32, 33]]
     *[[4, 5, 6], [10, 11, 12], [16, 17, 18]]
     *[[22, 23, 24], [28, 29, 30], [34, 35, 36]]
     */

thank you all

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.