Java has many ways to store collections of data. You can broadly categorize data collections as either ordered or unordered, and as either containing single values or objects, and as collections of key-value pairs. One of the simplest ways to store order data collections is in an array. They have certain limitations, but are fine for many purposes.
- [Narrator] Java offers many ways to store collections of data. You can broadly categorize data collections as either ordered or unordered. That is, either they keep the items within the collection in the order that you put them in, or the order is up to the compiler in the runtime. And, each collection can either contain either single values or objects, or collections of key value pairs. One of the simplest ways to store an ordered data collection is in an array.
Arrays have certain limitations, but they're fine for many purposes. Here are a couple of important things to know about arrays in Java. First of all, indexing starts at zero. The first item in an array will be accessed at index zero, the second at one, and so-on. Also, arrays can't be resized after they're created. When they're created, you have to fix the size. And, all of the items in the array must be of the same type. Here's some different ways to create an array. I'll start with a simple array of integer values.
I'll start with a bit of output. I'll call this Array of primitives. Then I'll declare an array. The syntax is the type, followed by a pair of brackets. If you want to fix the type when you declare it, you put the number of items in the array between the brackets. But I'm going to show you a bit of shorthand here. I'll call this ints for integers. And I'll set the array with a pair of braces, and then within the braces, the items, separated by commas.
So now this array has three items. I'm going to loop through the array using an iteration loop. I'll generate that with the fori code template. I'll set the value that I'm going to loop until to ints.length. Notice there are no parentheses after length, that's not a method. And then within the loop, I'll output the item using ints open bracket and then i as the index. When I run the application, I see three, six, and nine, one line at a time.
Now you can also loop through the array using a foreach loop. And notice that IntelliJ IDEA has a visual indicator saying, "I can do this better." I'll place the cursor after the for keyword and use an intention action and replace with foreach. When I run the code, the result is the same, but the code is a little bit cleaner, and there are fewer intermediate variables being created. You can also use this sort of syntax with strings. So I'll create a heading Array of strings.
Then I'll create a string array starting with the string type, followed by a pair of brackets. I'll name this colors. And I'll create the array of items with a comma delimited list, just as before. This time I'll start with a foreach loop from the beginning. And the item type will be a string. Its name will be color. And I'm getting it from the colors array. And within the loop, I'll output the current color. And now I have an array of strings with red, green, and blue.
You can also declare an array with a specific number of items, like this. Let's say I wanted to create an array referencing this complex type ClothingItem. Each of the items in the array will be an instance of that class. So I'll start with ClothingItem, open bracket close bracket. I'll name this items. And then I'll instantiate it with new ClothingItem, and I'll pass in a value of three into the second pair of brackets. So now this array has three items.
Initially they'll be null, but if I want to set them individually, I can reference them by the index. The indexing starts at zero, so items bracket zero close bracket means the first item. And I'll instantiate that with new ClothingItem. And I'll pass in shirt as the type, L as the size, and 15 as the price. Now I'll duplicate that line a couple of times. For the next two items, the index will be one and two.
I'll change the types to pants and hat. I'll change the size to 32 for the pants, and the hat to medium. And I'll set a couple of different prices. Now I'll loop through the clothing items, just like I did with the strings, using a foreach loop. And I'll output the current item. And when I run the code I see all three items listed, and the format is determined by the toString method of the class itself. Now let's say I didn't add an item into item two.
I'll just comment out that line of code. And now the last item will be null. And so that's the default condition of a complex object. If you instantiate it, you get the actual object, but if you don't, the size of the array doesn't change, but any non-initialized items will be null. Next I'll show you how to create a copy of an array. I'll create a new clothing item array, and I'll call this one copied. And I'll create it using this syntax.
Arrays, which is a class from Java.util, .copyOf. There are a bunch of versions of this method. I'm going to use the first version of this method. IntelliJ IDEA already knows that I'm passing in an array of clothing items. That'll be items. And then I need to pass in a new length. If I want the new array to have exactly the same number of items as the old array, I can pass in items.length. And then I'll copy and paste my foreach loop.
And this time I'll loop through the copied array. I'll run the code, and the arrays are identical. Now both arrays are pointing to the same data. Let's say that I wanted to make a change to one of the objects. I might say items bracket zero bracket .set price and I'm going to create a discount. That means my first item, the shirt, is $5. Then I'll output items bracket zero bracket.
But I'll also output the object that's referenced by the copied array, and watch what happens. I set the price from the items array but I affected both arrays, and that's because, as I described previously, I have two arrays, but they're pointing at the same objects in memory. So once again, things to remember about arrays in Java. You can't resize them after they've been created, all items in an array have to be of the same type, and when you copy an array, you'll have two arrays, but they'll both be referencing the same objects.
- Debugging Java code
- Handling exceptions
- Creating custom classes
- Working with inheritance
- Managing data collections
- Using Java packages and libraries
- Preparing a Java application for deployment