为什么我的ArrayList不能正确初始化?

Cod*_*ein 0 java arraylist

我在让ArrayList初始化时遇到了令人沮丧的困难.我在线上binaryTreeList.set(1, root);说错了

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.set(Unknown Source)
    at BinaryTreeADT.<init>(BinaryTreeADT.java:18)
    at Driver.main(Driver.java:7)
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用ArrayList实现一个简单的二叉树,我希望"root"元素位于ArrayList位置1.由于某种原因,`binaryTreeList的大小没有增长,尽管向所有的节点添加节点他们.

这是我的代码按顺序Driver,BinaryTreeADTMyTreeNode

public class Driver {


    public static void main(String[] args) {
        MyTreeNode mtn = new MyTreeNode(3, 'R');
        BinaryTreeADT bt = new BinaryTreeADT(mtn);
        bt.printTree();
    }

}
Run Code Online (Sandbox Code Playgroud)

BinaryTreeADT:

import java.util.ArrayList;
import javax.swing.tree.TreeNode;

public class BinaryTreeADT {

    private ArrayList<MyTreeNode> binaryTreeList;
    private MyTreeNode nullNode = new MyTreeNode(true);   //This creates a null node that initially populates the array.

    //Constructor with no root
    public BinaryTreeADT(){
        binaryTreeList = new ArrayList<MyTreeNode>(10);
    }
    public BinaryTreeADT(MyTreeNode root){
        binaryTreeList = new ArrayList<MyTreeNode>(10);
        initializeList();
        binaryTreeList.set(1, root);
    }
    private void initializeList(){
        for (int i = 0; i < binaryTreeList.size(); i++){
            binaryTreeList.add(nullNode);
        }
    }
    public void add(){

    }
    public void printTree(){
        for (int i = 0; i < binaryTreeList.size(); i++){
            if (binaryTreeList.get(i) != null)
                System.out.println(binaryTreeList.get(i).getNodeChar() + " | ");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

MyTreeNode:

import java.util.Enumeration;
import javax.swing.tree.TreeNode;

public class MyTreeNode implements TreeNode {

    private int nodeKey;
    private char nodeChar;
    private boolean isNull;

    public MyTreeNode(int key, char letter){
        nodeKey = key;
        nodeChar = letter;
    }
    //Constructor for Null Node
    public MyTreeNode(boolean setNull){
        isNull = setNull;
    }
    public boolean isNull(){ //Tells if this is a null node
        return isNull;
    }

    @Override
    public Enumeration children() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean getAllowsChildren() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public TreeNode getChildAt(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int getChildCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int getIndex(TreeNode arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public TreeNode getParent() {
        // TODO Auto-generated method stub
        return null;
    }

    public int getNodeKey() {
        return nodeKey;
    }

    public void setNodeKey(int nodeKey) {
        this.nodeKey = nodeKey;
    }

    public char getNodeChar() {
        return nodeChar;
    }

    public void setNodeChar(char nodeChar) {
        this.nodeChar = nodeChar;
    }

    @Override
    public boolean isLeaf() {
        // TODO Auto-generated method stub
        return false;
    }

}
Run Code Online (Sandbox Code Playgroud)

Vis*_*l K 9

原因是这一行:

binaryTreeList.set(1, root);
Run Code Online (Sandbox Code Playgroud)

因为大小binaryTreeList为零.你已经构建了ArrayList并且告诉它initial Capacity10使用构造函数ArrayList(int initialCapacity),但是由于ArrayList现在没有任何内容,所以ArrayList#size()返回为0.这就是为什么在你的initializeList方法中,for loop在第一次迭代时退出,而不是初始化binaryTreeListwith 10元素.所以大小binaryTreeList仍然是0.这就是在索引处设置一个1根本不存在的值的原因IndexOutOfBoundException.

你应该定义initializeList为:

    private void initializeList(){
     for (int i = 0; i < 10; i++){
        binaryTreeList.add(nullNode);
     }
    }
Run Code Online (Sandbox Code Playgroud)