export_big java chapter 7 arrays terms and impo codes Flashcards

1
Q

Chapter Goals

A

To become familiar with using arrays and array lists

To learn about wrapper classes, auto-boxing and the generalized for loop

To study common array algorithms

To learn how to use two-dimensional arrays

To understand when to choose array lists and arrays in your programs

To implement partially filled arrays

T To understand the concept of regression testin

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

Arrays?

A

Sequence of values of the same type

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

Construct array:

A

new double[10]

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

Store in variable of type

A

double[]:

double[] data = new double[10]

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

When array is created, all values are initialized depending on array type:

A

Numbers: 0

Boolean: false

Object References: null

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

Using the value stored: array

A

System.out.println(“The value of this data item is “

\+ values[2]);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

ArrayList

A

ArrayList class manages a sequence of objects

Can grow and shrink as needed

ArrayList class supplies methods for many common tasks, such as inserting and removing elements

ArrayList is a generic class:

ArrayList

collects objects of type parameter T:

size method yields number of elements

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

Wrapper Classes

A
For each primitive type there is a wrapper class for storing values of that type:
  Double d = new Double(29.95);  

Wrapper objects can be used anywhere that objects are required instead of primitive type values:

ArrayList  values= new ArrayList ();   

data.add(29.95);   

double x = data.get(0);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Auto-boxing

A

Automatic conversion between primitive types and the corresponding wrapper classes

Double d = 29.95; // auto-boxing; same as

     // Double d = new Double(29.95);

double x = d; // auto-unboxing; same as

   // double x = d.doubleValue(); 

Auto-boxing even works inside arithmetic expressions:

d = d + 1;

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

Auto-boxing and Array Lists

A

To collect numbers in an array list, use the wrapper type as the type parameter, and then rely on auto-boxing: ArrayList values = new ArrayList

values.add(29.95);

double x = values.get(0);

Storing wrapped numbers is quite inefficient

Acceptable if you only collect a few numbers

Use arrays for long sequences of numbers or characters

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

The Enhanced for Loop

A

Traverses all elements of a collection:

double[] values = …;

double sum = 0;

for (double element : values)

{

sum = sum + element;

}

Read the loop as “for each element in values

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

The Enhanced for Loop

A

Works for ArrayLists too:

ArrayList accounts = …;

double sum = 0;

for (BankAccount account : accounts)

{

sum = sum + aaccount.getBalance();

}

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

Array length

A

maximum number of elements in array

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

7.3.1 Filling

A

This loop fills an array with squares (0, 1, 4, 9, 16, …). Note that the element with

index 0 contains 0 2 , the element with index 1 contains 1 2 , and so on.

for (int i = 0; i

{

values[i] = i * i;

}

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

Problem Solving: Adapting Algorithms

A

By combining fundamental

algorithms, you can solve complex

programming tasks.

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

7.3.2 Sum and Average Value

A

.When the values are located in an array, the code looks much simpler:

double total = 0;

for (double element : values)

{

total = total + element;

}

double average = 0;

if (values.length > 0) { average = total / values.length;}

17
Q

7.3.3 Maximum and Minimum

A

double largest = values[0];

for (int i = 1; i largest)

{

largest = values[i];

}

}

18
Q

7.3.4 Element Separators

A

Print the separator before each element in the sequence except the initial one (with index 0)

for (int i = 0; i 0)

{

System.out.print(“ | “);

}

System.out.print(values[i]);

}

19
Q

7.3.5 Linear Search

A

int searchedValue = 100;

int pos = 0;

boolean found = false;

while (pos if (values[pos] == searchedValue)

{ found = true; }

else

{ pos++; } }

if (found) { System.out.println(“Found at position: “ + pos); } else { System.out.println(“Not found”);

20
Q

Common Array Algorithm: Removing an Element

A

Array list ⇒ use method remove

Unordered array ⇒

Overwrite the element to be removed with the last element of the array

Decrement the variable tracking the size of the array

values[pos] = values[valuesSize - 1];

valuesSize–;

21
Q

Common Array Algorithm: Removing an Element

A

Ordered array ⇒

Move all elements following the element to be removed to a lower index

Decrement the variable tracking the size of the array

for (int i = pos; i < valuesSize - 1; i++)

{

values[i] = values[i + 1];

}

valuesSize–;

22
Q

Common Array Algorithm: Removing an Element

A

https://s3.amazonaws.com/classconnection/655/flashcards/7082655/png/lzflr8-14A8A5872A2168F9A74.png

23
Q

Common Array Algorithm: Inserting an Element

A

Ordered array ⇒ Start at the end of the array, move that element to a higher index, then move the one before that, and so on until you finally get to the insertion location Insert the element

Increment the variable tracking the size of the array: if (valuesSize

{ for (int i = valuesSize; i > pos; i–)

{ values[i] = values[i - 1]; }

values[pos] = newElement;

valuesSize++; }

24
Q

Common Array Algorithm: Inserting an Element

A

Array list ⇒ use method add

Unordered array ⇒

Insert the element as the last element of the array

Increment the variable tracking the size of the array

if (valuesSize < values.length)

{

values[valuesSize] = newElement;

valuesSize++;

}

25
Common Array Algorithm: Inserting an Element
https://s3.amazonaws.com/classconnection/655/flashcards/7082655/png/otr2re-14A8A5A42935BCADB1F.png
26
Common Array Algorithm: Copying an Array
opying an array variable yields a second reference to the same array: double[] values = new double[6]; . . . // Fill array double[] prices = values; Copying an array variable yields a second reference to the same array: double[] values = new double[6]; . . . // Fill array double[] prices = values
27
Common Array Algorithm: Copying an Array
To grow an array that has run out of space, use the Arrays.copyOf method: values = Arrays.copyOf(values, 2 * values.length);
28
Test suite
a set of tests for repeated testing
29
Cycling:
bug that is fixed but reappears in later versions
30
Regression testing:
repeating previous tests to ensure that known failures of prior versions do not appear in new versions
31
Two-Dimensional Arrays
String[][] board = new String[ROWS][COLUMNS]; 
32
How do you declare and initialize a 4-by-4 array of integers? 
int[][] array = new int[4][4];
33
How do you count the number of spaces in the tic-tac-toe board? 
int count = 0; for (int i = 0; i   for (int j = 0; j     if (board[i][j] == ' ') count++;