patternjavaMinor
A simple ArrayList class in Java
Viewed 0 times
arraylistsimpleclassjava
Problem
I have created a simple
I am testing this for mistakes, but I am learning Java and my tests are maybe not comprehensive.
```
public class SJUArrayList {
// Data fields
private E[] theData;
private int size = 0;
private int capacity = 0;
// Constants
private static final int INIT_CAPACITY = 10;
// Constructors
public SJUArrayList() {
this(INIT_CAPACITY);
}
public SJUArrayList(int initCapacity) {
capacity = initCapacity;
theData = (E[]) new Object[capacity];
}
// Methods
public boolean add(E e) {
if(e == null) {
throw new NullPointerException();
}
if(size == capacity) {
reallocate();
}
theData[size] = e;
size++;
return true;
} // End add(E e) method
public void add(int index, E e) {
if(index size) {
throw new ArrayIndexOutOfBoundsException(index);
}
if(e == null) {
throw new NullPointerException();
}
if(size == capacity) {
reallocate();
}
for(int i = size; i > index; i--) {
theData[i] = theData[i - 1];
}
theData[index] = e;
size++;
} // End add(int index, E e) method
public void clear() {
theData = (E[]) new Object[capacity];
size = 0;
} // End clear() method
public boolean equals(Object o) {
if(o == null) {
return false;
}
if(getClass() != o.getClass()) {
return false;
}
SJUArrayList otherO = (SJUArrayList) o;
if(size != otherO.size) {
return false;
}
for(int i = 0; i = size) {
throw new ArrayIndexOutOfBoundsException(index);
}
return theData[index];
} // End get(int index) method
public int indexOf(Object o) {
if(o == n
ArrayList with some methods from the actual ArrayList in Java.I am testing this for mistakes, but I am learning Java and my tests are maybe not comprehensive.
```
public class SJUArrayList {
// Data fields
private E[] theData;
private int size = 0;
private int capacity = 0;
// Constants
private static final int INIT_CAPACITY = 10;
// Constructors
public SJUArrayList() {
this(INIT_CAPACITY);
}
public SJUArrayList(int initCapacity) {
capacity = initCapacity;
theData = (E[]) new Object[capacity];
}
// Methods
public boolean add(E e) {
if(e == null) {
throw new NullPointerException();
}
if(size == capacity) {
reallocate();
}
theData[size] = e;
size++;
return true;
} // End add(E e) method
public void add(int index, E e) {
if(index size) {
throw new ArrayIndexOutOfBoundsException(index);
}
if(e == null) {
throw new NullPointerException();
}
if(size == capacity) {
reallocate();
}
for(int i = size; i > index; i--) {
theData[i] = theData[i - 1];
}
theData[index] = e;
size++;
} // End add(int index, E e) method
public void clear() {
theData = (E[]) new Object[capacity];
size = 0;
} // End clear() method
public boolean equals(Object o) {
if(o == null) {
return false;
}
if(getClass() != o.getClass()) {
return false;
}
SJUArrayList otherO = (SJUArrayList) o;
if(size != otherO.size) {
return false;
}
for(int i = 0; i = size) {
throw new ArrayIndexOutOfBoundsException(index);
}
return theData[index];
} // End get(int index) method
public int indexOf(Object o) {
if(o == n
Solution
-
Consider the following test case:
It's a memory leak since the
-
You should implement a hashCode method too:
If two objects are equal according to the
method on each of the two objects must produce the same integer result.
The following test should pass:
-
Consider the following test case:
final SJUArrayList list = new SJUArrayList(4);
list.add("a");
list.add("b");
list.add("c"); // theData contains the following: [a, b, c, null]
list.remove("c"); // theData contains the following: [a, b, c, null]It's a memory leak since the
theData array has a reference to a removed object, therefore the garbage collector could not deallocate the memory of the unused, inaccessible c string object. It could be serious if you have lots of lists or use this list with bigger objects.-
You should implement a hashCode method too:
If two objects are equal according to the
equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
The following test should pass:
final SJUArrayList listOne = new SJUArrayList(4);
listOne.add("a");
final SJUArrayList listTwo = new SJUArrayList(4);
listTwo.add("a");
assertTrue("equals", listOne.equals(listTwo));
assertEquals("hashCode", listOne.hashCode(), listTwo.hashCode()); // should pass-
-1 is used multiple times. It should be a named constant.Code Snippets
final SJUArrayList<String> list = new SJUArrayList<String>(4);
list.add("a");
list.add("b");
list.add("c"); // theData contains the following: [a, b, c, null]
list.remove("c"); // theData contains the following: [a, b, c, null]final SJUArrayList<String> listOne = new SJUArrayList<String>(4);
listOne.add("a");
final SJUArrayList<String> listTwo = new SJUArrayList<String>(4);
listTwo.add("a");
assertTrue("equals", listOne.equals(listTwo));
assertEquals("hashCode", listOne.hashCode(), listTwo.hashCode()); // should passContext
StackExchange Code Review Q#16371, answer score: 8
Revisions (0)
No revisions yet.