Chapter 9 - Arrays Flashcards

1
Q

What is a class?

A

It stores a group of related data, and it stores the methods that operate on that data.

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

What is an Array?

A

An Array is a limited version of a class.

  • Similar to a class it stores related data, but it does not store methods.
  • Also, unlike a class and Arrays data must all be the same type.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are Arrays composed of?

A

Each part of an Array is called an element.

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

How does a class access one of its members?

A

Using dot notation

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

How does an Array access one of its elements?

A

Using square brackets around an index

I.E. [x]

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

How are index values determined in an Array?

A

The first index number starts at 0 and the final number is the number of elements in an array minus 1.

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

What command would you type to change the value of an array element?

A

The name of the Array = new value

I.E: phonelist[0] = 555-55-5555

(Name of the array and element number) = new Phone number

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

What command would you type to print out a phone number?

A

System.out.println(Arrayname and element number);

I.E: System.out.println(phoneList[1]);

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

What is an Array considered?

A

A Variable

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

How do you declare and Array as a variable?

A

“element-type”[] “array-variable”;

Examples:

int[] ids;

double[] workHours;

String[] names;

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

What are Arrays in Java?

A

Objects

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

After declaring an Array as a variable, what must be done to create the Array as an object?

A

You must use the “new” operator.

“array-variable” = new “element-type”[array-size];

Examples:

long[] phoneList; (this establishes the array variable)

phoneList = new long[10]; (this creates the array)

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

How do you combine and arrays declaration as a variable and creation?

A

“element-type”[] “array-name” = new “element-type”[length of array];

Example: long[] phoneList = new long[10];

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

How would you write a statement that declares, creates and assigns a 100-element array that stores book titles?

A

long[] bookTitles = new long[100];

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

What is an array initializer?

A

A single statement made up of an array declaration, creation and {} assignment.

Example: “element-type”[] “array-name” = {“element-values-list”};

String[] students = {“Hamoud”, “Lee”, “Brandon”};

*Note that the array is created without the “new” operator.

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

If an Array initializer is used, what is the size of the array?

A

The number of elements in the initialization list.

For example: {“John”, “Michael”, “Tom”}; would be an array size of 3

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

What does it mean when an array is instantiated?

A

It means the elements are assigned default values according to the array data type.

Basically, if an array is not given an assigned value instead of giving a complier error it will just assign default values to the array.

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

What are the default values when an array is instantiated? (They’re also the default values for instance variables and class variables)

A

Example:

float[] gpas = new float[1000];

String[] states = new String[50];

Default values:

This would return 0.0 1000 times

This would return null 50 times

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

How would you print and array that’s been initialized like this:

String[] colors = {“blue”, “gray”, “lime”, “teal”, “yellow”}; ?

A

for (int i = 0; i < colors.length; i++) {

System.out.println(colors[i]); }

(note that colors.length is the name of the array, and calls the length of the array since it’s assigned a set number of elements, in this case 5)

20
Q

How do you obtain an array’s length?

A

Specify the array name, dot, and then “length”

Example:

bookTitles.length

21
Q

“length” has two uses, what are they?

A
  • a String method
  • an array property

only use parentheses with Strings.

ANSY = arrays no, strings yes

22
Q

What is a partially filled array?

A

When you use some of an array’s elements, but not all.

23
Q

What do you have to do if you have a partially filled array?

A

Keep track of the number of filled elements in the array so you can process the filled elements differently from the non-filled elements.

Typically done through a variable that keeps track of the filled elements.

24
Q

What happens if you assign one array reference variable to another array reference variable?

A

Both array reference variables then point to the same single array object.

This is always the case with objects and their associated reference variables.

25
Q

Why would you want to copy an array?

A

To copy the original and point to different array objects. To do it, you need to assign array elements one at a time.

Example attached: The arrays elements are assigned one at a time, with February copying January. Then the code changes the “1” element, to a different price, meaing the second element in the array. Then prints them out.

