Java for循环未正确填充数组
我有以下代码:Java for循环未正确填充数组,java,loops,multidimensional-array,combinations,permutation,Java,Loops,Multidimensional Array,Combinations,Permutation,我有以下代码: public class solutionsTest { public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException { int allsolutions[][][][][] = new int[16][16][16][16][16]; for (int i = 0; i <= 15; i++) {
public class solutionsTest {
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
int allsolutions[][][][][] = new int[16][16][16][16][16];
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++) {
allsolutions[i][j][k][l][j] = i;
System.out.println("Set index " + i + " " + j + " " + k + " " + l + " " + j + " equal to " + i);
}
}
}
}
System.out.println(allsolutions[4][2][1][4][5]);
System.out.println(allsolutions[5][2][1][4][5]);
System.out.println(allsolutions[6][2][1][4][5]);
System.out.println(allsolutions[7][2][1][4][5]);
System.out.println(allsolutions[8][2][1][4][5]);
}
}
公共类解决方案测试{
公共静态void main(字符串[]args)引发FileNotFoundException、UnsupportedEncodingException{
int-allsolutions[][]=新的int[16][16][16][16][16];
对于(int i=0;i您从未将任何内容分配给所有解决方案[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);
}
}
}
}
}
对于(int i=0;i您从未将任何内容分配给所有解决方案[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);
}
}
}
}
}
for(int i=0;i您缺少一个内部循环。请尝试以下操作:
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);
}
}
}
}
}
final int DIM=16;
int[]allsolutions=new int[DIM][DIM][DIM][DIM][DIM][DIM];
对于(int i=0;i
此问题源于上一次使用j
而不是新变量(例如m
)进行索引。您缺少一个内部循环。请尝试以下操作:
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);
}
}
}
}
}
final int DIM=16;
int[]allsolutions=new int[DIM][DIM][DIM][DIM][DIM][DIM];
对于(int i=0;i
问题源于上一次索引使用了j
而不是新变量,例如m
重复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
}
您可以将其扩展到更复杂的内容,而无需静态存储所有数据。您正在重复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
}
您可以将其扩展到更复杂的领域,而无需静态存储所有数据。它对我很有用:
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
}
导入java.util.array;
公共类解决方案测试{
私有静态int[][]所有解决方案;
//将数组设置为静态,并将其作为字段而不是本地字段
公共静态void main(字符串参数[]){
allsolutions=new int[16][16][16];
//初始化数组
对于(inti=0;i<16;i++){//i或索引循环
对于(intx=0;x<16;x++){
对于(int y=0;y<16;y++){
对于(intz=0;z<16;z++){
allsolutions[i][x][y][z]=i;//将所有设置为索引
}
}
}
}
System.out.println(所有解决方案[0][0][0][0]);//打印0
System.out.println(所有解决方案[1][0][0]);//打印1
System.out.println(所有解决方案[2][0][0]);//打印2
System.out.println(所有解决方案[3][0][0]);//打印3
System.out.println(所有解决方案[0][4][5][5]);//打印0
System.out.println(所有解决方案[1][8][9][10]);//打印1
System.out.println(所有解决方案[2][9][10][1]);//打印2
System.out.println(Arrays.deepToString(allsolutions));
//打印所有数组
}
}
下面的代码输出正确的结果它对我有用:
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
}
导入java.util.array;
公共类解决方案测试{
私有静态int[][]所有解决方案;
//将数组设置为静态,并将其作为字段而不是本地字段
公共静态void main(字符串参数[]){
allsolutions=new int[16][16][16];
//初始化数组
对于(inti=0;i<16;i++){//i或索引循环
对于(intx=0;x<16;x++){
对于(int y=0;y<16;y++){
对于(intz=0;z<16;z++){
allsolutions[i][x][y][z]=i;//将所有设置为索引
}
}
}
}
System.out.println(所有解决方案[0][0][0][0]);//打印0
System.out.println(所有解决方案[1][0][0]);//打印1
System.out.println(所有解决方案[2][0][0]);//打印2
System.out.println(所有解决方案[3][0][0]);//打印3
System.out.println(所有解决方案[0][4][5][5]);//打印0
System.out.println(所有解决方案[1][8][9][10]);//打印1
System.out.println(所有解决方案[2][9][10][1]);//打印2
System.out.println(Arrays.deepToString(allsolutions));
//打印所有数组
}
}
下面的代码输出正确的结果可能是Java中的某种内存问题吗?可能是Java中的某种内存问题吗?谢谢!显然我今天没有打开我的大脑。谢谢!显然我今天没有打开我的大脑。