ThuyKhong 6 lat temu
rodzic
commit
f8e0e666c5

+ 19
- 9
src/main/java/com/zipcodewilmington/PersonHandler.java Wyświetl plik

@@ -11,26 +11,35 @@ public class PersonHandler {
11 11
     }
12 12
 
13 13
     public String whileLoop() {
14
-        String result = "";
14
+        StringBuilder stringResult = new StringBuilder();
15 15
         // assume there is a `counter`
16 16
         // while `counter` is less than length of array
17 17
             // begin loop
18
+        int i = 0;
19
+
20
+       while(i<personArray.length) {
21
+           stringResult.append(personArray[i].toString());
22
+           i++;
23
+       }
18 24
 
19 25
                 // use `counter` to identify the `current Person` in the array
20 26
                 // get `string Representation` of `currentPerson`
21 27
                 // append `stringRepresentation` to `result` variable
22 28
 
23 29
             // end loop
24
-        return result;
30
+        return stringResult.toString();
25 31
     }
26 32
 
27 33
 
28 34
 
29 35
     public String forLoop() {
30
-        String result = "";
31 36
         // identify initial value
32 37
         // identify terminal condition
33 38
         // identify increment
39
+        StringBuilder stringResult = new StringBuilder();
40
+        for (int i = 0; i <personArray.length ; i++) {
41
+            stringResult.append(personArray[i].toString());
42
+        }
34 43
 
35 44
         // use the above clauses to declare for-loop signature
36 45
             // begin loop
@@ -39,23 +48,24 @@ public class PersonHandler {
39 48
                 // append `stringRepresentation` to `result` variable
40 49
             // end loop
41 50
 
42
-        return result;
51
+        return stringResult.toString();
43 52
     }
44 53
 
45 54
 
46 55
 
47 56
     public String forEachLoop() {
48
-        String result = "";
49
-        // identify array's type
50
-        // identify array's variable-name
51
-
57
+        StringBuilder stringResult = new StringBuilder();
58
+        // identify array's type (type of data)
59
+        // identify array's variable-name (variable name you want to call that data)
60
+        for (Person name : personArray)
61
+            stringResult.append(name.toString());
52 62
         // use the above discoveries to declare for-each-loop signature
53 63
             // begin loop
54 64
                 // get `string Representation` of `currentPerson`
55 65
                 // append `stringRepresentation` to `result` variable
56 66
             // end loop
57 67
 
58
-        return result;
68
+        return stringResult.toString();
59 69
     }
60 70
 
61 71