我有以下代码:
freq1: {'h': 3, 's': 2, 'f': 1}
freq2: defaultdict(<type 'int'>, {'h': 3, 's': 2, 'f': 1})
freq3: defaultdict(<type 'int'>, {'h': 3, 's': 2, 'f': 1})
freq4: {'h': 3, 's': 2, 'f': 1}
Do all the dictionaries equal each other as they stand?
Answer: True
freq1: {'h': 3, 's': 2, 'f': 1}
freq2: {'h': 3, 's': 2, 'f': 1}
freq3: {'h': 3, 's': 2, 'f': 1}
freq4: {'h': 3, 's': 2, 'f': 1}
[Finished in 0.1s]
循环内的println检查正确地正确报告存储的数据,因为您可以看到是否运行该程序。但是,在循环之后,如果我尝试检索循环内设置的任何值,则所有值= 0.我缺少什么?
在循环中设置,所有值都应该对应于数组第一维的索引:
allsolutions [0] [x] [x] [x] [x] = 0;
allsolutions [1] [x] [x] [x] [x] = 1;
allsolutions [2] [x] [x] [x] [x] = 2;
等等......
答案 0 :(得分:9)
您永远不会向allsolutions[4][2][1][4][5]
或您正在打印的其他4个阵列位置分配任何内容,因此它们保持为0.您的阵列中只有4个嵌套循环和5个维度。
您只能将值分配给第二个索引等于第五个索引的位置。如果您尝试打印,例如System.out.println(allsolutions[4][2][1][4][2]);
,则会看到非零值。
您应该使用5个嵌套循环,而不是重复使用j索引:
for(int i=0;i<=15;i++){
for(int j=0;j<=15;j++){
for(int k=0;k<=15;k++){
for(int l=0;l<=15;l++){
for(int m=0;m<=15;m++){
allsolutions[i][j][k][l][m] = i;
System.out.println("Set index "+ i + " " + j + " " + k + " " + l + " " + m + " equal to " + i);
}
}
}
}
}
答案 1 :(得分:3)
你错过了一个内循环。请尝试以下方法:
RewriteEngine On
#we replace domain.com/$1 with %{SERVER_NAME}%{REQUEST_URI}.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) https://www.%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
#here we dont use www as non www was already redirected to www.
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
问题源于您使用final int DIM = 16;
int[][][][][] allsolutions = new int[DIM][DIM][DIM][DIM][DIM];
for (int i = 0; i < DIM; i++){
for (int j = 0; j < DIM; j++) {
for (int k = 0; k < DIM; k++) {
for (int l = 0; l < DIM; l++){
for (int m = 0; m < DIM; m++) {
allsolutions[i][j][k][l][m] = i;
System.out.println("Set index " + i + " " + j + " " + k + " " + l + " " + m + " equal to " + i);
}
}
}
}
}
而不是新变量的最后一个索引,例如j
。
答案 2 :(得分:0)
您正在重复j
,因此在[?][x1][?][?][x2]
时您不会填充x1 != x2
。
如果值取决于索引,为什么不使用函数?
public int getValue(int i, int dc1, int dc2, int dc3, int dc4){
if (i < 16 && 0 <= i){
return i;
}
// ... throw error or return sentinel value
}
并使用它:
System.out.println(getValue(4,2,1,4,5)); // 4
您可以将其扩展为更复杂的内容,而无需静态存储所有数据。
答案 3 :(得分:0)
它对我有用:
import java.util.Arrays;
public class SolutionsTest {
private static int [][][][] allsolutions;
//make the arrays static and as a field not local
public static void main (String args[]) {
allsolutions = new int [16][16][16][16];
//Initialize the arrays
for (int i = 0; i < 16;i++) { // The i or index loop
for (int x = 0; x < 16;x++) {
for (int y = 0; y < 16;y++) {
for (int z = 0; z < 16; z++) {
allsolutions[i][x][y][z] = i; //set all as index
}
}
}
}
System.out.println(allsolutions[0][0][0][0]); //prints 0
System.out.println(allsolutions[1][0][0][0]); //prints 1
System.out.println(allsolutions[2][0][0][0]); //prints 2
System.out.println(allsolutions[3][0][0][0]); //prints 3
System.out.println(allsolutions[0][4][5][5]); //prints 0
System.out.println(allsolutions[1][8][9][10]); //prints 1
System.out.println(allsolutions[2][9][10][1]); //prints 2
System.out.println(Arrays.deepToString(allsolutions));
//prints all arrays
}
}
以下代码输出正确的结果