Skip to content
Snippets Groups Projects
Commit 25bb1e25 authored by Teo Blaise Kaplanski's avatar Teo Blaise Kaplanski
Browse files

1 à 7

parent 0b51acff
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path=""/>
<classpathentry kind="output" path=""/>
</classpath>
/MyArrayList.class
/TestMyArrayList.class
/MyArrayList$1.class
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>tp5</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
import java.util.Objects;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Arrays;
import java.util.ListIterator;
public class MyArrayList<T> implements List<T> {
private int capacity;
private int size;
private transient T[] elementData;
public MyArrayList(int initialCapacity) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = (T[])new Object[initialCapacity];
}
public MyArrayList() {
this(10);
}
@Override
public boolean add(T element) {
ensureCapacity(1);
elementData[this.size] = element;
this.size += 1;
return true;
}
@Override
public int size() {
return this.size;
}
@Override
public boolean isEmpty() {
return this.size == 0;
}
@Override
public T get(int index) {
Objects.checkIndex(index, this.size);
return elementData[index];
}
@Override
public Iterator<T> iterator() {
return listIterator(); }
@Override
public T remove(int index) {
T lou = get(index);
for (int i = index + 1; i < size; i++)
elementData[i-1] = elementData[i];
size--;
return lou;
}
@Override
public boolean contains(Object o) {
for (T el : elementData)
if (o.equals(el)) {
return true;
}
return false;
}
public String toString() {
StringBuffer sb = new StringBuffer(0);
for (T s : elementData) {
if (s != null)
{
sb.append(s);
sb.append(" ");
}
}
String str = sb.toString();
return str;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof List<?>)) {
return false;
}
List<?> other = (List<?>) obj;
// TODO: check whether this and other are equal.
return this.equals(other);
}
@Override
public Object[] toArray() {
return null;
}
@Override
public boolean remove(Object o) {
remove(indexOf(o));
return true;
}
@Override
public boolean addAll(Collection collection) {
return false;
}
@Override
public boolean addAll(int i, Collection collection) {
return false;
}
@Override
public void clear() {
}
@SuppressWarnings("unchecked")
@Override
public Object set(int i, Object o) {
Objects.checkIndex(i, size());
T lib = get(i);
elementData[i] = (T)o;
return lib;
}
@SuppressWarnings("unchecked")
@Override
public void add(int i, Object o) {
ensureCapacity(1);
add((T)o);
for (int index = size() - 1; index > i; i++){
T lib = get(index);
set(index, get(index-1));
set(index-1, lib);
}
}
@Override
public int indexOf(Object o) {
for (int i = 0; i < size(); i++)
if (o.equals(get(i))){
return i;
}
return size;
}
@Override
public int lastIndexOf(Object o) {
for (int i = size() - 1; i >= 0; i--)
if (o.equals(get(i))){
return i;
}
return -1;
}
@Override
public ListIterator listIterator() {
return listIterator(0);
}
@Override
public ListIterator<T> listIterator(int i) {
MyArrayList<T> myArrayList = this;
return new ListIterator<T>() {
int ind = i - 1;
@Override
public boolean hasNext() {
return ind + 1 < size();
}
@Override
public T next() {
return get(++ind);
}
@Override
public boolean hasPrevious() {
return ind > 0;
}
@Override
public T previous() {
return get(--ind);
}
@Override
public int nextIndex() {
return ind + 1;
}
@Override
public int previousIndex() {
return ind - 1;
}
@Override
public void remove() {
myArrayList.remove(ind--);
}
public void set(T obj) {
myArrayList.set(ind, obj);
}
public void add(T obj) {
myArrayList.add(ind, obj);
}
};
}
@Override
public List subList(int i, int i1) {
return null;
}
@Override
public boolean retainAll(Collection collection) {
return false;
}
@Override
public boolean removeAll(Collection collection) {
return false;
}
@Override
public boolean containsAll(Collection collection) {
return false;
}
@Override
public Object[] toArray(Object[] objects) {
return new Object[0];
}
public void ensureCapacity(int Cap){
if (size + Cap <= capacity) return;
int newCapacity = 1;
while (newCapacity < size + Cap) {
newCapacity *= 2;
}
if (newCapacity == capacity) return;
T[] newelementData = (T[])new Object[newCapacity];
for (int i = 0; i < size(); ++i){
newelementData[i] = get(i);
}
elementData = newelementData;
capacity = newCapacity;
}
}
import java.util.ArrayList;
public class TestMyArrayList {
public static void main(String[] args){
TestMyArrayList test = new TestMyArrayList();
System.out.print("testIsEmpty1 : ");
if(test.testIsEmpty1() == true)
System.out.println("correct");
else
System.out.println("incorrect");
System.out.print("testIsEmpty2 : ");
if(test.testIsEmpty2() == true)
System.out.println("correct");
else
System.out.println("incorrect");
//System.out.print("testAdd1 : ");
//if(test.testAdd1() == true)
// System.out.println("correct");
//else
// System.out.println("incorrect");
System.out.print("testget : ");
if(test.testGet() == true)
System.out.println("correct");
else
System.out.println("incorrect");
System.out.print("testtoString : ");
if(test.testtoString() == true)
System.out.println("correct");
else
System.out.println("incorrect");
System.out.print("testContains : ");
if(test.testContains()) System.out.println("correct");
else System.out.println("incorrect");
System.out.print("testRemove1 : ");
if(test.testRemove()) System.out.println("correct");
else System.out.println("incorrect");
}
public boolean testIsEmpty1(){
MyArrayList<Integer> myArrayList = new MyArrayList<Integer>();
if(myArrayList.isEmpty() == true)
return true;
else
return false;
}
public boolean testIsEmpty2(){
MyArrayList<Integer> myArrayList = new MyArrayList<Integer>();
myArrayList.add(1);
if(myArrayList.isEmpty() == false)
return true;
else
return false;
}
@SuppressWarnings("unlikely-arg-type")
public boolean testAdd1(){
MyArrayList<Integer> myArrayList = new MyArrayList<Integer>();
myArrayList.add(1);
ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(1);
if(arrayList.equals(myArrayList))
return true;
else
return false;
}
public boolean testGet() {
MyArrayList<Integer> myArrayList = new MyArrayList<Integer>();
myArrayList.add(1);
if (myArrayList.get(0) == 1)
return true;
else
return false;
}
public boolean testtoString() {
MyArrayList<Integer> myArrayList = new MyArrayList<Integer>();
myArrayList.add(123);
myArrayList.add(1);
System.out.println(myArrayList.toString());
return true;
}
public boolean testContains(){
MyArrayList<Integer> myArrayList = new MyArrayList<>();
myArrayList.add(1);
myArrayList.add(2);
myArrayList.add(0);
myArrayList.add(9);
return myArrayList.contains(1) && myArrayList.contains(2) && myArrayList.contains(0);
}
public boolean testRemove(){
MyArrayList<Integer> myArrayList = new MyArrayList<>();
myArrayList.add(6);
myArrayList.add(7);
myArrayList.remove((Integer)6);
return myArrayList.size() == 1 && myArrayList.get(0).equals(7);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment