Integer Set
/**
* Class IntegerSet
*
*/
public class IntegerSet
{
public final static int START_SIZE = 4;
private int [] numbers;
private int item_count = 0;
public IntegerSet() {
numbers = new int[START_SIZE];
}
/**
Copy constructor
*/
public IntegerSet(IntegerSet old) {
this.numbers = new int[old.count()];
for (int i = 0; i < old.count(); i++) {
this.add(old.numbers[i]);
}
}
public IntegerSet union(IntegerSet other) {
IntegerSet temp = new IntegerSet();
for (int i = 0; i < this.count(); i++) {
temp.add(this.numbers[i]);
}
for (int i = 0; i < other.count(); i++) {
temp.add(other.numbers[i]);
}
return temp;
}
public IntegerSet intersection(IntegerSet other) {
IntegerSet temp = new IntegerSet();
for (int i = 0; i < other.count(); i++) {
if (exists(other.numbers[i])) {
temp.add(other.numbers[i]);
}
}
return temp;
}
public void add(int item) {
if (!exists(item)) {
if (numbers.length == item_count) {
doubleArraySize();
}
numbers[item_count] = item;
item_count++;
}
}
/**
Remove item from set (if found of course)
First create a copy of the existing set.
Next look for the element in the set.
Remove the item from the copy and return the copy.
If the element is not found, a duplicate is returned.
*/
public IntegerSet remove(int item) {
IntegerSet temp = new IntegerSet(this);
int i = temp.count() - 1;
boolean found = false;
while (!found && i >= 0) {
if (temp.numbers[i] == item) {
found = true; // i is the index of the found item
}
else {
i--;
}
}
if (found) {
// copy all other elements 'up' one in the array
for (int j = i; j < temp.count() - 1; j++) {
temp.numbers[j] = temp.numbers[j + 1];
}
temp.numbers = reduceArraySizeByOne(temp.numbers);
temp.item_count--;
}
return temp;
}
private int [] reduceArraySizeByOne(int [] nums) {
int [] temp = new int[nums.length - 1];
for (int i = 0; i < temp.length; i++) {
temp[i] = nums[i];
}
return temp;
}
private boolean exists(int item) {
// looking from the end back I don't have to check against max, just 0
int i = this.count() - 1;
boolean found = false;
while (!found && i >= 0) {
if (numbers[i] == item) {
found = true;
}
else {
i--;
}
}
return found;
}
private void doubleArraySize() {
int [] temp = new int[numbers.length * 2];
for(int i = 0; i < numbers.length; i++) {
temp[i] = numbers[i];
}
numbers = temp;
}
public int count() {
return item_count;
}
public String toString() {
StringBuffer temp = new StringBuffer();
for (int i = 0; i < this.count()-1; i++) {
temp.append(this.numbers[i] + ", ");
}
temp.append(this.numbers[this.count() - 1]);
return "IntegerSet: " + "\n" +
temp.toString();
}
//======================================================
public static void main(String [] args) {
IntegerSet x = new IntegerSet();
IntegerSet y = new IntegerSet();
IntegerSet z;
//System.out.println( x );
x.add(3);
x.add(1);
x.add(16);
x.add(3);
x.add(38);
x.add(-4);
x.add(-56);
y.add(4);
y.add(1);
y.add(15);
y.add(3);
y.add(-4);
System.out.println("x = " + x );
z = x.remove(38);
System.out.println("z = " + z );
System.out.println("y = " + y );
z = x.union(y);
System.out.println("z = " + z );
z = x.intersection(y);
System.out.println("z = " + z );
}
}