Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ public static void main(String[] args) {

System.out.println("Hello world!");

List<String> friends = new RecursiveLinkedList<String>();

List<String> friends = new ArrayList<String>();
List<String> friends2 = new ArrayList<String>();


System.out.println("initial size is " + friends.size());

Expand All @@ -23,7 +26,13 @@ public static void main(String[] args) {
friends.add("Dorothy");
friends.add("Sophia");
friends.add(2, "Wednesday");
friends.add("Tina");
friends2.add("daw");
friends2.add("hi");
friends2.add("Tina");
friends2.add("Rose");
System.out.println("size is now " + friends.size());
System.out.println(friends.lastIndexOf("Tina"));

//for (int i = 0; i < friends.size(); i++) {
// System.out.println(friends.get(i));
Expand All @@ -36,9 +45,18 @@ public static void main(String[] args) {
String name = itr.next();
System.out.println(name);
}
friends.addAll(friends2);
System.out.println("size is now " +friends.size());
friends.removeAll(friends2);
System.out.println("size is now " +friends.size());
System.out.println(friends2.size());
System.out.println(friends2.contains("Tina"));
friends.retainAll(friends2);
System.out.println("size is now " +friends.size());


for (String name : friends) {
Iterator<String> itrs = friends2.iterator();
while (itrs.hasNext()) {
String name = itrs.next();
System.out.println(name);
}

Expand All @@ -56,7 +74,29 @@ public static void main(String[] args) {
*/


for (String name : friends) {
System.out.println(name);
}
System.out.println();
System.out.println();


List<String> linked1 = new SinglyLinkedList<>();
linked1.add("Tina1");
linked1.add("Josh1");
linked1.add("Susan1");
linked1.add("Tyler1");
linked1.add("Usman1");
linked1.add("Dee1");
linked1.add("Rose1");
linked1.add("Blanche1");
linked1.add("Dorothy1");
linked1.add(3,"wadwe");
int haha=linked1.indexOf("wadwe");
System.out.println(haha);
Iterator<String> itrss = linked1.iterator();
while (itrss.hasNext()) {
String name = itrss.next();
System.out.println(name);
}
}
}
63 changes: 50 additions & 13 deletions src/edu/greenriver/sdev333/ArrayList.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ArrayList<ItemType> implements List<ItemType> {
// size is DIFFERENT than length
private int size;


// construction
public ArrayList() {
size = 0;
data = (ItemType[]) new Object[10];
Expand Down Expand Up @@ -179,7 +179,15 @@ public boolean containsAll(Collection<? extends ItemType> otherCollection) {
*/
@Override
public void addAll(Collection<? extends ItemType> otherCollection) {
throw new UnsupportedOperationException("Not gonna do it!");
Iterator<ItemType> itr = (Iterator<ItemType>)otherCollection.iterator();
while (itr.hasNext()) {
checkSize();

//ItemType itemToCheck = itr.next();

add(itr.next());
}

}

/**
Expand All @@ -192,7 +200,14 @@ public void addAll(Collection<? extends ItemType> otherCollection) {
*/
@Override
public void removeAll(Collection<? extends ItemType> otherCollection) {
Iterator<ItemType> itr = (Iterator<ItemType>)otherCollection.iterator();
while (itr.hasNext()) {


//ItemType itemToCheck = itr.next();

remove(itr.next());
}
}

/**
Expand All @@ -205,7 +220,16 @@ public void removeAll(Collection<? extends ItemType> otherCollection) {
*/
@Override
public void retainAll(Collection<? extends ItemType> otherCollection) {

Iterator<ItemType> itr = (Iterator<ItemType>)otherCollection.iterator();
Collection<ItemType> collection = new ArrayList<ItemType>();
while (itr.hasNext()) {
ItemType itemToCheck = itr.next();
if (contains(itemToCheck)) {
collection.add(itemToCheck);
}
}
clear();
addAll(collection);
}

/**
Expand All @@ -218,7 +242,7 @@ public void retainAll(Collection<? extends ItemType> otherCollection) {
*/
@Override
public ItemType get(int index) {
if (index >= size) {
if (index >= size||index<0) {
throw new IndexOutOfBoundsException("index is beyond size");
}

Expand All @@ -238,7 +262,7 @@ public ItemType get(int index) {
*/
@Override
public void set(int index, ItemType item) {
if (index >= size) {
if (index >= size||index<0) {
throw new IndexOutOfBoundsException("index is beyond size");
}

Expand Down Expand Up @@ -279,6 +303,9 @@ public void add(int index, ItemType item) {
*/
@Override
public void remove(int index) {
if (index >= size||index<0) {
throw new IndexOutOfBoundsException("index is beyond size");
}
// shift values left to overwrite the item at index
for (int i = index; i < size - 1; i++) {
data[i] = data[i + 1];
Expand Down Expand Up @@ -321,6 +348,11 @@ public int indexOf(ItemType item) {
*/
@Override
public int lastIndexOf(ItemType item) {
for (int i = size; i > 0; i--) {
if (item.equals(data[i])) {
return i;
}
}
return 0;
}

Expand All @@ -333,7 +365,7 @@ public int lastIndexOf(ItemType item) {
*/
@Override
public ListIterator<ItemType> listIterator() {
return null;
return new SecondCustomIterator();
}


Expand Down Expand Up @@ -361,41 +393,46 @@ public ItemType next() {
private class SecondCustomIterator implements ListIterator<ItemType> {
// fancier Iterator - lets us go forwards and backwards
private int currentPosition;

private int lastPosition;
public SecondCustomIterator() {
currentPosition = 0;
lastPosition=-1;
}

@Override
public boolean hasNext() {
return false;
return currentPosition < size();
}

@Override
public ItemType next() {
return null;
ItemType result = get(currentPosition);
currentPosition++;
return result;
}

@Override
public boolean hasPrevious() {
// hasNext checked currentPosition with size
// hasPrevious check currentPosition against 0
return false;
return currentPosition > 0;
}

@Override
public ItemType previous() {
return null;
ItemType result = get(currentPosition);
currentPosition--;
return result;
}

@Override
public int nextIndex() {
return 0;
return currentPosition+1;
}

@Override
public int previousIndex() {
return 0;
return currentPosition -1;
}

@Override
Expand Down
Loading