从arraylist

时间:2016-02-10 13:39:40

标签: java arrays arraylist

我有一个常量的2d int数组,声明为:

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

但是,我想要做的是动态生成确切的数组。所以我创建了一个arraylist,我分别添加每个整数,0,4,2,3 ... N.然后我使用for循环来进入arraylist并创建一个2d数组。但不知何故,我无法让它发挥作用。我无法弄清楚我在哪里做错了。

        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]);
    }

当我做Arrays.deepToString(被阻止); 我得到[[0, 0], [4, 4], [2, 2], [2, 2], [3, 3], [1, 1], [3, 3], [3, 3]],但它应该是[[0, 4], [2, 2], [3, 1], [3, 3]]

7 个答案:

答案 0 :(得分:2)

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]]

答案 1 :(得分:1)

试试这个:

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;
}

答案 2 :(得分:0)

您的问题出在for循环中。在循环的每次迭代中,i + 2将i递增2。

应该是:

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

答案 3 :(得分:0)

解决

    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]);
}

答案 4 :(得分:0)

此代码将int[][]更改为ArrayList<>Integer>,反之亦然,无论长度如何,代码也可在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;
    }
}
  

输出

########### Converted integer List ##############
0 4 2 2 3 1 3 3 4
########### Converted int array ##############
0 4 2 2 3 1 3 3 4

答案 5 :(得分:0)

更改您的代码:

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");
    }
}

}

答案 6 :(得分:0)

试试这个:

   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]);
   }

为我工作:)