简体   繁体   English

如何解决java.lang.IndexOutOfBoundsException问题:索引:0,大小:0

[英]How to solve the problem of java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

I am trying to set the a new position which is the global solution for the algorithm. 我正在尝试设置一个新的位置,该位置是该算法的全局解决方案。 I read the new position from a text file called (hh) but when I trying to pass it via the code line p.setNeighborhoodPosition(f,val1);, I got the following error: 我从名为(hh)的文本文件中读取了新位置,但是当我尝试通过代码行p.setNeighborhoodPosition(f,val1);将其传递时,出现以下错误:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:635)
    at java.util.ArrayList.set(ArrayList.java:426)
    at PSOFS.Particle.setNeighborhoodPosition(Particle.java:177)
    at PSOFS.Main.main(Main.java:213)

Here is the code that I used 这是我使用的代码

     Particle p = new Particle();
          if (s.getProblem().isBetter(newFitness, oldFitness)) {
              p.setNeighborhoodFitness(newFitness);
 String sc1 = new Scanner( new File("hh.txt") ).useDelimiter("\\A").next();
             sc1 = sc1.replaceAll("\\[","").replaceAll("\\]","");
            String[] doublesAsStrings1 = sc1.split(", ");
           double [] num=new double[doublesAsStrings1.length];
          for( int f = 0; f < doublesAsStrings1.length; f++ ){
              num[f] = Double.parseDouble(doublesAsStrings1[f]);
                  p.setNeighborhoodPosition(f,num[f]);}

the two lines that cause the error are 导致错误的两行是

public void setNeighborhoodPosition(int index, double value) {
        this._neighborhood_position.set(index, value);
    }

         p.setNeighborhoodPosition(f,num[f]);

Thank you 谢谢

As mentioned in the comments by Andreas and Stephen C , use method add() of class java.util.ArrayList , eg AndreasStephen C的评论中所述,请使用类java.util.ArrayList add()方法,例如

public void setNeighborhoodPosition(double value) {
    this._neighborhood_position.add(value);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决“ java.lang.IndexOutOfBoundsException:索引:1,大小:1”的问题 - How to fix “java.lang.IndexOutOfBoundsException: Index: 1, Size: 1” problem 如何解决java.lang.IndexOutOfBoundsException:无效的索引24,大小为21 - How to solve java.lang.IndexOutOfBoundsException: Invalid index 24, size is 21 java.lang.IndexOutOfBoundsException:索引:1,大小:1 - java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 java.lang.IndexOutOfBoundsException:索引:2,大小:0 - java.lang.IndexOutOfBoundsException: Index: 2, Size: 0 java.lang.IndexOutOfBoundsException:索引:1,大小:1 onBindViewHolder - java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 onBindViewHolder java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 in Kotlin - java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 in Kotlin GSEA:java.lang.IndexOutOfBoundsException:索引:0,大小:0 - GSEA : java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 java.lang.IndexOutOfBoundsException:无效的索引5,大小为5 - java.lang.IndexOutOfBoundsException: Invalid index 5, size is 5 例外:java.lang.IndexOutOfBoundsException:索引:0,大小:0 - Exception : java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 java.lang.IndexOutOfBoundsException:索引:5,大小:5 - java.lang.IndexOutOfBoundsException: Index: 5, Size: 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM