Chapter 8 Flashcards

1
Q

What are Arrays? What’s the difference with ArrayLists?

A

An Array is a programming language construct used to organize a list of objects and values of a primitive data type.

The are two important differences with ArrayLists:

  1. An ArrayList can only store objects. An Array can store primitive data types
  2. An Array is not dynamic, it doesn’t shrink when elements are removed.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

An Array element has a special syntax. What are some of its commands?

A

int[] scores = new int[10]
declares an Array called scores of type int and length 10 (with index going from 0 to 9).

scores[2]
returns the value at index 2 in the array

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

How do you initialize an array? What must be done carefully?

A

Example:
for (int index = 0; index < 15; index++) {
list[index] = index * 2;
}

It’s important that the loop doesn’t try to add an element to an index that is not defined in the array. In other words, we must not go over the size of the array.

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

What is the name of the constant that hold the size of an array?

A

this.length stores the size of the array. Not the last index of the array.

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

What does the following code do?


count[ i - 3 ]++;

A

This code increments by one the value stored inside the array “count” at index “i-3”.

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

What are the two formats with which an array is declared? Which one is preferred?

A

double[] prices;
double prices[];

The first is usually preferred as more readable.

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

What is an initializer list?

A

It’s a way of declaring the values present inside an array (without the need of even writing the size of it)
->
the initializer list can only be used in the declaration.

int[] units = {147, 323, 89, 933, 540, …}

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

Can elements of an array hold object references?

A

Yes, they can. Since an array is already an object, this means that an array is holding a reference to a space in memory that then stores other references to other objects.

This means that for each object reference inside an array. The array indicates an address in the memory that then indicates another address.

[] array
V
[][][][][][] elements of the array
V
[][][][][][] objects pointed by each of the elements.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the signature of the main method indicates?

A

The main method (main(String[] args) indicates that it takes an Array of String as parameters.

These values come from command-line arguments that are provided when the interpreter is invoked.

With the command:

(File name is NameTag):
public static void main(String[] args) {
System.out.println();
System.out.println(args[0]);
System.out.println("My name is " + args[1]);
}

> is command line

Output:
> java NameTag Howdy John

Howdy
My name is John

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

What is the syntax for declaring methods with a variable length parameter list?

Can it accept other parameters?
Other variable length parameters?

A

Example:
public double average (int … list)
int = element type
… = indicates that the number of parameters is variable
list = name of the array in which the values will be stored in.

Yes, the definite parameters must come before the one with a variable length.

No, only one parameter with variable length is acceptable.

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

How many dimensions can an array have?

What is a ragged array?

A
An array can have infinitely dimensions:
int[] 1 dimension
int[][] 2 dimensions
int[][][] 3 dimensions
int[][][][][][][][][][][]...

A ragged array is a specific multidimensional array that inside a dimension contains arrays with different lengths.

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

How do you create a polygon in JavaFX?

A
Example:
double[] hullPoints = {200, 25, 240, 23, 153, ...};
Polygon name = new Polygon(hullPoints);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly