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

I have a constant 2d int array which is declared as:

int blocked[][] = new int[][] { { 0, 4 }, { 2, 2 }, { 3, 1 }, { 3, 3 } };

However, what I would like to do is to generate the exact array dynamically. So I created an arraylist where I add each integer separately, 0,4,2,3...N. Then I use for loop to go through to arraylist and create a 2d array. But somehow I just can't get it to work. I can't figure out where I'm doing wrong.

        ArrayList<Integer> blocklist = new ArrayList<Integer>();


    int blocked[][];

    blocklist.add(0);
    blocklist.add(4);
    blocklist.add(2);
    blocklist.add(2);
    blocklist.add(3);
    blocklist.add(1);
    blocklist.add(3);
    blocklist.add(3);

    blocked = new int[blocklist.size()][2];


    for(int i=0; i+2 < blocklist.size(); i++){
        blocked[i][0] = blocklist.get(i);
        blocked[i][1] = blocklist.get(i+1);
    }


    for(int i=0;i<blocked.length;++i){
        System.out.print(blocked[i][0]);
    }

When I do Arrays.deepToString(blocked); I get [[0, 0], [4, 4], [2, 2], [2, 2], [3, 3], [1, 1], [3, 3], [3, 3]] but it should be [[0, 4], [2, 2], [3, 1], [3, 3]]

share|improve this question
    
for(int i=0; i< blocklist.size(); i+=2){ //body } – Tipu Feb 10 at 13:42
1  
unfortunately that doesn't work. outputs 00203030 – PRCube Feb 10 at 13:48
up vote 2 down vote accepted
ArrayList<Integer> blocklist = new ArrayList<Integer>();

        int blocked[][];

        blocklist.add(0);
        blocklist.add(4);
        blocklist.add(2);
        blocklist.add(2);
        blocklist.add(3);
        blocklist.add(1);
        blocklist.add(3);
        blocklist.add(3);



        blocked = new int[(blocklist.size()/2)][2];

        for (int i = -1, j = 0,k=0; k < blocklist.size(); j++) {
            blocked[j][0] = blocklist.get(++i);
            blocked[j][1] = blocklist.get(++i);
            k+=2;
        }

        for (int i = 0; i <(blocklist.size()/2);i++) {
            for (int j = 0; j < 2; ++j) {
                System.out.print(blocked[i][j]);
            }
        }

    System.out.println();
    String deepToString = Arrays.deepToString(blocked);
    System.out.println("string: "+deepToString);

output: 
04223133
string: [[0, 4], [2, 2], [3, 1], [3, 3]]
share|improve this answer
    
Unfortunately doesn't work. When I do Arrays.deepToString(blocked); I get [[0, 0], [4, 4], [2, 2], [2, 2], [3, 3], [1, 1], [3, 3], [3, 3]] but it should be [[0, 4], [2, 2], [3, 1], [3, 3]] – PRCube Feb 10 at 14:13
    
please check again..copy code and run it. – Tipu Feb 10 at 14:17
    
I got the correct output – Tipu Feb 10 at 14:17
    
Yep, that works perfectly. Thank you very much I see what I have done wrong now. – PRCube Feb 10 at 14:19
    
welcome dud . :) – Tipu Feb 10 at 14:20

Try this:

int j = 0

for (int i=0; i < blocklist.size(); i++) {
    blocked[i][0] = blocklist.get(j);
    blocked[i][1] = blocklist.get(j+1);

    j+=2;
}
share|improve this answer
    
Unfortunately doesn't work. When I do Arrays.deepToString(blocked); I get [[0, 0], [4, 4], [2, 2], [2, 2], [3, 3], [1, 1], [3, 3], [3, 3]] but it should be [[0, 4], [2, 2], [3, 1], [3, 3]] – PRCube Feb 10 at 14:13
    
This should definitely work, glad to see you resolved your problem anyway. – James Feb 10 at 14:27

Your issue is in the for loop. i+2 increments i by 2 on each iteration of the loop.

It should be:

