|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+*Create a Set*
|
|
|
2
|
+
|
|
|
3
|
+This program is taking various given Arrays and removing duplicates to create a new array. The first example uses an array of string letters, the second an array of rectangles, and the third a combination of the two. The methods work the same for all types.
|
|
|
4
|
+
|
|
|
5
|
+The main method first establishes what is contained in each array and prints out this original list as one string. Then it prints out the list as a new string minus the duplicates by calling the makeSet method.
|
|
|
6
|
+
|
|
|
7
|
+The makeSet method takes the original array and first tests that it's value is not null or that none of the individual objects' values are null. If there are any null values it returns an error message. A new Object array is created to hold the results of the search, it is initially set to the same length as the array being tested but all of the values would be null to start. Then it declares a variable to count the number of unique objects (numUnique) and initializes it to zero. It declares a boolean found but does not declare a value. This boolean will trigger the while and if loops below that are used to go through the array to find duplicates. Next an int is declared to count the indexInResult, it is not initialized to zero until the for statement, this is so that each time the for loop runs the next object is being compared to all objects in the new result array.
|
|
|
8
|
+
|
|
|
9
|
+Within the for loop i is initialized to 0 which will be used as the index for each item in the array as it works through the list until i reaches the length of the list. There is a while loop where as long as found is false (which it is initialized to in the for loop to begin the test and each time the for loop loops back) and the index number is less than the number of unique objects it will compare the value in the i location of the original array to the indexInResult value of the result array.
|
|
|
10
|
+
|
|
|
11
|
+For example the first time through the for loop unique objects and indexInResult are both zero so the while loop won't run and will instead jump straight to the if statement which stores the first object in the array in the new result array and increases the count of unique objects to one. Now the while loop will be triggered every time because the indexInResult is reset to zero each time through and the unique objects variable will always be at least one. This allows the next object in the original array to be compared to all objects being stored in the result array. While doing this if the object is the same as any in the results array, it triggers the boolean found to turn to true which gets it out of the while loop and means the if statement below is not triggered and the second object will not be recorded because it's the same as the last object (a duplicate). If the object is not the same as any in the results array, found would remain false and the if statement would trigger adding the object to the result array. This continues until all of the objects in the original array have been put through the for loop. Finally a new array list is created to take the results and trim out the nulls by making the list only the length of the number of unique objects found.
|