线程“主”中的异常java.lang.IndexOutOfBoundsException:索引:0,大小:0

超级

我正在尝试编写一个可以解决8-Puzzle问题的程序。我正在使用A *算法来找到解决方案。我已经审查了我的代码很多次,并且还尝试进行一些更改。甚至我的朋友都试图帮助我找到错误,但他们找不到。我仍然不明白我哪里出了问题。我使用javadocs查看我是否做错了什么,即使那并不能解决我的问题。我创建了三个类来解决此问题。

import java.util.*;
public class Solver implements Iterable<State>
{
    ArrayList<State> queue,solQueue;
    public int sol[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } };
    int temp[][],i;
    int moves;
    int leastPriority,removeIndex;
    State removeTemp;
    public Solver(State initial)
    {
        queue = new ArrayList<State>();
        solQueue = new ArrayList<State>();
        queue.ensureCapacity(16);
        solQueue.ensureCapacity(16);
        temp = new int[3][3];
    i=1;
    leastPriority = 100;
    removeTemp=initial;
    queue.add(removeTemp);
    Iterator<State> qu = queue.iterator();
    while(removeTemp.m!=sol)
    {
        leastPriority = 100;
        i=0;
        queue.iterator();
        for (State s : queue) 
        {
            if((s.mh + s.count) <leastPriority)
            {
                leastPriority = (s.mh + s.count);
                removeIndex = i;
            }
            if(qu.hasNext())
                i++;
        }
        for(State s : removeTemp.neighbours() )
        {
            queue.add(s);
        }
        removeTemp=queue.remove(removeIndex);
        solQueue.add(removeTemp);
    }
    this.moves();
    this.solution();
}
public int moves() 
{
    System.out.print("Solution found out in "+ moves+" moves");
    moves = removeTemp.count;
    return moves;
}
public Iterable<State> solution() 
{
    for(State s : solQueue)
    {
        System.out.println(s.m);
        System.out.println("");
    }
    return solQueue;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Iterator iterator() {
    return null;
}
}

JVM引发异常。

     Exception in thread "main" java.lang.IndexOutOfBoundsException: Index:  0,Size: 0
     at java.util.ArrayList.rangeCheck(Unknown Source)
     at java.util.ArrayList.get(Unknown Source)
     at Solver.<init>(Solver.java:41)
     at Main.main(Main.java:13)

我不明白的是,当我明确声明它为16时,ArrayList的大小如何为1。

状态类具有启发式功能,该功能被认为可以提高算法的效率。以下是状态类。

  import java.util.ArrayList;
  import java.util.Iterator;

  public class State implements Iterable<State>
  {
   public int sol[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } };
   int m[][], bi, bj, count, priority, si, sj;
   int i,j,tempm[][];
   int mh = 0;
   boolean isInitialState, isRepeatedState;
   State previousState, tempState;
   ArrayList<State> neighbourStates;

   public State(State s, int c, int[][] array) 
{
    neighbourStates = new ArrayList<State>();
    neighbourStates.ensureCapacity(16);

    tempState =this;
    m = new int[3][3];
    m=array;

    if (s == null) 
    {
        isInitialState = true;
        count = 0;
        previousState =null;
    } 
    else 
    {
        previousState = s;
        count = c+1;
    }

    this.findZero();
    this.manhattanHeuristic();
}

private void findZero() 
{
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
        {
            if(m[i][j]==0)
            {
                bi=i;
                bj=j;
            }
        }
}

private void manhattanHeuristic() {
    int n = 1;
    mh = 0;
    for (int i = 0; i < 3; i++)
        Z: for (int j = 0; j < 3; j++) {
            if ((i == bi) && (j == bj)) {
                continue Z;
            }

            else if (m[i][j] == n) {
                n++;
            }

            else {
                this.getSolutionIndex();
                mh = mh + Math.abs(i - si) + Math.abs(j - sj);
            }

        }
}

void getSolutionIndex() {
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++) {
            if (m[i][j] == 0) {
                si = i;
                sj = j;
            }
        }

}

