- At this point you're probably already familiar with arrays, but let's go over some of the basics. An array is a container object that holds a fixed number of values of a single data type. The length of an array is established when the array is created. Once the array is created the length cannot be changed. To reference elements in an array we use an index value. That index value represents the location of the element in the array. The index values always start at zero and go to length -1.
So if you have 10 elements, the first element is always gonna be in position zero. And if there's 10, the last one will be in position 9. Because you have 9 places plus the position zero, makes 10. The values in the array are stored in consecutive memory locations. The syntax for an array includes the data type[ ] = new data type[size]. Or, you can specify an initial list of values to be included in the array, and the size will be calculated automatically.
There is one caution, beware of the out-of-bounds error. Since arrays are a fixed size, if you try to access data at an index value greater than the size of the array, your program will end abruptly during execution. This is not checked ahead of time by the compiler. But remember our section on error handling, that it might be helpful to add a try/catch for an index out-of-bounds error. Arrays can contain any of the primitive data types. Let's take a look at some examples. I have a class called Array Examples, and inside my class I just created a few sample arrays, just to review.
The first one on line 13 is an integer array. I start by declaring the data type int, and then I have an [ and a ]. The name of my array, in this case I just called it "ages", is equal to a new int array, with 20 values. Remember that an array is a container object, and just like when we created classes in objects, we had to instantiate out objects by using the keyword "new", the same thing applies to arrays.
So when you're creating a new array, such as int[ ] ages = you have to say "new int". When you declare an array and you give it a list of values, you don't have to use the keyword "new". So on line 14, I have a double array with my square brackets called "prices", and it contains four values: 5.25, 6.50. 2.30. and 10.75. The length of that array will be four, but remember the array index value starts at zero and goes to one less than the length.
So starting on line 16, I wanted to add the values together, so I have double total = prices, at position 0 plus prices at position 1, prices at position 2, and finally, prices at position 3. If I tried to access prices at position 4, I was would get an array index out-of-bounds error. The next array is actually a Boolean array. You can create an array that holds a list of values that are either true or false. I name this array "responses" and I just set it equal to "true, false, false, true, true".
We can also have character arrays. For example, it might be handy to have an array that includes all the vowels. So I have character [ ] vowels = {"a", "e", "i", "o", "u"}. Note there are single quotes around each vowel because this is a character array, not a string array. And the last array is an example if you wanted to have an array that can hold 10 names, you might declare String [ ] names = new String [10]; This program contains of the examples of the types of arrays that you might want to create.
Released
6/29/2015- Getting started with parsing
- Reviewing data types
- Using decisions
- Creating user-defined methods
- Command-line debugging
- Exploring the Java API
- Creating and instantiating classes
- Working with interfaces
- Storing items with arrays
Share this video
Embed this video
Video: Storing items with arrays