Videos
You're confusing the size of the array list with its capacity:
- the size is the number of elements in the list;
- the capacity is how many elements the list can potentially accommodate without reallocating its internal structures.
When you call new ArrayList<Integer>(10), you are setting the list's initial capacity, not its size. In other words, when constructed in this manner, the array list starts its life empty.
One way to add ten elements to the array list is by using a loop:
for (int i = 0; i < 10; i++) {
arr.add(0);
}
Having done this, you can now modify elements at indices 0..9.
If you want a list with a predefined size you can also use:
List<Integer> arr = Arrays.asList(new Integer[10]);
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:
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);
}
}
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).
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 Strings work as well. That something is the maximum index. Arrays (and ultimately ArrayLists, 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:
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.
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 second element of the list, .get(0) would be the first.
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:
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.
If any part of this is more confusing than it should be, let me know and I'll try to fix it up.
The size member function.
myList.size();
http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html
System.out.println(myList.size());
Since no elements are in the list
output => 0
myList.add("newString"); // use myList.add() to insert elements to the arraylist
System.out.println(myList.size());
Since one element is added to the list
output => 1