简体   繁体   English

运行时错误,原因我无法弄清楚

[英]Runtime error for a reason I can't figure out

This code is supposed to take 2 integer array lists of length 3 such as (3, 3, 4) and (1, 2, 3), compare each index, and find which one is greater.这段代码应该取 2 个 integer 长度为 3 的数组列表,例如 (3, 3, 4) 和 (1, 2, 3),比较每个索引,找出哪个更大。

It then awards a point to whichever is greater, so 3>1 means array list 1 gets a point for total score 1-0, 3>2 means array list 1 gets a point for total score 2-0, and 4>3 means array list 1 gets a point for total score 3-0.然后它奖励一个更大的分数,所以 3>1 表示数组列表 1 获得总分 1-0 的分数,3>2 表示数组列表 1 获得总分 2-0 的分数,4>3 表示数组列表 1 以总比分 3-0 得一分。 It's then supposed to create a new array list of length 2 and set the first index to 3, the score of the first array list, then set the second index to 0, the score of the second array list.然后应该创建一个长度为 2 的新数组列表,并将第一个索引设置为 3,即第一个数组列表的分数,然后将第二个索引设置为 0,即第二个数组列表的分数。

For some reason I'm getting a runtime error and I cannot figure out what it is.出于某种原因,我遇到了运行时错误,我无法弄清楚它是什么。

Any help is appreciated, and if someone could explain what a runtime error is in laymen's terms that would be much appreciated.任何帮助表示赞赏,如果有人可以用外行的话解释什么是运行时错误,将不胜感激。

static List<Integer> compareTriplets(List<Integer> a, List<Integer> b) {
        int bobscore = 0;
        int alicescore = 0;
        int throwaway = 0;
        
    
        for (int i = 0; i < a.size(); i ++){
            if (a.get(i) == b.get(i)){
                throwaway++;
            }       
            
            if (a.get(i) > b.get(i)){
                alicescore++;
            }       
            if (a.get(i) < b.get(i)){
                bobscore++;
            }       
            
        }
        
        List<Integer> scores = new ArrayList<>(2);
        scores.set(0, alicescore);
        scores.set(1, bobscore);
        return scores;
    }

It's this part:就是这部分:

List<Integer> scores = new ArrayList<>(2);
        scores.set(0, alicescore);
        scores.set(1, bobscore);

That constructor still makes an empty list , so scores.set(0 fails because there is no 1st element to set. The '2' is a 'capacity hint': It is conveying to the arraylist constructor that it should start off by having room for 2 elements.该构造函数仍然是一个空列表,因此scores.set(0失败,因为没有要设置的第一个元素。“2”是“容量提示”:它向 arraylist 构造函数传达它应该从有空间开始对于 2 个元素。

ArrayLists automatically resize themselves if you keep adding elements to them;如果您不断向其中添加元素,ArrayLists 会自动调整其大小; however, such a resize does involve an under-the-hood copy of an array, and conversely, arraylists (usually) have more room than you're using, which is fine as well, but in rare circumstances, you want to optimize this;但是,这样的调整大小确实涉及数组的底层副本,相反,arraylists(通常)比您使用的空间更大,这也很好,但在极少数情况下,您希望优化它; if you know beforehand how large it's gonna be, you can pass that in and the arraylist will be almost inperceptibly faster.如果您事先知道它将有多大,您可以将其传递进去,arraylist 的速度几乎无法察觉。

That's why that constructor exists.这就是构造函数存在的原因。 You should not be using it here.你不应该在这里使用它。

Most likely you want:您很可能想要:

List<Integer> scores = new ArrayList<>();
scores.add(aliceScore);
scores.add(bobScore);

or even:甚至:

int[] scores = new int[2];
scores[0] = aliceScore;
scores[1] = bobScore;

Note that arraylist and arrays are completely different beasts.请注意,arraylist 和 arrays 是完全不同的野兽。 ArrayLists start out empty and can grow. ArrayLists 一开始是空的,并且可以增长。 arrays have a set size that you must provide upon creation and can never be any smaller or any larger; arrays 具有您必须在创建时提供的设置大小,并且不能更小或更大; a new int[2] is size 2. And will always be.一个new int[2]的大小为 2。而且永远都是。 (The name 'ArrayList' refers to the notion that it uses arrays internally to do its work). (名称“ArrayList”指的是它在内部使用 arrays 来完成其工作的概念)。

Use the simple static function List.of(aliceScore, bobScore) or Arrays.asList(aliceScore, bobScore) to return the list result.使用简单的 static function List.of(aliceScore, bobScore)Arrays.asList(aliceScore, bobScore)返回列表结果。

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

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