将节点元素添加到ArrayList并在列表上调用堆方法,从而抛出持久索引超出范围的异常

时间:2018-09-25 22:08:38

标签: java arraylist indexoutofboundsexception

一个非常基本的问题,但是我很固执,越来越绝望。任何有益的建议将不胜感激。谢谢

我正在尝试一次将现有ArrayList中的节点元素添加到一个新的空ArrayList中,然后在新ArrayList上调用堆方法。这个想法是在1个元素的列表上调用堆方法,然后在2个元素的列表上调用堆方法,然后在3个元素的列表上调用等等。但是,当尝试调用最初的“ Build_min_Heap()”方法时,我是得到以下异常:java.lang.IndexOutOfBoundsException:索引:3,大小:3

我的堆类如下:

import java.util.*;

public class Heap 
{
int heapSize;
Graph gr;

//constructor for heap object with the following attributes:
//a graph object and an int representing the size of an arraylist of nodes
public Heap(ArrayList<Node> A, Graph g)
{
    heapSize = A.size();
    gr = g;
}

public int getHeapSize() {
    return heapSize;
}

public void setHeapSize(int heapSize) {
    this.heapSize = heapSize;
}

//heap methods

public int Parent(ArrayList<Node> A, int i)
{
    //if (i == 1)
        //return (Integer)null;

    if (i%2 != 0)
        return i/2;

    else 
        return (i-1)/2;
}

public int Left(ArrayList<Node> A, int i)
{
    //if (2*i < heapSize)
        return (2*i)+1;
    //else
        //return (Integer)null;
}

public int Right(ArrayList<Node> A, int i)
{
    //if ((2*i)+1 < heapSize)
        return 2*i+2;
    //else
        //return (Integer)null;
}

public void Heapify(ArrayList<Node> A, int i)
{
    Node smallest;
    Node temp;
    int index;

    int l = Left(A,i);
    int r = Right(A,i);

    //if (l <= heapSize-1 && Integer.parseInt(A.get(l).getVal()) < Integer.parseInt(A.get(i).getVal()))
    if (Integer.parseInt(A.get(l).getVal()) < Integer.parseInt(A.get(i).getVal()))
    {   
        //left child is smaller
        smallest = A.get(l);
        index = l;
    }   
    else
    {   
        //parent node is smaller
        smallest = A.get(i);
        index = i;
    }   

    //if (r <= heapSize-1 && Integer.parseInt(A.get(r).getVal()) < Integer.parseInt(smallest.getVal()))
    if (Integer.parseInt(A.get(r).getVal()) < Integer.parseInt(smallest.getVal()))
    {   
        //right child is smaller
        smallest = A.get(r);
        index = r;
    }

    if (index != i)
    {   
        //if the smallest element is not the parent node
        //swap the smallest child with the parent  
        temp = A.get(i);
        A.set(i, A.get(index));
        A.set(index, temp);

        //recursively call heapify method to check next parent/child relationship
        Heapify(A, index);
    }   
}

//method to construct min heap from unordered arraylist of nodes
public void Build_min_Heap(ArrayList<Node> A)
{
    int i;
    int heapSize = A.size();

    for (i = (heapSize/2); i>=0; i--)
    {   
        Heapify(A, i);
        System.out.print(gr.toString2()+"\n");
    }   

//method to sort in descending order, a min heap
public void heap_Sort(ArrayList<Node> A)
{
    Node temp;

    Build_min_Heap(A);
    //System.out.println(gr.toString2()+"\n");

    for(int i = A.size()-1; i >= 1; i--)
    //for (int i = 0; i <= A.size()-1; i++)
    {
        //exchange a[0] with a[i]
        temp = A.get(0);
        A.set(0, A.get(i));
        A.set(i, temp);

        //temp = A.get(A.size()-1);
        //A.set(A.size()-1, A.get(i));
        //A.set(i, temp);

        //decrement heapSize
        heapSize--;

        //recursive heapify call
        Heapify(A, 0);
    }
}

包含main方法的类很长,但重要的部分是:

    g = new Graph();
    readGraphInfo( g );
    h = new Heap(g.getNodeList(), g);
    DelivB dB = new DelivB(inputFile, g);


    int numElements = g.getNodeList().size();
    ArrayList<Node> ordered_nodeList = new ArrayList<Node>(15);


        for (int i = 0; i <= numElements; i++)
        {
            ordered_nodeList.add(i, g.getNodeList().get(i));

            h.Build_min_Heap(ordered_nodeList);
        }

0 个答案:

没有答案