From the course: Java 8 Essential Training

Using simple arrays - Java Tutorial

From the course: Java 8 Essential Training

Start my 1-month free trial

Using simple arrays

- I've used simple arrays a number of times in this course but I'd like to talk a little bit more about them. They're one of the easiest ways to store ordered data in Java but there are certain limitations. I'm working in the project's simple arrays and I'll add a bit of code here declaring an array of primitive values. Arrays can point to any kind of variable, primitives, or objects. I'll declare a set of integers. I'll start with the data type int and then add a pair of brackets. And then, I'll set a name for the array, int. You can actually place the brackets either after the data type of you can place them here, after the array name. You shouldn't do both but it's preferred to place them after the data type. Now, I'll initialize the array's value with a pair of braces and I'll add in a comma delimited set of values that match the data type, 3, 6, and 9. This array now has three values. The array is not re-sizable. Once it's been set, its size can't be changed. but you can change the values at each index position. Next, I'll use an incremental loop and I'll loop through the array and for the second part of the loop declaration, I'll set a ceiling of ints.length an then within the loop, I'll output the value at that position. So I'm starting at zero and going through two. And the result is outputting values of 3, 6, and 9. Now let's see what happens if I change the values. I'll set the values initially as 9, 6, and 3. And then, I'll run the code again. And I see that the array order is exactly what I declared but it's also possible to sort an array. So now, I'll use a class named arrays and I'll call this sort method. Notice that there are versions of the sort method for all the different primitive data types. And there's also a set of methods called parallel sort. Those were added in Java 8. I'm going to just choose the sort method and I'll pass in the int array and then I'll run the code again and I see that after sorting, the values are back in ascending order, from three to nine. An array can point to any sort of object and I'll use the object that we use the most, a string. I'll declare a string array, I'll name it strings and once again, I'll start with a pair of braces, and I'll add in three values, red, green, and blue. Then, I'll use a for each loop to loop through the values. Each time through the loop, I'll be working with a string and I'll name it color. And I'll get its values from the array that I named strings. Within the loop, I'll output the color value and I'll run the code and I see red, green, and blue. But now, let's see what happens when I sort that array. Once again, I'll use Arrays.sort and I'll pass in the array. And I'll run the code and this time, I get blue, green, red. The colors are now in alphabetical order. When you declare an array, you can set the initial size without declaring the values. Just as before, I'll start with a data type. I'll use int again and I'll name this array sized and now instead of using braces, I'll start with the key word, new, and then the data type again, end brackets. But this time, I'll pass in a numeric value. That's the number of items that I want to be available in the array. Then, I'll use a for each loop. Each time the loop, I'll be looking for an integer and the name of the integer will be value and I'll get that value from the sized array. With an array of primitive values declared in this way, the values will all be the default for that data type, zero for the numeric primitives, false for Booleans, and so on. But now, I'll add a bit of code before this and I'll set the values explicitly. I'll add an incrementing loop and I'll loop from zero to the size of the array with sized.length. Then within the loop, I'll set the value of the item at that index. I'll use the expression sized and then the index in brackets and then I'll set the value as i * 100 and I'll run the code again and I see that the values have been updated as I expect. Notice that the first value is still 0. It's the value i * 100, which is still 0. But then each of the other index values has also been multiplied by 100. Finally, I'll describe how to copy an array using a method of the system class called array copy. I'll declare another integer array and I'll name this one copy and as I did before, I'll use the keyword new and the data type again but I'll set the size of this array to 5. Then, I'll call system.arraycopy. This method takes five arguments. The first argument is the original array then there's an integer, which is the beginning position from where you want to copy. Then the third argument is the destination array and then a beginning position where you want to copy to and a length, the number of items you want to copy. I'll pass in sized and 5, meaning that I want to copy from the sized array, starting at index 5, halfway through the array. Then, I'll set my destination array to copied and pass in values of 0 and 5. I'm copying to the range, starting at 0, going through 5. Then, I'll output the results. I'll use a for loop and I'll start with int then value then copied and then within the braces, I'll output the value. Notice I'm reusing the variable value because it's scope in the original declaration was limited to this for loop so I can reuse the variable name. I'll run the code and I see that my copied array has the values starting at 500.

Contents