 Up until now, we've been using arrays. They're very useful, but they have one problem. They have a fixed size. You have to know in advance how many elements the array will have. What happens if we need to have an arbitrary number of items? For that, we use an array list. Let's write a program that asks the user for prices until they enter zero, and then list those prices as a percentage of the maximum value. To use array lists, you need to import java.util.arraylist. To create an array list, you must specify the type of object that it contains, and that type must be a reference type. In our example, we'll want an array list of double objects, with a capital D, because those are reference classes, as opposed to small d-doubles, which are primitives. You specify the array list type in angle brackets, and then you give the array list a name, and say that it's a new array list. Use the angle brackets again, and parentheses. If you want to, you can repeat the data type inside these angle brackets, on the right-hand side, but you don't need to. Here's a do-while loop to read in the numbers until we get a zero, the sentinel value. We'll set a small d-double, one price to 0.0, and then put up the prompt, and read the value. If the price is greater than zero, then we'll add it to the array list, and we'll do that as long as one price is not equal to zero. To add an item to an array list, use the add method, priceless.add one price. Hold on, you might be thinking. Didn't you just say that we need capital D double objects inside our array list? But here you gave a small d-double value to add. What's going on here? In a similar way that Java promotes an int to a double when one is needed, Java automatically converts a primitive to the corresponding object when the context demands it. In this example, the right-hand side, 29.95, is a primitive, and the left-hand side is a double object. Behind the scenes, Java is actually doing this. It's calling the value of method in the double class to wrap the primitive 29.95 as a double object. This conversion is called boxing. The reverse from an object to a primitive is called unboxing. In the second statement, we're multiplying d-price, an object, by .90, a primitive. Behind the scenes, Java is unwrapping the object, extracting the primitive value, and using that in the calculation. And that's why it's okay to do this. Our primitive one price is being put into a capital D double box, and the price lists add method is happy. Now let's do the rest of the calculations, but only if we really have items in the price list. We'll set the number of prices to be price list dot size, and that's the method that tells how many elements are currently in the array list. If the number of prices is greater than zero, then we have work to do. Otherwise, the user didn't enter any data. We use the get method to get the first item in the price list as the current maximum value. We'll set double max price to be price list dot get zero. Just as with arrays, the first item in an array list has an index number of zero. Now go through the remaining prices to find the maximum. For int i equals one, i less than n prices, i plus plus, if price list dot get at that index value is greater than the maximum price, then the maximum price becomes that value. The next thing I'm going to do is go through the price list again and change it in place to percentages. I'll use the get and set methods to do this. Technically, I don't need to change the array in place. I could print out the percentages without storing them back in the array list, but I needed an excuse to show the set method. Let's go through the entire array list starting at zero and calculate the percentage of the maximum to be the price list item at the given index divided by my maximum price. And then I'm going to set the value at index i to that new value that I calculated. By the way, there's a lot of automatic boxing and unboxing going on in lines 36 and 37. See if you can figure out where it's happening. Now it's time to print out the results. And I'm going to use a different way of iterating through the price list. I'll say four double price in my price list, system dot out dot print f. Let's use three places to the right of the decimal point and a percent sign. Because percent sign is special, it introduces formatting. To get a true percent sign, I need two percent signs in a row and print the price. And then make sure that my output ends with a new line. Let's compile that and let's run it. Let's say $3.95, $20, $17.95, $19.95, and $7.25. And then that's our last price. And there are the percentages of the maximum price. When you're working with array lists, get and set are the two methods that you'll use the most. The ArrayList class has other useful methods, but that's the subject of another video.