将int转换为int数组

时间:2017-11-28 08:51:39

标签: java arrays type-conversion

现在我有另一个问题。 我需要制作CNF公式并使用3d数组的整合图。 现在我尝试使用这个任务,不同的是插入一个3d数组,而不是2d。

我是用柱子做的(它是2星(**))并且它有效。

使用块(你可以看到*********星),eclipse说"不能将fron int转换为[] int"

为什么写这个? 在"列中它没有"。

我希望我很清楚。

public static void encode(int sqrtN, int[][] hints, int[][][] map) {
    int[] vars = new int [sqrtN*sqrtN]; 
    int nVars = sqrtN*sqrtN*sqrtN*sqrtN*sqrtN*sqrtN;


**  //each cell in the columns gets a different value.**
    for(int i=0; i<sqrtN*sqrtN; i=i+1){
        for(int j=0; j<sqrtN*sqrtN; j=j+1) {
            for(int k=0; k<sqrtN*sqrtN; k=k+1) 
                vars[k] = columns(map)[i][k][j];

            SATSolver.addClauses(exactlyOne(vars));
        }   
    }

    *****//each cell in the blocks gets a different value.******
    for(int i=0; i<sqrtN*sqrtN; i=i+1){
        for(int j=0; j<sqrtN*sqrtN; j=j+1) {
            for(int k=0; k<sqrtN*sqrtN; k=k+1) 
                vars[k] = blocks(map,sqrtN)[i][k][j];

            SATSolver.addClauses(exactlyOne(vars));
        }   
    }
    //hints.
    int [] hintsSATS = new int[1];
    for (int i = 0; i < hints.length; i = i + 1) {
        hintsSATS[0] = varName(hints[i][0],hints[i][1],hints[i][2]-1,sqrtN*sqrtN);
        SATSolver.addClause(hintsSATS);
    }

}

 //A function to trunsfer to columns.
public static int[][][] columns (int[][][] matrix) {
    int[][][] columns = new int[matrix.length][matrix.length][matrix.length];
    for(int j=0; j<matrix.length; j= j+1){
        for(int i=0; i<matrix.length; i= i+1){
            columns[j][i] = matrix[i][j];
        }
    }
    return columns;
}

  ************  //A function to trunsfer to blocks.**********
public static int[][][] blocks (int[][][] matrix, int sqrtN) {  
    int[][][] blocks = matrix;
    for (int i = 0; i<matrix.length; i= i+1) {
        for (int j = 0; j<matrix.length; j= j+1) {
            blocks[i][j] = j%sqrtN+ i%sqrtN*sqrtN + 1;
        }
    }
    return blocks;
}




//The name of the variable.
public static int varName(int i, int j, int k, int n) {
    int varName = n*n*i+n*j+k+1;
    return varName;
}
    //functions for the CNF. Excactly one.

public static int[][] atLeastOne(int[] vars) {
    int [][] atLeastOne = new int [1][vars.length];
    atLeastOne[0] = vars;
    return atLeastOne;  
}

public static int[][] atMostOne(int[] vars) {
    int [][]atMostOne = new int[AmountOfArrays(vars.length)][2];
    int k = 0;
    for(int i= 0;i<vars.length;i= i+1){
        for( int j=i+1;j<vars.length;j= j+1){
            atMostOne[k][0] = -vars[i];
            atMostOne[k][1] = -vars[j];
            k=k+1;          
        }
    }
    return atMostOne;
}

public static int AmountOfArrays(int n){
    int counter = 0;
    for(int i= 0;i<n-1;i= i+1){
        for( int j=i+1;j<n;j= j+1){
            counter= counter+1;         
        }
    }
    return counter;
}

public static int[][] exactlyOne(int[] vars) {
    int [][] exactlyOne = new int [AmountOfArrays(vars.length)+1][vars.length];
    int [][]atLeastOne = new int [1][vars.length];
    atLeastOne = atLeastOne(vars);
    exactlyOne[0] = atLeastOne[0];
    for(int i=1; i<exactlyOne.length; i= i+1){
        int [][]atMostOne = atMostOne(vars);
        exactlyOne[i] = atMostOne[i-1];
    }
    return exactlyOne;
}

2 个答案:

答案 0 :(得分:1)

问题在这里blocks[i][j] = j%sqrtN+ i%sqrtN*sqrtN + 1; blocks[i][j]需要一个数组但j%sqrtN+ i%sqrtN*sqrtN + 1;返回一个int。

答案 1 :(得分:0)

在方法matrix中,您没有正确使用多个数组matrix -> int[][][] matrix[i] -> int[][] matrix[i][j] -> int[] matric[i][j][k] -> int

int

因此,您需要添加一个循环来迭代另一个数组维度,以便能够在单元格中设置for (int i = 0; i < matrix.length; ++i) { for (int j = 0; j < matrix[i].length; ++j) { for (int k = 0; k < matrix[i][j].length; ++k) { blocks[i][j][k] = ???? } } } 值。

matrix[i].length

请注意,我使用了if getattr(sys, 'frozen', False): Path = sys._MEIPASS #This is for when the program is frozen else: Path = os.path.dirname(__file__) #This is when the program normally runs ,如果您的数组维度大小不同,则更安全。