26
Q

What is a histogram?

A

A graph that displays quantities for a set of categories. It indicates the category quantities with bars.

Shorter bars = smaller quantities

Longer bars = larger quantities

Example attached

27
Q

When using a histogram, what type of array can you use to track outcomes?

A

You can use a frequency array

For example: if coding histogram to track the number of heads from 3 coins being flipped 100 times

frequency[0] = number of time no heads show up

frequency[1] = number of times 1 heads shows up

frequency[2] = number of times 2 heads shows up

frequency[3] = number of times 3 heads shows up

28
Q

What are the elements in a frequency array called?

A

A bin

29
Q

What happens to a bin in a frequency array after each simulation?

A

1 is added to the appropriate frequency bin

30
Q

How do you go about searching an array for a particular value?

A

In Pseudocode:

i <- 0

while i < number of filled elements(the [number])

if “array-name”[i] equals the searched-for value

then do something and stop the loop

increment i

31
Q

What is a simple sorting algorithm?

A

The Selection Sort algorithm.

Example:

for (i<–0; i < list’s_length; i++)

find the smallest value in list from list[i]

down to the bottom of the list

swap the found value with list[i]

This will find the smallest value in the list from list[i] down to the bottom of the list and swap the found value with list[i]

Visual attached

32
Q

What should you use to sort an Array if you have multiple arrays to sort?

A

A generic sorting method.

This method recieves and array as a parameter (any array in the code) and sorts it.

33
Q

What has to be done to make the sorting method generic so that it can be used by multiple classes?

A

Put the sort method in a utility class.

34
Q

What is a utility class?

A

A class with general-purpose methods that other classes can easily use.

To make the methods easy to use, use class methods (as opposed to instance methods).

35
Q

What happens if you make the sort method an instance method?

A

You’re required to instantiate the sort method’s enclosing class prior to call the sort method.

Example: assuming the sort method’s enclosing class is named Sort:

Sort s = new Sort();

s.sort(studentIds);

36
Q

What happens if you make the sort method a class method?

A

You are not required to instantiate the sort method’s enclosing class prior to calling the sort method.

Instead, you simply prefix the sort method call with the class name and then dot.

Example: assuming the sort method’s enclosing class is named Sort:

Sort.sort(studentIds);

37
Q

What should you use if you have a group of related data that’s organized in a table format?

A

Two-dimensional array.

38
Q

How do two-dimensional arrays differ from regular arrays?

A

Same syntax except they use a second pair of []’s.

For example: int[][] x = {{1, 2, 3}, {4, 5, 6}};

The first index identifies the row and the second index identifies the column position within a row.

39
Q

What are the two ways to assign values into a two-dimensional array’s elements?

A
  • An array initialzier (only useable if you know the values)
  • Assignment statements

Examples:

Initializer = int [][] x = {{8, -2, 4}, {1, 0, 5}};

Assignment = attached

40
Q

What command allows you to loop through the rows in a two-dimensional array?

A

“array-name”.length

41
Q

What command allows you to loop through the elements within a particular row in a two-dimensional array?

A

“array-name”[0].length

Example: attached

42
Q

What must you ensure in order to create and array of objects?

A
  • Instantiate the array with the new operator
  • Instantiate each object that’s stored in the array with individual new operators
43
Q

What is a for-each loop?

A

Alternative to the traditional for loop. Used when you want to iterate through all array elements and don’t know or care which particular elements are loaded.

Example:

for (“element-type” “element-reference” : “array-reference”)

Spoken sounds like: For each “element-reference” in “array reference”

Visualized in attachement

44
Q

How does a for-each loop reduce code clutter?

A
  • It avoids index initialization, testing and incrementing.
  • Avoding indexing simplifies element method calls, instead of calling primes[i] you just call p
45
Q

What does a for-each loop not use when looping through its elements?

A

In index variable