Arrays Flashcards
1) Array creation is a two-stage process:
- declare an array variable; e.g. double[ ] temperature; \this reserves a space in the memory
in which the address of the array
is stored, not the actual array. - allocate memory to store the array elements. e.g temperature = new double [7]; \creates the space in memory
for an array of the given size
and element type
Q: Can we change the array size once it has been set?
A: No
Always make sure you create an array that is big enough for your purpose!
2) Do the array elements have default values?
Yes
Number types are set to 0
char will be set to Unicode character that represents ‘empty’
boolean set to false
3) The two stages of array creation (d________ and a________- memory space for the elements) can also be combined into one step as follows:
double[] temperature = new double[7];
declaring
allocating
4) Array Elements
The individual elements are uniquely identified by an additional i______ v_______.
These index values are always contiguous integers.
This index value is always enclosed in square brackets, so the first temperature in the list is identified as
temperature[0]
Arrays can only hold p_________ data types and S__________. Nothing else!
index values
primitive
Strings
5)
Usually, when an array is created, values will be added into it as the program runs.
If, however, all the values of the array elements are known beforehand, then an array can be created without the use of the new operator by initializing the array as follows:
double[] temperature = {9, 11.5, 11, 8.5, 7, 9, 8.5} ;
Q: What is the value of the element at index 0?
Q: How many elements are in the array?
Q: What is the index value of the last element?
A: 9
A: 7
A: 6
6) i as the index value
If we wanted to return all of the values in an array, we could use a ‘for’ loop.
e.g.
for(int i = 0; i<7; i++) // note, loop counter runs from 0 to 6
{
System.out.println(“enter max temperature for day “+(i+1)); \ the loop counter is being used to display day number
temperature[i] = keyboard.nextDouble(); // use loop counter
}
What day will this display?
1
7) .length
Arrays have a built-in feature that returns the length of an array.
It is accessed by using the word ‘l_______’ after the name of the array.
e.g.
System.out.print(“number of temperatures = “);
System.out.println(temperature.length); // returns the size of the array
length
8) .length
This attribute can be used in place of a fixed number in the for loop as follows:
for (int i = 0; i < temperature.length, i++)
{
// code for loop goes here
}
9) Arrays as Attributes
Note from the Temperature project that we were using an array of temperatures as an a_______ of the Temperature class.
We can use arrays as attributes of a class.
e.g.
public class Temperature
{
double [ ] temperature; //class attributes
public Temperature( ) { temperature = new double[4]; }
attribute
10) Collection Classes
A class can contain many items of the same type; a list of temperature readings, a list of print jobs, a list of phone numbers, a tutorial group of students.
Classes which contain collections of similar items are called c________ classes.
collection classes
14) Flexible-Size Collections
The ArrayList clas is available in one of the libraries that comes as a standard part of a J____ e________. It is defined in the java.util package.
ArrayList is an example of a c__________ class. Collections can store an arbitrary number of elements, with each element being another o_________.
Java environment
collection
object
16) There are at least three important features of the ArrayList class that you should observe:
1) It is able to increase its internal capacity as r________; as more items are added, it simply makes enough room for them.
2) It keeps its own p_____ c________ of how many items it is currently storing. Its size method returns the number of objects
currently stored in it.
3) It maintains the o_______ of items you insert into it. You can later retrieve them in the same order.
required
private count
order