Bladeren bron

tried to clone

Trinh Tong 6 jaren geleden
bovenliggende
commit
d173fcf296
2 gewijzigde bestanden met toevoegingen van 27 en 1 verwijderingen
  1. 14
    1
      src/main/java/MyArrayList.java
  2. 13
    0
      src/test/java/MyArrayListTest.java

+ 14
- 1
src/main/java/MyArrayList.java Bestand weergeven

@@ -1,3 +1,4 @@
1
+import java.lang.reflect.Array;
1 2
 import java.util.ArrayList;
2 3
 import java.util.Arrays;
3 4
 
@@ -119,7 +120,19 @@ public class MyArrayList<T> {
119 120
             if (myArrayList[i].equals(data))
120 121
                 return true;
121 122
         }
122
-
123 123
         return false;
124 124
     }
125
+
126
+
127
+    public int indexOf(T findMe) {
128
+        int count = -1;
129
+        for (Object o: myArrayList) {
130
+            count++;
131
+            if (o.equals(findMe)) {
132
+                return count;
133
+            }
134
+        }
135
+        return count;
136
+    }
137
+
125 138
 }

+ 13
- 0
src/test/java/MyArrayListTest.java Bestand weergeven

@@ -103,4 +103,17 @@ public class MyArrayListTest {
103 103
         String expectedString = "they moved the table";
104 104
         Assert.assertFalse(stringList.contains(expectedString));
105 105
     }
106
+
107
+    @Test
108
+    public void testIndexOf() {
109
+        String findMe = "Nhu got more tears to fill up her tea";
110
+        stringList.add("Wilhelm spilled the tea");
111
+        stringList.add("the table got wet :(");
112
+        stringList.add("Nhu got more tears to fill up her tea");
113
+        stringList.add("Wilhelm spilled the tea");
114
+
115
+        int expectedIndex = 3;
116
+
117
+        Assert.assertEquals(expectedIndex, stringList.indexOf(findMe));
118
+    }
106 119
 }