简体   繁体   English

用初始容量重新实例化 ArrayList

[英]Reinstantiating ArrayList with initial capacity

I already have two ArrayLists of vertices for what is basically a graph data structure.对于基本上是图形数据结构的顶点,我已经有两个 ArrayLists。 The first being the vertices, or intersections, and the second being the edges, called roads.第一个是顶点或交叉点,第二个是边,称为道路。 I am trying to reinstantiate them when I know how many vertices/edges are in the graph.当我知道图中有多少顶点/边时,我试图重新实例化它们。

int line = 0;
        while(reader.hasNextLine()){
            String thisLine = reader.nextLine();
            System.out.println(thisLine + " line: " + line);
            if(line == 0){

                numIntersections = Integer.parseInt(thisLine.split("\\s+")[0]);
                intersections = new ArrayList<Intersection>(numIntersections);
                System.out.println("numIntersections: " + numIntersections + " intersections size: " + intersections.toArray().length);
                numRoads = Integer.parseInt(thisLine.split("\\s+")[1]);
                roads = new ArrayList<Road>(numRoads);
            }
            else if(line > 0 && line < numIntersections + 1){
                int first = Integer.parseInt(thisLine.split("\\s+")[0]);
                int second = Integer.parseInt(thisLine.split("\\s+")[1]);
                int third = Integer.parseInt(thisLine.split("\\s+")[2]);
                intersections.add(first, new Intersection(second, second, third));
            }
            else if(line > numIntersections + 1){
                roads.add(new Road(intersections.get(Integer.parseInt(thisLine.split("\\s+")[0])), intersections.get(Integer.parseInt(thisLine.split("\\s+")[1]))));
                intersections.get(Integer.parseInt(thisLine.split("\\s+")[0])).addNeighbor(intersections.get(Integer.parseInt(thisLine.split("\\s+")[1])));
            }
            line++;
        }

You can see that in the first if statement I reinstantiate the ArrayList when I know the numIntersections.您可以看到,在第一个 if 语句中,当我知道 numIntersections 时,我重新实例化了 ArrayList。 I do the same for when I know the number of roads.当我知道道路的数量时,我也会这样做。

However, when I try to add a new intersection to the list in the first else if statement, it throws an out of bounds exception.但是,当我尝试在第一个 else if 语句的列表中添加一个新的交集时,它会抛出一个越界异常。 Which should not occur because the capacity is set to the numIntersections.这不应该发生,因为容量设置为 numIntersections。

Capacity does not equal size.容量不等于大小。

A newly created ArrayList with a capacity of 10, will have a backing array that allows for the allocation of 10 elements, but its size will still be zero.新创建的容量为 10 的ArrayList将有一个支持数组,允许分配 10 个元素,但其大小仍为零。 Addressing any element in that ArrayList will result in an IndexOutOfBoundsException , although the add() method will let you add an element at index 0.寻址ArrayList中的任何元素都会导致IndexOutOfBoundsException ,尽管add()方法会让您在索引 0 处添加一个元素。

As the Javadoc for the ArrayList.add() method states:正如ArrayList.add()方法的 Javadoc 所述:

Throws:投掷:

IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size()) IndexOutOfBoundsException - 如果索引超出范围(index < 0 || index > size())

This answer shows how an ArrayList can be initialized with a specific number of values. 此答案显示了如何使用特定数量的值初始化ArrayList

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM