From 25bb1e25ea00fb13856a241f9d5380bf5558c138 Mon Sep 17 00:00:00 2001
From: Teo Blaise Kaplanski <teobk@macbook-air-de-teo.home>
Date: Fri, 6 Nov 2020 11:14:35 +0100
Subject: [PATCH] =?UTF-8?q?1=20=C3=A0=207?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 tp5/.classpath           |   6 +
 tp5/.gitignore           |   3 +
 tp5/.project             |  17 +++
 tp5/MyArrayList.java     | 274 +++++++++++++++++++++++++++++++++++++++
 tp5/TestMyArrayList.java | 117 +++++++++++++++++
 5 files changed, 417 insertions(+)
 create mode 100644 tp5/.classpath
 create mode 100644 tp5/.gitignore
 create mode 100644 tp5/.project
 create mode 100755 tp5/MyArrayList.java
 create mode 100755 tp5/TestMyArrayList.java

diff --git a/tp5/.classpath b/tp5/.classpath
new file mode 100644
index 0000000..3f3893a
--- /dev/null
+++ b/tp5/.classpath
@@ -0,0 +1,6 @@
+<?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>
diff --git a/tp5/.gitignore b/tp5/.gitignore
new file mode 100644
index 0000000..ed38427
--- /dev/null
+++ b/tp5/.gitignore
@@ -0,0 +1,3 @@
+/MyArrayList.class
+/TestMyArrayList.class
+/MyArrayList$1.class
diff --git a/tp5/.project b/tp5/.project
new file mode 100644
index 0000000..1d65abc
--- /dev/null
+++ b/tp5/.project
@@ -0,0 +1,17 @@
+<?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>
diff --git a/tp5/MyArrayList.java b/tp5/MyArrayList.java
new file mode 100755
index 0000000..78dbfcd
--- /dev/null
+++ b/tp5/MyArrayList.java
@@ -0,0 +1,274 @@
+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;
+    }
+
+}
diff --git a/tp5/TestMyArrayList.java b/tp5/TestMyArrayList.java
new file mode 100755
index 0000000..869cdb4
--- /dev/null
+++ b/tp5/TestMyArrayList.java
@@ -0,0 +1,117 @@
+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);
+
+	}
+
+    
+
+}
-- 
GitLab