final int VALUES_LENGTH = 100; double[] values = new double[VALUES_LENGTH]; int valuesSize = 0;
values[valuesSize] = x; valuesSize++;
int valuesSize = 0; Scanner in = new Scanner(System.in); while (in.hasNextDouble()) { if (valuesSize < values.length) { values[valuesSize] = in.nextDouble(); valuesSize++; } }
for (int i = 0; i < valuesSize; i++)
{
System.out.println(values[i]);
}
for (int i = valuesSize - 1; i >= 0; i--) System.out.println(values[i]);
values.length--;
valuesSize--;
values.remove(values.size() - 1);
values[valuesSize] = 0;
for (int i = 0; i < values.length; i++) { values[i] = 0; }
for (int i = 0; i < values.size(); i++) { values.set(i, i * i); }
double total = 0; for (double element : values) { total = total + element; }
double average = total / values.size(); // For an array list
What needs to be done to adapt this algorithm to a completely filled array?
values.size()
to valuesSize
values.size()
to values.length
public class Bank { private ArrayList<BankAccount> accounts; public int count(double atLeast) { int matches = 0; for (BankAccount account : accounts) { if (account.getBalance() >= atLeast) matches++; // Found a match } return matches; } . . . }
BankAccount largestYet = accounts.get(0); for (int i = 1; i < accounts.size(); i++) { BankAccount a = accounts.get(i); if (a.getBalance() > largestYet.getBalance()) largestYet = a; } return largestYet;
if (accounts.size() == 0) return null; BankAccount largestYet = accounts.get(0); . . .
Consider the following modification of the algorithm to find a maximum in an
array of double
values.
double largestYet = 0; for (double value : values) { if (value > largestYet) largestYet = value; } return largestYet;
Which of the following statements is correct?
public class Bank { public BankAccount find(int accountNumber) { for (BankAccount account : accounts) { if (account.getAccountNumber() == accountNumber) // Found a match return account; } return null; // No match in the entire array list } . . . }
Consider this modification of the linear search algorithm for an array of
double
values.
public int find(double[] values, double number) { for (int i = 0; i < values.length; i++) { if (values[i] == number) return i; } return -1; }
Which of the following statements is true about this algorithm?
return null
for
loop is always traversed values.length
times