Ch 9: Arrays Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

what are the individual memory locations in an array called

A

elements

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

several consecutive memory locations of the same data type under one name is

A

an array

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

number of elements in an array is called

A

size or length of the array

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

program can refer to an individual element of an array using the array name followed by the element’s number which is called

A

[index or subscript] notice the brackets*

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

an index can be

A

any integer constant, variable, or expression

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

create an array

A

someType[] nameOfArray;

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

create an array with specified number of elements

A

int[] scores = new int [10];

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

what are default values for an array

A

numeric elements are initialized to 0

boolean to false

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

if array elements are of a class type then…

A

the array contains references to objects of that type, these are initialized to null

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

if array elements are references, you have to initialize each element by setting it to a valid reference before the element is used

A
color[0] = new Color (207, 189, 250);
colors[1] = Color.BLUE;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what error do you get when you call a method of a non existing object(the reference is null)

A

nullpointerexception

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

another way to declare and create an array list is to list explicitly like

A

int [] scores = { 95,97,99, 100 };
String[] names = { “Vika”, “hello”, “justin” };

Color [] rainbowColors =
{
Color.Red, Color.Orange, Color.Grey, Color.Pink
};

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

once an array is declared and initialized, either with the new operator or with a list of values, it is not possible to ?

A

change its size

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

declare an array of 100 integer elemnts

A

final int MAXCOUNT = 100;
int[] a = new int [MAXCOUNT];

int[99] is the biggest since index begins at 0

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

how do you find the size of an array

A

arrayName.length

length acts as a public field that holds the size of an array

In array, length is not a method(as in the String class). It is accessed like a field

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

how are arrays passed to methods?

A

always as references.
if an array is passed to a method, the method gets the address of the original array and works with the original array, not a copy, therefore a method can change an array passed to it.

17
Q

swap two element’s places in an array

A
public void swap(double[] a, int i, int j) {
  double temp = a[i];
  a[i] = a[j];
  a[j] = temp;
}
18
Q

declare and initialize a 2-d array of doubles

A

int rows =2;
int cols =3;

double[] [] a = new doubles [rows] [cols];

double[] []  b =
{
  {0.0, 0.1, 0.2}.
  {1.0, 1.1, 1.2}
};
// each row is an array
// both are two rows and two columns
19
Q

be careful when using break in nested loops, why

A

a break statement inside a nested loop will only break out of the inner loop

20
Q

define traversal

A

procedure in which every element of a list is “visited” and processed once. kind of like your order being taken at a mcdonald’s drive thru :P

21
Q

rewrite
String[] names = new String[newGuests];
for (int i = 0; i

A
for (String str : names) {
  // do something with str
}

or

double sum = 0;
for (double sum : scores) {
sum += num;
}

double average = sum/scores.length;

22
Q

are you able to change the values of an array using an for each loop?

A

no, you cannot! the variable that refers to an element holds only a copy of the element

for each loop does not give you access to the index of the “visited” element

23
Q

use a for loop to find the largest number of an array

A

double aMax = 0;

for(int k=0; k aMax ) {
aMax = a[i];
}
}

}