public Iterable<State> neighbours()
{
    tempm = m;
    this.up();
    if(!(equals(tempm)))
    {
        tempState = new State(this,count,tempm);
        neighbourStates.add(tempState);
    }

    this.down();
    if(!(equals(tempm)))
    {
        tempState = new State(this,count,tempm);
        neighbourStates.add(tempState);
    }

    this.left();
    if(!(equals(tempm)))
    {
        tempState = new State(this,count,tempm);
        neighbourStates.add(tempState);
    }

    this.right();
    if(!(equals(tempm)))
    {
        tempState = new State(this,count,tempm);
        neighbourStates.add(tempState);
    }

    return neighbourStates;     

}

public boolean equals(int s[][])
{
    if((isInitialState==false)&&(previousState.m == s))
        return true;
    else
        return false;

}

@Override
public Iterator<State> iterator() {
    // TODO Auto-generated method stub
    return null;
}

public void up()
{
    if ((bi > 1) && (bi < 2) && (bj < 3)&& (bj > 1))
    {
        i = bi;
        i = i + 1;
        this.move(i,bj);
    }

}

public void  down()
{
    if ((bi > 2) && (bi < 3) && (bj < 3) && (bj > 1)) 
    {
        i = bi;
        i = i - 1;
        this.move(i,bj);
    }
}

public void left()
{
    if ((bi > 1) && (bi < 3) && (bj < 2)&& (bj > 1)) {
        j = bj;
        j = j + 1;
        this.move(bi, j);
    }
}

public void right()
{
    if ((bi > 1) && (bi < 3) && (bj < 3) && (bj > 2)) {
        j = bj;
        j = j - 1;
        this.move(bi, j);

    }

}

public void move(int x, int y) {
    {
        tempm = m;
    }
    if ((tempm[x + 1][y] == 0) || (tempm[x - 1][y] == 0) || (tempm[x][y + 1] == 0)|| (tempm[x][y - 1] == 0)) {
        tempm[bi][bj] = tempm[x][y];
        tempm[x][y] = 0;
        bi = x;
        bj = y;



    }
}

}

最后是具有主要功能的类。

  import java.util.Scanner;
  public class Main {
  public static void main(String[] args) 
  {
     @SuppressWarnings("resource")
     Scanner sc = new Scanner(System.in);
     int[][] tiles = new int[3][3];
     System.out.println("Enter the elements");
     for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            tiles[i][j] = sc.nextInt();
     State initial = new State(null,0,tiles);
     Solver solver = new Solver(initial);
     solver.solution();

     System.out.println("Minimum number of moves = " + solver.moves());

     }

}
杰斯珀

我不明白的是,当我明确声明它为16时,ArrayList的大小如何为1。

您未将的大小设置ArrayList为16。您已设置了容量:

queue.ensureCapacity(16);
solQueue.ensureCapacity(16);

这不会使ArrayList大小为16。

一个ArrayList具有一个数组来保存其数据。当您向中添加更多元素ArrayList并且其内部数组已满时,它将必须分配一个更大的数组,并复制当前持有的内容以及新元素的内容。

容量ArrayList是最小尺寸,该内部阵列具有。您可以ensureCapacity用来确保ArrayList不必太频繁地调整大小(调整大小和复制内容是一项昂贵的操作)。因此,ensureCapacity您要打个电话才能有效地工作。

它不ArrayList包含16个元素;它只能确保ArrayList至少有16个元素的空间。

如果您要ArrayList拥有16个元素,则必须一个一个地添加这些元素。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

java.lang.IndexOutOfBoundsException:索引:0,大小:0 Java 8

来自分类Dev

java.lang.IndexOutOfBoundsException索引:0,大小:0

来自分类Dev

java.lang.IndexOutOfBoundsException:索引:0,大小:0?

来自分类Dev

线程“主”中的异常java.lang.IndexOutOfBoundsException:索引:10,大小:10

来自分类Dev

线程“主”中的异常java.lang.IndexOutOfBoundsException:索引:15,大小:5

来自分类Dev

线程“主”中的异常java.lang.IndexOutOfBoundsException:索引:3,大小:2

来自分类Dev

如何解决“ AWT-EventQueue-0” java.lang.IndexOutOfBoundsException:索引:0,大小:0

来自分类Dev

JsArray <?>的大小为1,但得到“ java.lang.IndexOutOfBoundsException:索引:0,大小:0”

来自分类Dev

线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:0

来自分类Dev

Protobuf + java java.lang.IndexOutOfBoundsException:无效的索引0,大小为0

来自分类Dev

Android java.lang.IndexOutOfBoundsException:无效的索引0,大小为0

