Pārlūkot izejas kodu

duplicateDeleter

Jamez-s 6 gadus atpakaļ
vecāks
revīzija
96c7c3288e
4 mainītis faili ar 82 papildinājumiem un 92 dzēšanām
  1. 35
    41
      IntegerDuplicateDeleter.java
  2. 0
    0
      README.TXT
  3. 35
    39
      StringDuplicateDeleter.java
  4. 12
    12
      package.bluej

+ 35
- 41
IntegerDuplicateDeleter.java Parādīt failu

@@ -2,57 +2,51 @@ import java.util.*;
2 2
 import java.util.List;
3 3
 import java.util.Collection;
4 4
 
5
-
6
-public class IntegerDuplicateDeleter extends DuplicateDeleter{
7
-    
5
+public class IntegerDuplicateDeleter extends DuplicateDeleter<Integer>{
8 6
     public IntegerDuplicateDeleter(Integer[] array) {
9 7
         super(array);
10 8
     }
11
-    
9
+
12 10
     public Integer[] removeDuplicates(int maxNumberOfDuplications) {
13
-       List<Integer> list = new ArrayList<>();
14
-        
15
-        // go thru each element in array
16
-       for (Object num : super.array) {
17
-            
18
-            // count occurence
19
-            int occurence = countOccurrence((Integer)num);
20
-        
21
-            //add to new array if occurrence != exactnumber of duplication
22
-            if (occurence < maxNumberOfDuplications) 
23
-                list.add((Integer)num);
24
-                
25
-       }
26
-        return list.toArray(new Integer[list.size()]);
11
+       Integer[] answer = new Integer[array.length];
12
+        int answerIndex = 0;
13
+        for (int i = 0; i < array.length; i++) {
14
+            Integer occurences = getAppearances(array[i]);
15
+            if(occurences < maxNumberOfDuplications) {
16
+                answer[answerIndex] = array[i];
17
+                answerIndex++;    
18
+
19
+            }
20
+        }
21
+        Integer[] realAnswer = Arrays.copyOf(answer, answerIndex);
22
+        return realAnswer;
27 23
     }
28
-    
29
-        
24
+
30 25
     public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) {
31
-        List<Integer> list = new ArrayList<>();
32
-        
33
-        // go thru each element in array
34
-        for (Object num : super.array) {
35
-            
36
-            // count occurence
37
-            int occurrence = countOccurrence((Integer)num);
38
-        
39
-            //add to new array if occurrence != exactnumber of duplication
40
-            if (occurrence != exactNumberOfDuplications) 
41
-                list.add((Integer)num);
42
-                
26
+        Integer[] answer = new Integer[array.length];
27
+        int answerIndex = 0;
28
+        for (int i = 0; i < array.length; i++) {
29
+            Integer occurences = getAppearances(array[i]);
30
+
31
+            if (occurences  < exactNumberOfDuplications || occurences > exactNumberOfDuplications){ 
32
+                answer[answerIndex] = array[i];
33
+                answerIndex++;
34
+            }
43 35
         }
44
-        return list.toArray(new Integer[list.size()]);
36
+        Integer[] realAnswer = Arrays.copyOf(answer, answerIndex);
37
+        return realAnswer;
38
+
45 39
     }
46 40
 
47
-    private int countOccurrence(Integer number) {
48
-        List<Integer> list = new ArrayList<>();
49
-        int count = 0;
50
-        
51
-        for (Object i : super.array) {
52
-            if (i.equals(number)) { // when == 1
53
-                count++;
41
+    private int getAppearances(Integer s) {
42
+
43
+        int appearances = 0;
44
+
45
+        for (Integer element: array) {
46
+            if (element == s) { // when == 1
47
+                appearances++;
54 48
             }
55 49
         }
56
-        return count;
50
+        return appearances;
57 51
     }
58 52
 }

+ 0
- 0
README.TXT Parādīt failu


+ 35
- 39
StringDuplicateDeleter.java Parādīt failu

@@ -4,54 +4,50 @@ public final class StringDuplicateDeleter extends DuplicateDeleter<String> {
4 4
     public StringDuplicateDeleter(String[] array) {
5 5
         super(array);
6 6
     }
7
-    
7
+
8 8
     public String[] removeDuplicates(int maxNumberOfDuplications) {
9
-       List<String> list = new ArrayList<>();
10
-        
11
-        // go thru each element in array
12
-       for (Object num : super.array) {
13
-            
14
-            // count occurence
15
-            int occurence = countOccurrence((String)num);
16
-        
17
-            //add to new array if occurrence != exactnumber of duplication
18
-            if (occurence < maxNumberOfDuplications) 
19
-                list.add((String)num);
20
-                
21
-       }
22
-        return list.toArray(new String[list.size()]);
9
+
10
+        String[] answer = new String[array.length];
11
+        int answerIndex = 0;
12
+        for (int i = 0; i < array.length; i++) {
13
+            Integer occurences = getAppearances(array[i]);
14
+            if(occurences < maxNumberOfDuplications) {
15
+                answer[answerIndex] = array[i];
16
+                answerIndex++;    
17
+
18
+            }
19
+        }
20
+        String[] realAnswer = Arrays.copyOf(answer, answerIndex);
21
+        return realAnswer;
22
+
23 23
     }
24
-    
25
-        
24
+
26 25
     public String[] removeDuplicatesExactly(int exactNumberOfDuplications) {
27
-        List<String> list = new ArrayList<>();
28
-        
29
-        // go thru each element in array
30
-        for (Object num : super.array) {
31
-            
32
-            // count occurence
33
-            int occurrence = countOccurrence((String)num);
34
-        
35
-            //add to new array if occurrence != exactnumber of duplication
36
-            if (occurrence != exactNumberOfDuplications) 
37
-                list.add((String)num);
38
-                
26
+        String[] answer = new String[array.length];
27
+        int answerIndex = 0;
28
+        for (int i = 0; i < array.length; i++) {
29
+            Integer occurences = getAppearances(array[i]);
30
+
31
+            if (occurences  < exactNumberOfDuplications || occurences > exactNumberOfDuplications){ 
32
+                answer[answerIndex] = array[i];
33
+                answerIndex++;
34
+            }
39 35
         }
40
-        return list.toArray(new String[list.size()]);
36
+        String[] realAnswer = Arrays.copyOf(answer, answerIndex);
37
+        return realAnswer;
38
+
41 39
     }
42 40
 
43
-    private int countOccurrence(String s) {
41
+    private int getAppearances(String s) {
44 42
 
45
-        int count = 0;
46
-        
47
-        for (Object i : super.array) {
48
-            if (i.equals(s)) { // when == 1
49
-                count++;
43
+        int appearances = 0;
44
+
45
+        for (String element: array) {
46
+            if (element == s) { // when == 1
47
+                appearances++;
50 48
             }
51 49
         }
52
-        return count;
50
+        return appearances;
53 51
     }
54 52
 }
55 53
 
56
-
57
-

+ 12
- 12
package.bluej Parādīt failu

@@ -1,28 +1,28 @@
1 1
 #BlueJ package file
2
-dependency1.from=StringDuplicateDeleterTest
2
+dependency1.from=IntegerDuplicateDeleterTest
3 3
 dependency1.to=DuplicateDeleter
4 4
 dependency1.type=UsesDependency
5
-dependency2.from=StringDuplicateDeleterTest
6
-dependency2.to=StringDuplicateDeleter
5
+dependency2.from=IntegerDuplicateDeleterTest
6
+dependency2.to=IntegerDuplicateDeleter
7 7
 dependency2.type=UsesDependency
8
-dependency3.from=IntegerDuplicateDeleterTest
8
+dependency3.from=StringDuplicateDeleterTest
9 9
 dependency3.to=DuplicateDeleter
10 10
 dependency3.type=UsesDependency
11
-dependency4.from=IntegerDuplicateDeleterTest
12
-dependency4.to=IntegerDuplicateDeleter
11
+dependency4.from=StringDuplicateDeleterTest
12
+dependency4.to=StringDuplicateDeleter
13 13
 dependency4.type=UsesDependency
14
-editor.fx.0.height=807
15
-editor.fx.0.width=1440
16
-editor.fx.0.x=0
17
-editor.fx.0.y=23
14
+editor.fx.0.height=614
15
+editor.fx.0.width=858
16
+editor.fx.0.x=405
17
+editor.fx.0.y=216
18 18
 objectbench.height=189
19 19
 objectbench.width=722
20 20
 package.divider.horizontal=0.6
21 21
 package.divider.vertical=0.6979969183359014
22 22
 package.editor.height=446
23 23
 package.editor.width=1109
24
-package.editor.x=263
25
-package.editor.y=218
24
+package.editor.x=205
25
+package.editor.y=126
26 26
 package.frame.height=707
27 27
 package.frame.width=1235
28 28
 package.numDependencies=4