Java - ArrayList - .size() 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30578739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - ArrayList - .size() Method
提问by Jeff Sours
I am currently in the process of learning the Java programming language, and I need help understand the size method of the ArrayList class. Before asking the question, let me explain the program.
我目前正在学习Java编程语言,需要帮助理解ArrayList类的size方法。在问这个问题之前,让我解释一下程序。
This program finds the average of class grades.
该程序计算班级成绩的平均值。
My question would be about the size method of the ArrayList class. In the For-Loop, the relational expression, "i < grades.size()", will run "i" until it is less than the size of the array. I was instructed that the size of the array is always one more than the number of elements in the array, so in my case, given that I have 3 elements (grades) in my array, I actually have an array size of 4 (similar to how a string works). Notice that after I am out of the For-Loop, I am able to appropriately calculate my average with the size method. I am confused because if the size of the array would be one more than the number of elements, then would it not just divide by 4 rather than 3? Why do the rules function differently in a For-Loop? Thank you for anybody willing to shed light on this.
我的问题是关于 ArrayList 类的 size 方法。在 For 循环中,关系表达式“i <grades.size()”将运行“i”,直到它小于数组的大小。我被指示数组的大小总是比数组中的元素数多 1,所以在我的例子中,鉴于我的数组中有 3 个元素(等级),我实际上有一个数组大小为 4(类似字符串如何工作)。请注意,在我退出 For-Loop 之后,我可以使用 size 方法适当地计算我的平均值。我很困惑,因为如果数组的大小比元素数多一,那么它不只是除以 4 而不是除以 3?为什么规则在 For 循环中的功能不同?感谢任何愿意阐明这一点的人。
import java.util.ArrayList;
public class Chap11Part1
public static void main(String[] args)
{
double average;
int total = 0;
ArrayList<Integer> grades = new ArrayList<Integer>();
grades.add(78);
grades.add(84);
grades.add(90);
for(int i = 0; i < grades.size(); ++i)
total += grades.get(i);
average = total / grades.size();
System.out.println("The average is " + average);
回答by Rajesh
size of the array is same as number of elements. Only thing to remember is index starts with 0; So last index will be size-1:
数组的大小与元素数相同。唯一要记住的是索引从 0 开始;所以最后一个索引将是 size-1:
import java.util.ArrayList;
public class Chap11Part1 {
public static void main(String[] args) {
double average;
int total = 0;
ArrayList<Integer> grades = new ArrayList<Integer>();
grades.add(78);
grades.add(84);
grades.add(90);
for (int i = 0; i < grades.size(); ++i)
total += grades.get(i);
average = total / grades.size();
System.out.println("The average is " + average);
}
}
回答by Brandon Buck
As has been pointed out, you have a confusion that starts here:
正如已经指出的,你有一个从这里开始的困惑:
I was instructed that the size of the array is always one more than the number of elements in the array, so in my case, given that I have 3 elements (grades) in my array, I actually have an array size of 4 (similar to how a string works).
我被指示数组的大小总是比数组中的元素数多 1,所以在我的例子中,鉴于我的数组中有 3 个元素(等级),我实际上有一个数组大小为 4(类似字符串如何工作)。
Whether this misunderstanding was on part of the instructor explaining things or you misinterpreting the meaning is irrelevant. This understanding is the source of your confusion and ultimately the problem.
这种误解是教师解释事情的一部分还是您误解了含义无关紧要。这种理解是你困惑的根源,最终是问题所在。
You are correct. And ArrayList
's size
function will return 1 more than something, similar to how String
s work as well. That somethingis the maximum index. Arrays (and ultimately ArrayList
s, which is just a Data Structure implemented on top of an underlying array) have an index value that starts at 0
(not 1
like you typically would when counting). That means, if you had three numbers, you would end up with something like this:
你是对的。AndArrayList
的size
函数将返回 1 多于something,类似于String
s 的工作方式。那个东西是最大的索引。数组(最终ArrayList
是 s,它只是一个在底层数组之上实现的数据结构)有一个开始于0
(1
不像你通常在计数时那样)的索引值。这意味着,如果你有三个数字,你最终会得到这样的结果:
numbers[0] = 10
numbers[1] = 108
numbers[2] = 309
(Ignore the psuedo-code like syntax, pay attention to the values in brackets here)
(忽略伪代码之类的语法,注意这里括号中的值)
If I were to get the size
(or length
with certain types) I would get 3
. You can clearly see there are only 3 numbers shown. The "one more than" aspect is one more than the maximum index. The maximum index here is 2
, given that we start counting (index-wise) at 0
.
如果我要得到size
(或length
某些类型),我会得到3
. 您可以清楚地看到只显示了 3 个数字。“一多”方面是比最大指数多一。2
考虑到我们从 开始计数(按索引),这里的最大索引是0
。
This issue is one of the more common I've seen from new developers and it's often referred to as an "off-by-one error" or OBO Error as you want the first element of a list you may try list.get(1)
when that's is actually the secondelement of the list, .get(0)
would be the first.
这个问题是我从新开发人员那里看到的最常见的问题之一,它通常被称为“逐个错误”或 OBO 错误,因为您想要列表的第一个元素,list.get(1)
当它实际上是列表的第二个元素,.get(0)
将是第一个。
So now...
所以现在...
You have 3 elements in your ArrayList
, a call to .size
would give (as you should expect) the number 3
in return. Your loop comparisons go like this:
您的 中有 3 个元素ArrayList
,调用.size
将给出(如您所料)作为3
回报的数字。你的循环比较是这样的:
Iteration 1: i = 0; 0 < 3 == true; execute body
Iteration 2: i = 1; 1 < 3 == true; execute body
Iteration 3: i = 2; 2 < 3 == true; execute body
Iteration 4: i = 3; 3 < 3 == false; jump over body
As you can see, your condition is checked "one more than the number of elements" times (in this case, 4
) but only 3
times, or the actual number of elements, is the loop body executed or run. If you had accidentally used <=
instead, you'd get an OBO Error by accessing "one more than the list contains." Thats why having a grasp on this whole "start at 0" counting system is going to be key, as well as understanding that sizes or locations tend to be one-greater than index values.
如您所见,您的条件被检查“比元素数多一”次(在本例中为4
),但只有3
次数或实际元素数是执行或运行的循环体。如果您不小心<=
改为使用,您将通过访问“比列表包含的多一个”而收到 OBO 错误。这就是为什么掌握整个“从 0 开始”计数系统将是关键,以及了解大小或位置往往比索引值大 1 的原因。
If any part of this is more confusing than it should be, let me know and I'll try to fix it up.
如果其中的任何部分比应有的更令人困惑,请告诉我,我会尽力解决。
回答by Ojonugwa Jude Ochalifu
The elements in your array (78, 84, 90) are counted to give the size/length of the array, which is 3 (one more than the largest subscript) What you should know is that the index (position of each element) starts from 0. So in your list, 78 is at index 0, 84 at 1 and 90 at 2.
计算数组中的元素 (78, 84, 90) 以给出数组的大小/长度,即 3(比最大下标多 1)您应该知道的是索引(每个元素的位置)开始从 0 开始。因此,在您的列表中,78 位于索引 0,84 位于 1,90 位于 2。
I think you should read more on the various methods of Arrays
and List
s and also the difference between ++i
and i++
我想你应该多看一些关于各种方法Arrays
和List
S和也的区别++i
和i++