my add and remove program
I am asked to create an add method with the following rules:
When adding new elements, check to ensure there is room in the array data. If there is no unused array index, grow the array by delta elements. When removing existing elements, shrink the array by delta elements whenever there is room to add more than delta elements. In other words, leave no more than delta empty array locations at any time. The array must have from 0 through delta empty indexes at all times. The following assertions must pass.
This add method must work for any data type. And so far the method that I have create is:
publicboolean add(E element){
boolean result =false;
Object[] temp =new Object[data.length+delta];
if (data.length == n){
for (int i = 0; i<data.length; i++){
temp[i] = data[i];
}
data = temp;
}
for (int i=0; i><data.length; i++){
if (data[i].equals(element))
result =false;
else{
n++;
data[n-1] = element;
result =true;
}
}
return result;
}
The problem with this code is that when I try to add an interger it always gives me a null point exception because all the value in the array are set as null the very first time, how do I get this method to work the way it's suppose to be?>
Perhaps this is stupid question but have you considered using one of the classes that Java already provides such as ArrayList for example?
I am writing this method inside the AraySet class that implements the Set interface. So how to solve this?
When is the NullPointerException being thrown - which line in your method causes it to be thrown?Secondly, when you say 'add an integer', you do mean add an Integer and not add an int I hope because I am guessing that the class should only hold objects not primitives.
I am using a junit test to test the add method which is below:
@Test
public void testGrowAndShrink() {
Set<Integer> ints = new ArraySet<Integer>(5, 10);
for (int i = 1; i <= 20; i++)
ints.add(i);
assertEquals(20, ints.size());
// Array has now grown twice by 10 elements (capacity went from 5 to 25)
assertEquals(25, ints.capacity());
for (int i = 1; i <= 4; i++)
ints.remove(i);
assertEquals(16, ints.size());
assertEquals(25, ints.capacity());
ints.remove(5);
// Array shrunk by 10 elements
assertEquals(15, ints.size());
assertEquals(15, ints.capacity());
}
by the way do you have any instant messenger so I can explain this problem better?
Message was edited by:
aditya15417
No, sorry, I do not mean the test you are using, which line of code within the add() method actually causes the exception to be thrown when it is executed?
Also, it looks from your test code as if you are trying at add a primitive. Can you try changing this line;
ints.add(i);
to
ints.add(new Integer(i));
and seeing what the result is?
I can't change the test method because it is given by my instructor to be like that. All I can change is the method add that I create here. The null point exception was on the test method not on the add method. This is confusing..
It gives me a null point exception on this line:
if (data[i].equals(element))
I believe it's because the first time the number i is compared with null.
Message was edited by:
aditya15417
Oh, now I am confused because you are declaring ArraySet to hold Integer(s )- that is instances of the Integer class - and then trying to add primitive int values to it in the test code. That is certainly something to ask about bearing in mind that you are trying to call the equals() method on a primitive here when adding int(s).
Also, I have taken the time to look a little more closely at the code and you are using an operator I have never seen before in this for clause;
for (int i=0; i><data.length; i++)
I do not think that >< is a legal operator.
Aside from that, there is one thing you could do to help overcome what I am guessing could be the source of the problem and that is to add another check for null values, something like;
if(data[i] == null) {
// Do whatever with a null value.
}
else if (data[i].equals(element)) {
result = false;
}
else {
n++;
data[n-1] = element;
result = true;
}
I think you can clean that up further by saying;
if(data[i] != null && data[i].equals(element)) {
result = false;
}
else {
n++;
data[n-1] = element;
result = true;
}
but you in the end data.equals(element) will then generate a null point exception.....
Hey, Tillerman, look up "autoboxing". It works in Java 5, and that's the context of the question anyway.
okay guys, so here's now for my remove method:
public boolean remove(E element) {
boolean result = true;
int position = 0;
int count = 0;
for (int i = 0; i <= n; i++) {
if (element.equals(data[i])) {
result = true;
position = i;
}
}
if (result == true) {
for (int j = position; j < n - 1; j++) {
data[position] = data[position + 1];
}
data[n - 1] = null;
n--;
}
for (int k = 0; k< data.length; k++) {
if (data[k] == null)
count++;
}
if (count>=delta){
Object[] temp = new Object[data.length - (count-delta)];
for (int m = 0; m<temp.length; m++)
temp[m] = data[m];
data = temp;
}
return result;
}
This method wont work according to the rules:
When removing existing elements, shrink the array by delta elements whenever there is room to add more than delta elements. In other words, leave no more than delta empty array locations at any time. The array must have from 0 through delta empty indexes at all times. The following assertions must pass.
why is that? can someone help me?
My teacher has provided me with this test method:
@Test
public void testGrowAndShrink() {
Set><Integer> ints = new ArraySet<Integer>(5, 10);
for (int i = 1; i <= 20; i++)
ints.add(i);
assertEquals(20, ints.size());
// Array has now grown twice by 10 elements (capacity went from 5 to 25)
assertEquals(25, ints.capacity());
for (int i = 1; i <= 4; i++)
ints.remove(i);
assertEquals(16, ints.size());
assertEquals(25, ints.capacity());
ints.remove(5);
// Array shrunk by 10 elements
assertEquals(15, ints.size());
assertEquals(15, ints.capacity());
}
and it didn't pass the assertEquals(15, ints.capacity());
why?
Message was edited by:
aditya15417
Message was edited by:
aditya15417
