瀏覽代碼

tried to clone

Trinh Tong 6 年之前
父節點
當前提交
d173fcf296
共有 2 個文件被更改,包括 27 次插入1 次删除
  1. 14
    1
      src/main/java/MyArrayList.java
  2. 13
    0
      src/test/java/MyArrayListTest.java

+ 14
- 1
src/main/java/MyArrayList.java 查看文件

1
+import java.lang.reflect.Array;
1
 import java.util.ArrayList;
2
 import java.util.ArrayList;
2
 import java.util.Arrays;
3
 import java.util.Arrays;
3
 
4
 
119
             if (myArrayList[i].equals(data))
120
             if (myArrayList[i].equals(data))
120
                 return true;
121
                 return true;
121
         }
122
         }
122
-
123
         return false;
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 查看文件

103
         String expectedString = "they moved the table";
103
         String expectedString = "they moved the table";
104
         Assert.assertFalse(stringList.contains(expectedString));
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
 }