Introduction to arrays
1. Introduction to arrays
We've worked with single variables to store values. But what if we need to store multiple values of the same type? Instead of creating separate variables for each value, we can use an array.2. Array is ...
An array is a collection of values stored under a single variable name.3. Array is ... a row of lockers!
We can think of it like a row of lockers - each one holds a value. To identify each locker, it has a label or index, which allows us to access it easily.4. Declaring and filling arrays
In Java, we define an array variable using square brackets. For example, if we want to store multiple numbers, we declare an array called `prices` of integers by writing `int[] prices`, and then we assign values using curly brackets. Now let’s make use of the index labels: the array is indexed from zero, so if we store `{10, 20, 30, 40}`, the first number, 10, is at index 0, the second number, 20, is at index 1, and so on.5. Accessing elements
To access an element, we use square brackets with an index number. If `prices` is our array, writing `prices[0]` gives us the first element, `prices[1]` gives the second, and so on.6. Changing value of an element
Arrays’ content is not fixed. We can change the values in an array by assigning a new value to a specific index. For example, if we have our array of prices and want to update the third price, we write `prices[2] = 95`, which replaces whatever was at index 2 with `95`.7. Array length is fixed
While arrays’ values can be changed, the array length cannot. We must decide on the number of elements when creating the array. If an array has five elements, we can't just add a sixth one—it won't fit. Trying to access an index beyond the array's length will cause an error. We can check the size of an array using the `.length` property. If an array has four elements, calling `prices.length` returns 4.8. Printing out values
When we want to see the values in an array, we can print them by first accessing the element and then printing it.9. Trying to print an entire array
If we try to print the values of an array, the output looks strange. That's because Java prints a representation of the array. There are ways around this, but they are out of the scope of this course.10. Arrays of other things
Just like arrays of numbers, we can also create arrays of `Strings`. If we want to store a list of product names, we define a `String` array by writing `String[] productNames`, then assign values inside curly brackets, like so.11. Values in an array
We are not limited to just numbers or `Strings` though. While we can have an array with values of any type, all the values in an array have to be the same type.12. Recap
Arrays might seem complex at first, but they are immensely useful for storing and working with multiple values in an organized way. They allow us to retrieve, update, and perform operations on elements efficiently.13. Let's practice!
Now, let's practice working with arrays in the next exercise!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.