Arrays Flashcards

1
Q

1) Array creation is a two-stage process:

  1. 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.
  2. 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

A: No

Always make sure you create an array that is big enough for your purpose!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

2) Do the array elements have default values?

A

Yes

Number types are set to 0
char will be set to Unicode character that represents ‘empty’
boolean set to false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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];

A

declaring
allocating

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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!

A

index values
primitive
Strings

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A: 9
A: 7
A: 6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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?

A

1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

length

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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
}

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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]; 
 }
A

attribute

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A

collection classes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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_________.

A

Java environment
collection
object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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.

A

required
private count
order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly