-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dwarves.java
89 lines (64 loc) · 1.62 KB
/
Dwarves.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.util.*;
/*
* This is the starting point code for Lab: Dwarves.
*/
public class Dwarves {
// The collect to hold the names
private java.util.ArrayList<String> dwarves;
private String name;
public Dwarves() {
//dwarves = new java.util.ArrayList();
dwarves = new java.util.ArrayList<String>();
}
/**
* Add the names of the dwarves to the collection.
*/
public void addNames() {
add("Doc");
add("Grumpy");
add("Happy");
add("Sleepy");
add("Bashful");
add("Sneezy");
add("Dopey");
}
/**
* Add a single name to the collection
* @param name The name to be added
*/
public void add(String name) {
dwarves.add(name);
}
/**
* A simple method to print out the contents of
* the collection, using the for loop.
*/
public void print() {
System.out.println("Print out the list using a for loop:");
for(String name : dwarves){
System.out.print(name + " ");
}
System.out.println();
}
/**
* A second method to print out the contents of
* the collection, using an Iterator.
*/
public void print2() {
System.out.println("Using an iterator and while loop:" ) ;
Iterator<String> it = dwarves.iterator();
while(it.hasNext())
System.out.print( it.next() + " ");
}
/**
* The application method
* @param args Command-line parameters
*/
public static void main(String[] args) {
// instantiate the Dwarves class
Dwarves theGuys = new Dwarves();
theGuys.addNames();
theGuys.print();
theGuys.print2();
}
}