for(int i=0; i < blocklist.size(); i++){
    blocked[i][0] = blocklist.get(i);
    blocked[i][1] = blocklist.get(i);
}
share|improve this answer
    
Unfortunately doesn't work. When I do Arrays.deepToString(blocked); I get [[0, 0], [4, 4], [2, 2], [2, 2], [3, 3], [1, 1], [3, 3], [3, 3]] but it should be [[0, 4], [2, 2], [3, 1], [3, 3]] – PRCube Feb 10 at 14:12

solved

    blocked = new int[blocklist.size()][2];

for(int i=0; i < blocklist.size(); i++){
blocked[i][0] = blocklist.get(i);
blocked[i][1] = blocklist.get(i);

}

for(int i=0;i<blocked.length;++i){
    System.out.print(blocked[i][0]);
}
share|improve this answer

this code changes int[][] to ArrayList<>Integer> and vice versa irrespective of length, the code is also available on github

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created by Pankaj Nimgade on 10-02-2016.
 */
public class TestDriveInteger {

    public static void main(String[] args) {
        int blocked[][] = new int[][]{{0, 4}, {2, 2}, {3, 1}, {3, 3}};
        ArrayList<Integer> list = toArrayList(blocked);

        System.out.println("########### Converted integer List ##############");

        for (Integer integer : list) {
            System.out.print(integer + " ");
        }

        int[][] source = toIntegerArray(list);

        System.out.println("########### Converted int array ##############");
        for (int i = 0; i < source.length; i++) {
            for (int j = 0; j < source[i].length; j++) {
                System.out.print(source[i][j]+" ");
            }
        }
        System.out.println(source.length);
    }

    private static int[][] toIntegerArray(ArrayList<Integer> arrayList) {
        int[][] block = new int[arrayList.size() / 2][2];
        System.out.println(block.length);
        if ((arrayList.size() % 2) == 0) {

           int count = arrayList.size()/2;

            Iterator<Integer> integerIterator = arrayList.iterator();

            for (int i = 0; i < count; i++) {
                block[i][0] = integerIterator.next();
                block[i][1] = integerIterator.next();
            }

        } else {
            System.out.println("it is not a even size");
        }

        return block;
    }

    private static ArrayList<Integer> toArrayList(int[][] integerArray) {
        ArrayList<Integer> integerArrayList = new ArrayList<>();
        for (int i = 0; i < integerArray.length; i++) {
            for (int j = 0; j < integerArray[i].length; j++) {
                integerArrayList.add(integerArray[i][j]);
            }
        }
        return integerArrayList;
    }
}

output

########### Converted integer List ##############
0 4 2 2 3 1 3 3 4
########### Converted int array ##############
0 4 2 2 3 1 3 3 4
share|improve this answer

Change your code like this:

import java.util.ArrayList;

public class Simple {

public static void main(String[] args) {

    ArrayList<Integer> blocklist = new ArrayList<Integer>();

    blocklist.add(0);
    blocklist.add(4);
    blocklist.add(2);
    blocklist.add(2);
    blocklist.add(3);
    blocklist.add(1);
    blocklist.add(3);
    blocklist.add(3);

    int length = blocklist.size() / 2;

    int blocked[][];
    blocked = new int[ length ][2];

    for(int i=0, j=0; i < length; i++) {
        blocked[i][0] = blocklist.get(j++);
        blocked[i][1] = blocklist.get(j++);
    }

    for(int i=0;i<length;i++){
        System.out.print(blocked[i][0] + " " + blocked[i][1] + "\n");
    }
}

}

share|improve this answer
    
after that you will get: [0 4] [2 2] [3 1] [3 3] – Akbar Begimqulov Feb 10 at 14:42

Try this one:

   for(int i=0,j=0; j < (blocklist.size()/2); i+=2,j++){
        blocked[j][0] = blocklist.get(i);
        blocked[j][1] = blocklist.get(i+1);
   }
   for(int i=0;i<blocked.length/2;++i){
        System.out.print(blocked[i][0]);
        System.out.print(blocked[i][1]);
   }

Working for me :)

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.