export_big java chapter 7 arrays terms and impo codes Flashcards
Chapter Goals
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
Arrays?
Sequence of values of the same type
Construct array:
new double[10]
Store in variable of type
double[]:
double[] data = new double[10]
When array is created, all values are initialized depending on array type:
Numbers: 0
Boolean: false
Object References: null
Using the value stored: array
System.out.println(“The value of this data item is “
\+ values[2]);
ArrayList
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
Wrapper Classes
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);
Auto-boxing
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;
Auto-boxing and Array Lists
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
The Enhanced for Loop
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
The Enhanced for Loop
Works for ArrayLists too:
ArrayList accounts = …;
double sum = 0;
for (BankAccount account : accounts)
{
sum = sum + aaccount.getBalance();
}
Array length
maximum number of elements in array
7.3.1 Filling
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;
}
Problem Solving: Adapting Algorithms
By combining fundamental
algorithms, you can solve complex
programming tasks.
7.3.2 Sum and Average Value
.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;}
7.3.3 Maximum and Minimum
double largest = values[0];
for (int i = 1; i largest)
{
largest = values[i];
}
}
7.3.4 Element Separators
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]);
}
7.3.5 Linear Search
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”);
Common Array Algorithm: Removing an Element
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–;
Common Array Algorithm: Removing an Element
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–;
Common Array Algorithm: Removing an Element
https://s3.amazonaws.com/classconnection/655/flashcards/7082655/png/lzflr8-14A8A5872A2168F9A74.png
Common Array Algorithm: Inserting an Element
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++; }
Common Array Algorithm: Inserting an Element
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++;
}