modifying ListOfThree class
This commit is contained in:
parent
5a1a468c78
commit
b43787bca5
@ -1,31 +1,31 @@
|
||||
package com.baeldung.accessmodifiers.publicmodifier;
|
||||
|
||||
import java.util.AbstractList;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class ListOfThree<E> extends AbstractList<E> {
|
||||
|
||||
private static final int MAX_LENGTH = 3;
|
||||
private int size;
|
||||
private Object[] elements = new Object[MAX_LENGTH];
|
||||
private static final int LENGTH = 3;
|
||||
private Object[] elements;
|
||||
|
||||
public ListOfThree(E[] data) {
|
||||
if(data == null
|
||||
|| data.length != LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
this.elements = Arrays.copyOf(data, data.length); //shallow copy
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public E get(int index) {
|
||||
public E get(int index) {
|
||||
return (E)elements[index];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(E e) {
|
||||
|
||||
elements[size] = e;
|
||||
size++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return size;
|
||||
return LENGTH;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -48,31 +48,21 @@ public class PublicAccessModifierUnitTest {
|
||||
@Test
|
||||
public void whenCreatingCustomList_concreteAndInheritedMethodsWork() {
|
||||
|
||||
List<String> list1 = new ListOfThree<String>();
|
||||
list1.add("zero"); //inherited implementation
|
||||
list1.add("one");
|
||||
list1.add("two");
|
||||
String[] dataSet1 = new String[] {"zero", "one", "two"};
|
||||
|
||||
List<String> list1 = new ListOfThree<String>(dataSet1);
|
||||
|
||||
//our implemented methods
|
||||
assertEquals("zero", list1.get(0));
|
||||
|
||||
assertThrows(ArrayIndexOutOfBoundsException.class, () -> {
|
||||
list1.get(4);
|
||||
});
|
||||
|
||||
assertThrows(ArrayIndexOutOfBoundsException.class, () -> {
|
||||
list1.add("three");
|
||||
});
|
||||
|
||||
assertEquals("one", list1.get(1));
|
||||
assertEquals(3, list1.size());
|
||||
|
||||
list1.indexOf("one"); //inherited implementation
|
||||
//inherited implementations
|
||||
assertEquals(1, list1.indexOf("one"));
|
||||
|
||||
List<String> list2 = new ListOfThree<String>();
|
||||
list2.add("zero");
|
||||
list2.add("one");
|
||||
String[] dataSet2 = new String[] {"two", "zero", "one"};
|
||||
List<String> list2 = new ListOfThree<String>(dataSet2);
|
||||
|
||||
assertTrue(list1.containsAll(list2)); //inherited implementation
|
||||
assertTrue(list1.containsAll(list2));
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user