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