ArrayList completedmess is approaching

master
Chloe Fontenot 🏳️‍⚧️ 2023-09-29 13:33:32 +07:00
parent b372b1d559
commit 1e9d90d12a
4 changed files with 162 additions and 80 deletions

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<netbeans.hint.jdkPlatform>JDK_20</netbeans.hint.jdkPlatform>
</properties>
</project-shared-configuration>

@ -7,8 +7,8 @@
<packaging>jar</packaging> <packaging>jar</packaging>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source> <maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target> <maven.compiler.target>20</maven.compiler.target>
<exec.mainClass>com.calebfontenot.mp4_generics_calebfontenot.MP4_Generics_CalebFontenot</exec.mainClass> <exec.mainClass>com.calebfontenot.mp4_generics_calebfontenot.MP4_Generics_CalebFontenot</exec.mainClass>
</properties> </properties>
</project> </project>

@ -29,7 +29,8 @@ public class ArrayListASDV<E>
* Constructs an empty list with an initial capacity of three. * Constructs an empty list with an initial capacity of three.
* *
*/ */
public ArrayListASDV() { public ArrayListASDV()
{
list = (E[]) new Object[3]; list = (E[]) new Object[3];
index = 0; index = 0;
} }
@ -40,7 +41,8 @@ public class ArrayListASDV<E>
* @param initialCapacity - the initial capacity of the list * @param initialCapacity - the initial capacity of the list
* @throws IllegalArgumentException - if the specified initial capacity is negative * @throws IllegalArgumentException - if the specified initial capacity is negative
*/ */
public ArrayListASDV(int initialCapacity) { public ArrayListASDV(int initialCapacity)
{
if (initialCapacity < 0) { if (initialCapacity < 0) {
throw new IllegalArgumentException("initialCapacity id negative: " + initialCapacity); throw new IllegalArgumentException("initialCapacity id negative: " + initialCapacity);
} }
@ -57,7 +59,8 @@ public class ArrayListASDV<E>
* *
* *
*/ */
public ArrayListASDV(Collection<? extends E> c) { public ArrayListASDV(Collection<? extends E> c)
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
} }
@ -72,7 +75,8 @@ public class ArrayListASDV<E>
* @throws IllegalArgumentException - if some property of the element prevents it from being added to this collection * @throws IllegalArgumentException - if some property of the element prevents it from being added to this collection
*/ */
@Override @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. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
@ -84,12 +88,14 @@ public class ArrayListASDV<E>
* @return the number of elements in this list. * @return the number of elements in this list.
*/ */
@Override @Override
public int size() { public int size()
{
return index; return index;
} }
@Override @Override
public String toString() { public String toString()
{
String s = "ArrayListASDV["; String s = "ArrayListASDV[";
for (int i = 0; i < index; ++i) { for (int i = 0; i < index; ++i) {
@ -107,7 +113,8 @@ public class ArrayListASDV<E>
* *
*/ */
@Override @Override
public boolean isEmpty() { public boolean isEmpty()
{
return this.index == 0; return this.index == 0;
} }
@ -120,7 +127,8 @@ public class ArrayListASDV<E>
* *
*/ */
@Override @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. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
@ -132,7 +140,8 @@ public class ArrayListASDV<E>
* @return an array containing all of the elements in this list in proper sequence * @return an array containing all of the elements in this list in proper sequence
*/ */
@Override @Override
public Object[] toArray() { public Object[] toArray()
{
Object[] returnArray = new Object[index]; Object[] returnArray = new Object[index];
for (int i = 0; i < index; ++i) { for (int i = 0; i < index; ++i) {
Object objCopy = list[i]; Object objCopy = list[i];
@ -149,7 +158,8 @@ public class ArrayListASDV<E>
*/ */
@Override @Override
public boolean remove(Object o public boolean remove(Object o
) { )
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
@ -159,7 +169,8 @@ public class ArrayListASDV<E>
* 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. * 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 @Override
public void clear() { public void clear()
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
} }
@ -173,7 +184,8 @@ public class ArrayListASDV<E>
*/ */
@Override @Override
public E get(int index public E get(int index
) { )
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
} }
@ -188,7 +200,8 @@ public class ArrayListASDV<E>
*/ */
@Override @Override
public E set(int index, E element public E set(int index, E element
) { )
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
} }
@ -202,7 +215,8 @@ public class ArrayListASDV<E>
* @throws IndexOutOfBoundsException - if the index is out of range (index GE 0 || index GE size()) * @throws IndexOutOfBoundsException - if the index is out of range (index GE 0 || index GE size())
*/ */
@Override @Override
public void add(int index, E element) { public void add(int index, E element)
{
if (element == null) { if (element == null) {
throw new NullPointerException("cant add null"); throw new NullPointerException("cant add null");
} }
@ -231,7 +245,8 @@ public class ArrayListASDV<E>
* @throws IndexOutOfBoundsException - if the index is out of range (index GE 0 || index GE size()) * @throws IndexOutOfBoundsException - if the index is out of range (index GE 0 || index GE size())
*/ */
@Override @Override
public E remove(int index) { public E remove(int index)
{
E r = this.list[index]; E r = this.list[index];
this.list[index] = null; this.list[index] = null;
if (index == 0) { if (index == 0) {
@ -256,7 +271,8 @@ public class ArrayListASDV<E>
*/ */
@Override @Override
public int indexOf(Object o public int indexOf(Object o
) { )
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
} }
@ -268,7 +284,8 @@ public class ArrayListASDV<E>
* @return the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element * @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 @Override
public int lastIndexOf(Object o) { public int lastIndexOf(Object o)
{
ListIterator<E> iterator = listIterator(size()); ListIterator<E> iterator = listIterator(size());
while (iterator.hasPrevious()) { while (iterator.hasPrevious()) {
int indexOf = iterator.previousIndex(); int indexOf = iterator.previousIndex();
@ -293,7 +310,8 @@ public class ArrayListASDV<E>
*/ */
@Override @Override
public List<E> subList(int fromIndex, int toIndex public List<E> subList(int fromIndex, int toIndex
) { )
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
} }
@ -307,7 +325,8 @@ public class ArrayListASDV<E>
* @throws NullPointerException - if the specified array is null * @throws NullPointerException - if the specified array is null
*/ */
@Override @Override
public <T> T[] toArray(T[] a) { public <T> T[] toArray(T[] a)
{
Class<?> clazz = a.getClass(); Class<?> clazz = a.getClass();
//>length of a is too small //>length of a is too small
if (a.length < index) // Make a new array of a's runtime type if (a.length < index) // Make a new array of a's runtime type
@ -331,9 +350,10 @@ public class ArrayListASDV<E>
} }
@Override @Override
public Iterator<E> iterator() { public Iterator<E> iterator()
return new IteratorASDV<>(this); {
return new IteratorASDV<E>().iterator();
//return new Iterator < E >(this);
} }
/** /**
@ -343,17 +363,19 @@ public class ArrayListASDV<E>
* @return a list iterator over the elements in this list (in proper sequence * @return a list iterator over the elements in this list (in proper sequence
*/ */
@Override @Override
public ListIterator<E> listIterator() { public ListIterator<E> listIterator()
{
return listIterator(0); return listIterator(0);
} }
@Override @Override
public ListIterator<E> listIterator(int index) { public ListIterator<E> listIterator(int index)
{
ListIterator<E> it = new ListIterator<E>() { ListIterator<E> it = new ListIterator<E>() {
E[] list = (E[]) new Object[3];
int current = 0; 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.) * 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<E>
* @return true if the list iterator has more elements when traversing the list in the forward direction * @return true if the list iterator has more elements when traversing the list in the forward direction
*/ */
@Override @Override
public boolean hasNext() { public boolean hasNext()
return next() != null; {
if (this.index == this.list.length) {
}
} }
/** /**
@ -372,21 +397,24 @@ public class ArrayListASDV<E>
* @throws NoSuchElementException - if the iteration has no next element * @throws NoSuchElementException - if the iteration has no next element
*/ */
@Override @Override
public E next() throws NoSuchElementException { public E next() throws NoSuchElementException
if (current < index) { {
if (index > this.list.length) {
throw new NoSuchElementException(); throw new NoSuchElementException();
} }
return list[current++]; return this.list[index++];
} }
@Override @Override
public boolean hasPrevious() { public boolean hasPrevious()
return previous() != null; {
return index > 0;
} }
@Override @Override
public E previous() { public E previous()
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. {
return this.list[--index];
} }
@ -396,7 +424,8 @@ public class ArrayListASDV<E>
* @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 * @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 @Override
public int nextIndex() { public int nextIndex()
{
return index + 1 == size() ? size() : index + 1; return index + 1 == size() ? size() : index + 1;
} }
@ -406,7 +435,8 @@ public class ArrayListASDV<E>
* @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 * @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 @Override
public int previousIndex() { public int previousIndex()
{
return index - 1; return index - 1;
} }
@ -417,7 +447,8 @@ public class ArrayListASDV<E>
* @throws NullPointerException - if the specified action is null * @throws NullPointerException - if the specified action is null
*/ */
@Override @Override
public void forEachRemaining(Consumer<? super E> action) { public void forEachRemaining(Consumer<? super E> action)
{
while (hasNext()) { while (hasNext()) {
if (action == null) { if (action == null) {
throw new NullPointerException("Action is NULL! YOU ARE A FOOL!"); throw new NullPointerException("Action is NULL! YOU ARE A FOOL!");
@ -428,17 +459,20 @@ public class ArrayListASDV<E>
} }
@Override @Override
public void remove() { public void remove()
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
} }
@Override @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. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
} }
@Override @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. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
} }
}; };
@ -458,20 +492,23 @@ public class ArrayListASDV<E>
* @throws ClassCastException - if the types of one or more elements in the specified collection are incompatible with this collection * @throws ClassCastException - if the types of one or more elements in the specified collection are incompatible with this collection
*/ */
@Override @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. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
} }
@Override @Override
public boolean addAll(Collection<? extends E> c) { public boolean addAll(Collection<? extends E> c)
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
} }
@Override @Override
public boolean addAll(int index, Collection<? extends E> c public boolean addAll(int index, Collection<? extends E> c
) { )
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
} }
@ -486,7 +523,8 @@ public class ArrayListASDV<E>
* @throws ClassCastException - if the types of one or more elements in this collection are incompatible with the specified collection * @throws ClassCastException - if the types of one or more elements in this collection are incompatible with the specified collection
*/ */
@Override @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. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
} }
@ -501,13 +539,15 @@ public class ArrayListASDV<E>
*/ */
@Override @Override
public boolean retainAll(Collection<?> c public boolean retainAll(Collection<?> c
) { )
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
} }
public static void main(String[] args) public static void main(String[] args)
throws ClassNotFoundException, InterruptedException { throws ClassNotFoundException, InterruptedException
{
ArrayList<Integer> aaa = new ArrayList(); ArrayList<Integer> aaa = new ArrayList();
ArrayListASDV<Integer> list1 = new ArrayListASDV(); ArrayListASDV<Integer> list1 = new ArrayListASDV();
@ -714,7 +754,8 @@ public class ArrayListASDV<E>
System.out.println(li.next()); System.out.println(li.next());
li.forEachRemaining(new Consumer<Integer>() { li.forEachRemaining(new Consumer<Integer>() {
@Override @Override
public void accept(Integer t) { public void accept(Integer t)
{
System.out.print(t + 1 + " "); System.out.print(t + 1 + " ");
} }
}); });
@ -793,17 +834,20 @@ class A1 implements Consumer<A1> {
int x; int x;
public A1(int x) { public A1(int x)
{
this.x = x; this.x = x;
} }
@Override @Override
public String toString() { public String toString()
{
return "A1{" + "x=" + x + '}'; return "A1{" + "x=" + x + '}';
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj)
{
if (this == obj) { if (this == obj) {
return true; return true;
} }
@ -821,7 +865,8 @@ class A1 implements Consumer<A1> {
} }
@Override @Override
public void accept(A1 t) { public void accept(A1 t)
{
System.out.println(t.x * t.x); System.out.println(t.x * t.x);
} }

@ -10,33 +10,52 @@ import java.util.function.Consumer;
/** /**
* *
* @author caleb * @author caleb
*/ */
public class IteratorASDV<T> implements Iterator<T> { public class IteratorASDV<E> {
ArrayListASDV<?> arrayList; E[] list = (E[]) new Object[3];
int i;
public IteratorASDV(ArrayListASDV<?> arrayList) { public void add (E e) {
this.arrayList = arrayList; list[i++] = e;
} }
public IteratorASDV<E> iterator() implements Iterator<T>{
/** Checks to see if the array has an element in it. Iterator<E> it = new Iterator<E>()
* {
* @return true if our ArrayList has items in it int index = 0;
*/
@Override @Override
public boolean hasNext() { public boolean hasNext()
return arrayList.get(arrayList.size() - 1) != null; {
if (index == IteratorASDV.this.list.length) {
return false;
}
return true;
}
@Override
public E next()
{
return IteratorASDV.this.list[index++];
}
@Override
public void forEachRemaining(Consumer<? super E> action)
{
Iterator.super.forEachRemaining(action); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/OverriddenMethodBody
}
};
return (IteratorASDV<E>) it;
}
public static void main(String[] args)
{
IteratorASDV<Integer> ti = new IteratorASDV<Integer>();
ti.add(1);
ti.add(2);
ti.add(3);
Iterator<Integer> it = (Iterator<Integer>) 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
}
}