java - 为什么我会收到 "Exception in thread "main"java.lang.IndexOutOfBoundsException : Index 0 out of bounds for length 0 "

标签 java backtracking

我必须回溯列表中代表限制的数字,例如:“x1 + x2> = 1”。如果它满足所有条件,则该数组将添加到另一个数组中,此外还有另一个列表表示我必须对所有变量“x1 + x2 + x3 + x4”进行求和,并搜索那个具有最小值。

在回溯中我应该做的就是制作一个包含满足限制的所有可能性的二进制矩阵。我所做的是这样的,但我收到错误:“线程中的异常”主要“java.lang.IndexOutOfBoundsException:索引2超出长度0的范围”,我不知道我的问题出在哪里。

import java.util.ArrayList;

public class Pra_hacer_pruebas {
    public static void main(String[] args) {
           Pra_hacer_pruebas a = new Pra_hacer_pruebas();
           ArrayList<Integer> conf1= new ArrayList<>(); // conf1 is the list that will contain one of the possibilities that may or may not be added to the binary matrix.
           ArrayList<ArrayList<Integer>>pos_v = new ArrayList<>();// pos_v is where the possibilities will be added, binary matrix
           int[][] restric = new int[2][2];// restric is the list with restrictions
           restric[0][0]=2;
           restric[0][1]=1;
           restric[1][0]=4;
           restric[1][1]=2;
           for(int t=0;t<4;t++){
               conf1.set(t, -1);
           }
           //System.out.println(conf.get(i));
           a.binario(conf1,restric,0,0,0,pos_v,0,4,-1);
    }

    public void binario(ArrayList<Integer> conf1, int[][] restric, int suma,int filas,int columnas,ArrayList<ArrayList<Integer>> pos_validas,int posicion, int cont,int bin){
        //filas = rows, suma= sum is to see if it meets the condition, columnas = columns, pos_validas = pos_v, posicion is to advance the rows of the matrix, cont: is the amount of different variables, bin is the binary variable
        Boolean booleano = false;  // booleano is the flag that if it is true it is because there was a null position (-1)
        for (int[] restric1 : restric) {
            suma=0;
            for (int co = 0; co < restric1.length; co++) {
                if ((conf1.get(restric1[co]) == 1) || (conf1.get(restric1[co]) == 0)) {
                    suma = suma + conf1.get(restric1[co]);
                } else {
                    booleano = true;
                }
            }
            if (booleano == false) {
                if (suma < 1){
                    break;
                }
            }
        }
        if (booleano == false) {
            pos_validas.set(posicion, conf1);
            posicion++;
        }
        for (int f = 0; f < cont; f++) {
            if (conf1.get(f) < 1) {
                bin++;
                conf1.set(f, bin);
binario(conf1,restric,suma,filas,columnas,pos_validas,posicion,cont,bin);
            }
            bin--;
        }
    }
}

最佳答案

尝试add方法。即使您使用initialCapacity创建ArrayList,它也不会按您的预期工作。如果你在set之前打印ArrayList的大小,你可以检查它。

System.out.println(conf1.size());
for(int t=0; t<4; t++){
    conf1.set(t, Integer.valueOf(-1));
}

修改代码以使用add

for(int t=0; t<4; t++){
    conf1.add(-1);
}

关于java - 为什么我会收到 "Exception in thread "main"java.lang.IndexOutOfBoundsException : Index 0 out of bounds for length 0 ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58573910/

相关文章:

java - 类型 Set 不是通用的;它不能用参数 <Integer> 参数化

java - 包含TextView的ScrollView不滚动

python - 迷宫不是随机的

java - RestTemplate 线程安全吗?

java - 二叉树上的遗传算子

java - 有没有办法在 Spring XML 中指定默认属性值?

python - 打印n叉树python的所有路径

algorithm - smlnj中开放骑士之旅(回溯)算法

c++ - 多重约束背包

algorithm - 如何使用回溯法求解 M<N 时的 M 个皇后