Java Reference
In-Depth Information
60 */
61
public double
getBalance()
62 {
63
return
balance;
64 }
65
66
private int
accountNumber;
67
private double
balance;
68 }
Output
Size: 3
Expected: 3
First account number: 1008
Expected: 1008
Last account number: 1729
Expected: 1729
296
297
S
ELF
C
HECK
3.
How do you construct an array of 10 strings? An array list of strings?
4.
What is the content of names after the following statements?
ArrayList<String> names = new
ArrayList<String>();
names.add("A");
names.add(0, "B");
names.add("C");
names.remove(1);
C
OMMON
E
RROR
7.3: Length and Size
Unfortunately, the Java syntax for determining the number of elements in an array,
an array list, and a string is not at all consistent. It is a common error to confuse
these. You just have to remember the correct syntax for every data type.
Data Type
Number of Elements
a.length
Array
a.size()
Array list
a.length()
String