来自分类Dev

无法暂停活动,java.lang.IndexOutOfBoundsException:无效的索引0,大小为0

来自分类Dev

无法解决java.lang.IndexOutOfBoundsException:无效的索引1,大小为0

来自分类常见问题

尝试在另一个活动“ java.lang.IndexOutOfBoundsException:索引:0,大小:0”中显示详细信息

来自分类Dev

“线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:0” java错误

来自分类Dev

应用程序抛出 java.lang.IndexOutOfBoundsException:索引 1 无效,删除项目后大小为 0

来自分类Dev

线程“主”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-3

来自分类Dev

线程“主”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:4

来自分类Dev

线程“主”中的异常java.lang.NullPointerException-HBase索引数据

来自分类Dev

线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:在fibo.main处为0(fibo.java:7)

来自分类Dev

PDFbox异常-线程“主”中的异常java.lang.VerifyError

来自分类Dev

线程“main”中的异常 java.lang.IndexOutOfBoundsException:

来自分类Dev

线程“main”中的kotlin异常java.lang.IndexOutOfBoundsException

来自分类Dev

线程“主”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:java.lang.String.charAt(String.java:646)处为5

来自分类Dev

线程“主”中发生异常java.lang.IndexOutOfBoundsException:无组1

来自分类Dev

args [0] == null线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:Main.main处为1(Main.java:69)

来自分类Dev

线程“主”中的异常java.lang.StackOverflowError

来自分类Dev

线程“主”中的异常java.lang.ClassNotFoundException:WordCount

来自分类Dev

线程“主”中的异常java.lang.ClassNotFoundException:WordCount

Related 相关文章

  1. 1

    java.lang.IndexOutOfBoundsException:索引:0,大小:0 Java 8

  2. 2

    java.lang.IndexOutOfBoundsException索引:0,大小:0

  3. 3

    java.lang.IndexOutOfBoundsException:索引:0,大小:0?

  4. 4

    线程“主”中的异常java.lang.IndexOutOfBoundsException:索引:10,大小:10

  5. 5

    线程“主”中的异常java.lang.IndexOutOfBoundsException:索引:15,大小:5

  6. 6

    线程“主”中的异常java.lang.IndexOutOfBoundsException:索引:3,大小:2

  7. 7

    如何解决“ AWT-EventQueue-0” java.lang.IndexOutOfBoundsException:索引:0,大小:0

  8. 8

    JsArray <?>的大小为1,但得到“ java.lang.IndexOutOfBoundsException:索引:0,大小:0”

  9. 9

    线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:0

  10. 10

    Protobuf + java java.lang.IndexOutOfBoundsException:无效的索引0,大小为0

  11. 11

    Android java.lang.IndexOutOfBoundsException:无效的索引0,大小为0

  12. 12

    无法暂停活动,java.lang.IndexOutOfBoundsException:无效的索引0,大小为0

  13. 13

    无法解决java.lang.IndexOutOfBoundsException:无效的索引1,大小为0

  14. 14

    尝试在另一个活动“ java.lang.IndexOutOfBoundsException:索引:0,大小:0”中显示详细信息

  15. 15

    “线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:0” java错误

  16. 16

    应用程序抛出 java.lang.IndexOutOfBoundsException:索引 1 无效,删除项目后大小为 0

  17. 17

    线程“主”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-3

  18. 18

    线程“主”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:4

  19. 19

    线程“主”中的异常java.lang.NullPointerException-HBase索引数据

  20. 20

    线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:在fibo.main处为0(fibo.java:7)

  21. 21

    PDFbox异常-线程“主”中的异常java.lang.VerifyError

  22. 22

    线程“main”中的异常 java.lang.IndexOutOfBoundsException:

  23. 23

    线程“main”中的kotlin异常java.lang.IndexOutOfBoundsException

  24. 24

    线程“主”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:java.lang.String.charAt(String.java:646)处为5

  25. 25

    线程“主”中发生异常java.lang.IndexOutOfBoundsException:无组1

  26. 26

    args [0] == null线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:Main.main处为1(Main.java:69)

  27. 27

    线程“主”中的异常java.lang.StackOverflowError

  28. 28

    线程“主”中的异常java.lang.ClassNotFoundException:WordCount

  29. 29

    线程“主”中的异常java.lang.ClassNotFoundException:WordCount

热门标签

归档