소스 검색

MyArrayList - Complete

MySet - Working
add, get, contains, remove
Nick Satinover 6 년 전
부모
커밋
d7fe92337f
5개의 변경된 파일110개의 추가작업 그리고 7개의 파일을 삭제
  1. 1
    1
      .idea/compiler.xml
  2. 1
    1
      generics.iml
  3. 2
    2
      pom.xml
  4. 34
    3
      src/main/java/MySet.java
  5. 72
    0
      src/test/java/MySetTest.java

+ 1
- 1
.idea/compiler.xml 파일 보기

@@ -10,7 +10,7 @@
10 10
       </profile>
11 11
     </annotationProcessing>
12 12
     <bytecodeTargetLevel>
13
-      <module name="generics" target="1.7" />
13
+      <module name="generics" target="1.8" />
14 14
     </bytecodeTargetLevel>
15 15
   </component>
16 16
 </project>

+ 1
- 1
generics.iml 파일 보기

@@ -1,6 +1,6 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2 2
 <module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3
-  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7">
3
+  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
4 4
     <output url="file://$MODULE_DIR$/target/classes" />
5 5
     <output-test url="file://$MODULE_DIR$/target/test-classes" />
6 6
     <content url="file://$MODULE_DIR$">

+ 2
- 2
pom.xml 파일 보기

@@ -13,8 +13,8 @@
13 13
                 <groupId>org.apache.maven.plugins</groupId>
14 14
                 <artifactId>maven-compiler-plugin</artifactId>
15 15
                 <configuration>
16
-                    <source>1.7</source>
17
-                    <target>1.7</target>
16
+                    <source>1.8</source>
17
+                    <target>1.8</target>
18 18
                 </configuration>
19 19
             </plugin>
20 20
         </plugins>

+ 34
- 3
src/main/java/MySet.java 파일 보기

@@ -21,7 +21,13 @@ public class MySet<T> extends MyArrayList implements Set {
21 21
 
22 22
     @Override
23 23
     public int size() {
24
-        return 0;
24
+        int countSize = 0;
25
+
26
+        for (T elem: this.genericSet) {
27
+            countSize++;
28
+        }
29
+
30
+        return countSize;
25 31
     }
26 32
 
27 33
     @Override
@@ -49,7 +55,9 @@ public class MySet<T> extends MyArrayList implements Set {
49 55
 
50 56
     @Override
51 57
     public Object[] toArray() {
52
-        return new Object[0];
58
+        Object[] retArr = genericSet;
59
+
60
+        return retArr;
53 61
     }
54 62
 
55 63
     @Override
@@ -69,7 +77,30 @@ public class MySet<T> extends MyArrayList implements Set {
69 77
 
70 78
     @Override
71 79
     public boolean remove(Object o) {
72
-        return false;
80
+
81
+        if (this.contains(o)){
82
+
83
+            int objectCount = 0;
84
+            for (T element: genericSet) {
85
+                if (element.equals(o)){
86
+                    objectCount++;
87
+                }
88
+            }
89
+            T[] newGenericSet = (T[]) Array.newInstance(this.genericSet.getClass().getComponentType(),
90
+                    genericSet.length - objectCount);
91
+            int i = 0;
92
+            for (T element: genericSet) {
93
+                if (!element.equals(o)){
94
+                    newGenericSet[i] = element;
95
+                    i++;
96
+                }
97
+            }
98
+            genericSet = newGenericSet;
99
+            return true;
100
+        }
101
+        else {
102
+            return false;
103
+        }
73 104
     }
74 105
 
75 106
     @Override

+ 72
- 0
src/test/java/MySetTest.java 파일 보기

@@ -50,4 +50,76 @@ public class MySetTest {
50 50
         // Then
51 51
         Assert.assertEquals(expected, actual);
52 52
     }
53
+
54
+    @Test
55
+    public void sizeLongTest() {
56
+        // Given
57
+        int expected = 4;
58
+        Long[] inputArray = {1L, 2L, 8L, 4L};
59
+        MySet<Long> mySet = new MySet<Long>(inputArray);
60
+
61
+        // When
62
+        int actual = mySet.size();
63
+
64
+        // Then
65
+        Assert.assertEquals(expected, actual);
66
+    }
67
+
68
+    @Test
69
+    public void toArrayStringTest() {
70
+        // Given
71
+        Object[] expected = {"1", "2", "8", "4"};
72
+        String[] inputArray = {"1", "2", "8", "4"};
73
+        MySet<String> mySet = new MySet<String>(inputArray);
74
+
75
+        // When
76
+        Object[] actual = mySet.toArray();
77
+
78
+        // Then
79
+        Assert.assertEquals(expected, actual);
80
+    }
81
+
82
+    @Test
83
+    public void removeStringTest() {
84
+        // Given
85
+        String[] expected = {"2", "8", "4"};
86
+        String[] inputArray = {"1", "2", "8", "4"};
87
+        MySet<String> mySet = new MySet<String>(inputArray);
88
+        Object object = "1";
89
+        // When
90
+        mySet.remove(object);
91
+        String[] actual = mySet.get();
92
+
93
+        // Then
94
+        Assert.assertEquals(expected, actual);
95
+    }
96
+
97
+    @Test
98
+    public void removeBooleanReturnStringTest() {
99
+        // Given
100
+        boolean expected = true;
101
+        String[] inputArray = {"1", "2", "8", "4"};
102
+        MySet<String> mySet = new MySet<String>(inputArray);
103
+        Object object = "1";
104
+        // When
105
+        boolean actual = mySet.remove(object);
106
+
107
+        // Then
108
+        Assert.assertEquals(expected, actual);
109
+    }
110
+
111
+    @Test
112
+    public void removeMultipleStringTest() {
113
+        // Given
114
+        String[] expected = {"4"};
115
+        String[] inputArray = {"1", "1", "1", "4"};
116
+        MySet<String> mySet = new MySet<String>(inputArray);
117
+        Object object = "1";
118
+        // When
119
+        mySet.remove(object);
120
+        String[] actual = mySet.get();
121
+
122
+        // Then
123
+        Assert.assertEquals(expected, actual);
124
+    }
53 125
 }