From 1e9d90d12a21c110de6295a3c43d58bff2ace3ae Mon Sep 17 00:00:00 2001 From: Caleb Fontenot Date: Fri, 29 Sep 2023 13:33:32 -0500 Subject: [PATCH] ArrayList completedmess is approaching --- .../nb-configuration.xml | 18 +++ Semester 3/MP4_Generics_CalebFontenot/pom.xml | 4 +- .../ArrayListASDV.java | 149 ++++++++++++------ .../IteratorASDV.java | 71 ++++++--- 4 files changed, 162 insertions(+), 80 deletions(-) create mode 100644 Semester 3/MP4_Generics_CalebFontenot/nb-configuration.xml diff --git a/Semester 3/MP4_Generics_CalebFontenot/nb-configuration.xml b/Semester 3/MP4_Generics_CalebFontenot/nb-configuration.xml new file mode 100644 index 0000000..9c74157 --- /dev/null +++ b/Semester 3/MP4_Generics_CalebFontenot/nb-configuration.xml @@ -0,0 +1,18 @@ + + + + + + JDK_20 + + diff --git a/Semester 3/MP4_Generics_CalebFontenot/pom.xml b/Semester 3/MP4_Generics_CalebFontenot/pom.xml index fb9efb8..a121d6e 100644 --- a/Semester 3/MP4_Generics_CalebFontenot/pom.xml +++ b/Semester 3/MP4_Generics_CalebFontenot/pom.xml @@ -7,8 +7,8 @@ jar UTF-8 - 17 - 17 + 20 + 20 com.calebfontenot.mp4_generics_calebfontenot.MP4_Generics_CalebFontenot \ No newline at end of file diff --git a/Semester 3/MP4_Generics_CalebFontenot/src/main/java/com/calebfontenot/mp4_generics_calebfontenot/ArrayListASDV.java b/Semester 3/MP4_Generics_CalebFontenot/src/main/java/com/calebfontenot/mp4_generics_calebfontenot/ArrayListASDV.java index 62b1ccf..fcaa8bd 100644 --- a/Semester 3/MP4_Generics_CalebFontenot/src/main/java/com/calebfontenot/mp4_generics_calebfontenot/ArrayListASDV.java +++ b/Semester 3/MP4_Generics_CalebFontenot/src/main/java/com/calebfontenot/mp4_generics_calebfontenot/ArrayListASDV.java @@ -29,7 +29,8 @@ public class ArrayListASDV * Constructs an empty list with an initial capacity of three. * */ - public ArrayListASDV() { + public ArrayListASDV() + { list = (E[]) new Object[3]; index = 0; } @@ -40,7 +41,8 @@ public class ArrayListASDV * @param initialCapacity - the initial capacity of the list * @throws IllegalArgumentException - if the specified initial capacity is negative */ - public ArrayListASDV(int initialCapacity) { + public ArrayListASDV(int initialCapacity) + { if (initialCapacity < 0) { throw new IllegalArgumentException("initialCapacity id negative: " + initialCapacity); } @@ -57,7 +59,8 @@ public class ArrayListASDV * * */ - public ArrayListASDV(Collection c) { + public ArrayListASDV(Collection c) + { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @@ -72,7 +75,8 @@ public class ArrayListASDV * @throws IllegalArgumentException - if some property of the element prevents it from being added to this collection */ @Override - public boolean add(E e) { + public boolean add(E e) + { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. @@ -84,12 +88,14 @@ public class ArrayListASDV * @return the number of elements in this list. */ @Override - public int size() { + public int size() + { return index; } @Override - public String toString() { + public String toString() + { String s = "ArrayListASDV["; for (int i = 0; i < index; ++i) { @@ -107,7 +113,8 @@ public class ArrayListASDV * */ @Override - public boolean isEmpty() { + public boolean isEmpty() + { return this.index == 0; } @@ -120,7 +127,8 @@ public class ArrayListASDV * */ @Override - public boolean contains(Object o) { + public boolean contains(Object o) + { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. @@ -132,7 +140,8 @@ public class ArrayListASDV * @return an array containing all of the elements in this list in proper sequence */ @Override - public Object[] toArray() { + public Object[] toArray() + { Object[] returnArray = new Object[index]; for (int i = 0; i < index; ++i) { Object objCopy = list[i]; @@ -149,7 +158,8 @@ public class ArrayListASDV */ @Override public boolean remove(Object o - ) { + ) + { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. @@ -159,7 +169,8 @@ public class ArrayListASDV * Removes all of the elements from this list. The list will be empty after this call returns. Note: Traverse the array and set all of its elements to null. Set its index to zero. */ @Override - public void clear() { + public void clear() + { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @@ -173,7 +184,8 @@ public class ArrayListASDV */ @Override public E get(int index - ) { + ) + { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @@ -188,7 +200,8 @@ public class ArrayListASDV */ @Override public E set(int index, E element - ) { + ) + { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @@ -202,7 +215,8 @@ public class ArrayListASDV * @throws IndexOutOfBoundsException - if the index is out of range (index GE 0 || index GE size()) */ @Override - public void add(int index, E element) { + public void add(int index, E element) + { if (element == null) { throw new NullPointerException("cant add null"); } @@ -231,7 +245,8 @@ public class ArrayListASDV * @throws IndexOutOfBoundsException - if the index is out of range (index GE 0 || index GE size()) */ @Override - public E remove(int index) { + public E remove(int index) + { E r = this.list[index]; this.list[index] = null; if (index == 0) { @@ -256,7 +271,8 @@ public class ArrayListASDV */ @Override public int indexOf(Object o - ) { + ) + { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @@ -268,7 +284,8 @@ public class ArrayListASDV * @return the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element */ @Override - public int lastIndexOf(Object o) { + public int lastIndexOf(Object o) + { ListIterator iterator = listIterator(size()); while (iterator.hasPrevious()) { int indexOf = iterator.previousIndex(); @@ -293,7 +310,8 @@ public class ArrayListASDV */ @Override public List subList(int fromIndex, int toIndex - ) { + ) + { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @@ -307,7 +325,8 @@ public class ArrayListASDV * @throws NullPointerException - if the specified array is null */ @Override - public T[] toArray(T[] a) { + public T[] toArray(T[] a) + { Class clazz = a.getClass(); //>length of a is too small if (a.length < index) // Make a new array of a's runtime type @@ -331,9 +350,10 @@ public class ArrayListASDV } @Override - public Iterator iterator() { - return new IteratorASDV<>(this); - + public Iterator iterator() + { + return new IteratorASDV().iterator(); + //return new Iterator < E >(this); } /** @@ -343,17 +363,19 @@ public class ArrayListASDV * @return a list iterator over the elements in this list (in proper sequence */ @Override - public ListIterator listIterator() { + public ListIterator listIterator() + { return listIterator(0); } @Override - public ListIterator listIterator(int index) { + public ListIterator listIterator(int index) + { ListIterator it = new ListIterator() { - - int current = 0; + E[] list = (E[]) new Object[3]; + int index = 0; /** * Returns true if this list iterator has more elements when traversing the list in the forward direction. (In other words, returns true if ListIterator.next would return an element rather than throwing an exception.) @@ -361,8 +383,11 @@ public class ArrayListASDV * @return true if the list iterator has more elements when traversing the list in the forward direction */ @Override - public boolean hasNext() { - return next() != null; + public boolean hasNext() + { + if (this.index == this.list.length) { + + } } /** @@ -372,21 +397,24 @@ public class ArrayListASDV * @throws NoSuchElementException - if the iteration has no next element */ @Override - public E next() throws NoSuchElementException { - if (current < index) { + public E next() throws NoSuchElementException + { + if (index > this.list.length) { throw new NoSuchElementException(); } - return list[current++]; + return this.list[index++]; } @Override - public boolean hasPrevious() { - return previous() != null; + public boolean hasPrevious() + { + return index > 0; } @Override - public E previous() { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + public E previous() + { + return this.list[--index]; } @@ -396,7 +424,8 @@ public class ArrayListASDV * @return the index of the element that would be returned by a subsequent call to next, or list size if the list iterator is at the end of the list */ @Override - public int nextIndex() { + public int nextIndex() + { return index + 1 == size() ? size() : index + 1; } @@ -406,7 +435,8 @@ public class ArrayListASDV * @return the index of the element that would be returned by a subsequent call to previous, or -1 if the list iterator is at the beginning of the list */ @Override - public int previousIndex() { + public int previousIndex() + { return index - 1; } @@ -417,7 +447,8 @@ public class ArrayListASDV * @throws NullPointerException - if the specified action is null */ @Override - public void forEachRemaining(Consumer action) { + public void forEachRemaining(Consumer action) + { while (hasNext()) { if (action == null) { throw new NullPointerException("Action is NULL! YOU ARE A FOOL!"); @@ -428,17 +459,20 @@ public class ArrayListASDV } @Override - public void remove() { + public void remove() + { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override - public void set(E e) { + public void set(E e) + { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override - public void add(E e) { + public void add(E e) + { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }; @@ -458,20 +492,23 @@ public class ArrayListASDV * @throws ClassCastException - if the types of one or more elements in the specified collection are incompatible with this collection */ @Override - public boolean containsAll(Collection c) { + public boolean containsAll(Collection c) + { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override - public boolean addAll(Collection c) { + public boolean addAll(Collection c) + { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public boolean addAll(int index, Collection c - ) { + ) + { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @@ -486,7 +523,8 @@ public class ArrayListASDV * @throws ClassCastException - if the types of one or more elements in this collection are incompatible with the specified collection */ @Override - public boolean removeAll(Collection c) { + public boolean removeAll(Collection c) + { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @@ -501,13 +539,15 @@ public class ArrayListASDV */ @Override public boolean retainAll(Collection c - ) { + ) + { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } public static void main(String[] args) - throws ClassNotFoundException, InterruptedException { + throws ClassNotFoundException, InterruptedException + { ArrayList aaa = new ArrayList(); ArrayListASDV list1 = new ArrayListASDV(); @@ -714,7 +754,8 @@ public class ArrayListASDV System.out.println(li.next()); li.forEachRemaining(new Consumer() { @Override - public void accept(Integer t) { + public void accept(Integer t) + { System.out.print(t + 1 + " "); } }); @@ -793,17 +834,20 @@ class A1 implements Consumer { int x; - public A1(int x) { + public A1(int x) + { this.x = x; } @Override - public String toString() { + public String toString() + { return "A1{" + "x=" + x + '}'; } @Override - public boolean equals(Object obj) { + public boolean equals(Object obj) + { if (this == obj) { return true; } @@ -821,7 +865,8 @@ class A1 implements Consumer { } @Override - public void accept(A1 t) { + public void accept(A1 t) + { System.out.println(t.x * t.x); } diff --git a/Semester 3/MP4_Generics_CalebFontenot/src/main/java/com/calebfontenot/mp4_generics_calebfontenot/IteratorASDV.java b/Semester 3/MP4_Generics_CalebFontenot/src/main/java/com/calebfontenot/mp4_generics_calebfontenot/IteratorASDV.java index a61c8e0..db94c85 100644 --- a/Semester 3/MP4_Generics_CalebFontenot/src/main/java/com/calebfontenot/mp4_generics_calebfontenot/IteratorASDV.java +++ b/Semester 3/MP4_Generics_CalebFontenot/src/main/java/com/calebfontenot/mp4_generics_calebfontenot/IteratorASDV.java @@ -10,33 +10,52 @@ import java.util.function.Consumer; /** * * @author caleb - */ -public class IteratorASDV implements Iterator { - ArrayListASDV arrayList; - - public IteratorASDV(ArrayListASDV arrayList) { - this.arrayList = arrayList; + */ +public class IteratorASDV { + E[] list = (E[]) new Object[3]; + int i; + public void add (E e) { + list[i++] = e; } - - /** Checks to see if the array has an element in it. - * - * @return true if our ArrayList has items in it - */ - @Override - public boolean hasNext() { - return arrayList.get(arrayList.size() - 1) != null; + public IteratorASDV iterator() implements Iterator{ + Iterator it = new Iterator() + { + int index = 0; + + @Override + public boolean hasNext() + { + if (index == IteratorASDV.this.list.length) { + return false; + } + return true; + } + + @Override + public E next() + { + return IteratorASDV.this.list[index++]; + } + + @Override + public void forEachRemaining(Consumer action) + { + Iterator.super.forEachRemaining(action); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/OverriddenMethodBody + } + + }; + return (IteratorASDV) it; + } + public static void main(String[] args) + { + IteratorASDV ti = new IteratorASDV(); + ti.add(1); + ti.add(2); + ti.add(3); + Iterator it = (Iterator) ti.iterator(); + while (it.hasNext()) { + System.out.println(it.next()); + } } - @Override - public T next() { - arrayList.remove(0); - return (T) arrayList.get(0); - } - - @Override - public void remove() { - Iterator.super.remove(); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/OverriddenMethodBody - } - -}