Skip to main content
added 102 characters in body
Source Link
bob9123
  • 755
  • 2
  • 11
  • 31
 The zoo now holds 6 animals: Cheetah Jaguar Leopard Lion Panther Tiger 

 Testing the iterator
 >> Cheetah Jaguar Leopard Lion Panther  //Not posting everything inWorks array.fine

 Testing the iterator again without resetting
 >>  // Not sure why thisThis is still blank?

Testing the iterator again after resetting
>> Cheetah Jaguar Leopard Lion Panther 

Testing for-each loop
>> Cheetah Jaguar Leopard Lion Panther // Works fine.

Letting all the animals escape
The zoo now holds 0 animals: //Is there a way to remove by changing the MyArraylist class instead of changing the added class?

Testing the iterator with an empty list
>> Tiger  //Still inaccurate.
 The zoo now holds 6 animals: Cheetah Jaguar Leopard Lion Panther Tiger 

 Testing the iterator
 >> Cheetah Jaguar Leopard Lion Panther //Not posting everything in array.

 Testing the iterator again without resetting
 >>  // Not sure why this is blank?

Testing the iterator again after resetting
>> Cheetah Jaguar Leopard Lion Panther 

Testing for-each loop
>> Cheetah Jaguar Leopard Lion Panther 

Letting all the animals escape
The zoo now holds 0 animals: 

Testing the iterator with an empty list
>> Tiger 
 The zoo now holds 6 animals: Cheetah Jaguar Leopard Lion Panther Tiger 

 Testing the iterator
 >> Cheetah Jaguar Leopard Lion Panther  //Works fine

 Testing the iterator again without resetting
 >>  // This is still blank

Testing the iterator again after resetting
>> Cheetah Jaguar Leopard Lion Panther 

Testing for-each loop
>> Cheetah Jaguar Leopard Lion Panther // Works fine.

Letting all the animals escape
The zoo now holds 0 animals: //Is there a way to remove by changing the MyArraylist class instead of changing the added class?

Testing the iterator with an empty list
>> Tiger  //Still inaccurate.
added 31 characters in body
Source Link
bob9123
  • 755
  • 2
  • 11
  • 31
 The zoo now holds 6 animals: Cheetah Jaguar Leopard Lion Panther Tiger 

 Testing the iterator
 >> Cheetah Jaguar Leopard Lion Panther //Not posting everything in array.

 Testing the iterator again without resetting
 >>  // Not sure why this is blank?

Testing the iterator again after resetting
>> Cheetah Jaguar Leopard Lion Panther 

Testing for-each loop
>> Cheetah Jaguar Leopard Lion Panther 

Letting all the animals escape
The zoo now holds 0 animals: 

Testing the iterator with an empty list
>> Tiger 
 The zoo now holds 6 animals: Cheetah Jaguar Leopard Lion Panther Tiger 

 Testing the iterator
 >> Cheetah Jaguar Leopard Lion Panther //Not posting everything in array.

 Testing the iterator again without resetting
 >> 

Testing the iterator again after resetting
>> Cheetah Jaguar Leopard Lion Panther 

Testing for-each loop
>> Cheetah Jaguar Leopard Lion Panther 

Letting all the animals escape
The zoo now holds 0 animals: 

Testing the iterator with an empty list
>> Tiger 
 The zoo now holds 6 animals: Cheetah Jaguar Leopard Lion Panther Tiger 

 Testing the iterator
 >> Cheetah Jaguar Leopard Lion Panther //Not posting everything in array.

 Testing the iterator again without resetting
 >>  // Not sure why this is blank?

Testing the iterator again after resetting
>> Cheetah Jaguar Leopard Lion Panther 

Testing for-each loop
>> Cheetah Jaguar Leopard Lion Panther 

Letting all the animals escape
The zoo now holds 0 animals: 

Testing the iterator with an empty list
>> Tiger 
added 284 characters in body
Source Link
bob9123
  • 755
  • 2
  • 11
  • 31
 public class adding{

static MyArrayList zoo = new MyArrayList() {
    @Override
    public Iterator<Object> iterator() {
        return null;
    }
};


public static void printZoo() {
    System.out.print("The zoo now holds " + zoo.size() + " animals: ");
    for (int j = 0; j < zoo.size(); j++) System.out.print(zoo.get(j) + " ");
    System.out.println();
}
public static void main(String[] args) {

    String[] zooList = {"Cheetah", "Jaguar", "Leopard", "Lion", "Panther", "Tiger"};

    for (String x: zooList) zoo.add(x);
    printZoo();

    System.out.printf("\nTesting the iterator\n>> ");
    Iterator it = zoo.iterator();
    while (it.hasNext()) {
        System.out.print(it.next() + " ");
    }
    System.out.println();

    System.out.printf("\nTesting the iterator again without resetting\n>> ");
    while (it.hasNext()) {
        System.out.print(it.next() + " ");
    }
    System.out.println();

    System.out.printf("\nTesting the iterator again after resetting\n>> ");
    it = zoo.iterator();
    while (it.hasNext()) {
        System.out.print(it.next() + " ");
    }
    System.out.println();

    System.out.printf("\nTesting for-each loop\n>> ");
    for(Object animal: zoo) System.out.print(animal + " ");
    System.out.println();

    System.out.println("\nLetting all the animals escape");
    while (zoo.size()>0) zoo.remove(0);
    printZoo();

    System.out.printf("\nTesting the iterator with an empty list\n>> ");
    it = zoo.iterator();
    while (it.hasNext()) {
        System.out.print(it.next() + " ");
    }
    System.out.println();

    System.out.println("\nTest complete");


}
 }

Right now when I run this it come out with error:OUTPUT

 The zoo now holds 6 animals: Cheetah Jaguar Leopard Lion Panther Tiger 
Exception 
 inTesting threadthe "main"iterator
 >> Cheetah Jaguar Leopard Lion Panther //Not posting everything in array.

 Testing the iterator again without resetting
 >> java.lang.NullPointerException
at
Testing adding.main(adding.java:28)the iterator again after resetting
at>> sun.reflect.NativeMethodAccessorImpl.invoke0(NativeCheetah Method)Jaguar Leopard Lion Panther 
at
Testing for-each loop
>> Cheetah Jaguar Leopard Lion Panther 

Letting all the sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)animals escape
atThe zoo now sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.javaholds 0 animals:43) 
at
Testing java.lang.reflect.Method.invoke(Method.java:497)the iterator with an empty list
at>> com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)Tiger 

NotPretty sure how to go onthe logic of my iterator from here, appreciate all help as I'm a new Java programmerthe MyArrayList class is not accurate.

 public class adding{

static MyArrayList zoo = new MyArrayList() {
    @Override
    public Iterator<Object> iterator() {
        return null;
    }
};


public static void printZoo() {
    System.out.print("The zoo now holds " + zoo.size() + " animals: ");
    for (int j = 0; j < zoo.size(); j++) System.out.print(zoo.get(j) + " ");
    System.out.println();
}
public static void main(String[] args) {

    String[] zooList = {"Cheetah", "Jaguar", "Leopard", "Lion", "Panther", "Tiger"};

    for (String x: zooList) zoo.add(x);
    printZoo();

    System.out.printf("\nTesting the iterator\n>> ");
    Iterator it = zoo.iterator();
    while (it.hasNext()) {
        System.out.print(it.next() + " ");
    }
    System.out.println();

    System.out.printf("\nTesting the iterator again without resetting\n>> ");
    while (it.hasNext()) {
        System.out.print(it.next() + " ");
    }
    System.out.println();

    System.out.printf("\nTesting the iterator again after resetting\n>> ");
    it = zoo.iterator();
    while (it.hasNext()) {
        System.out.print(it.next() + " ");
    }
    System.out.println();

    System.out.printf("\nTesting for-each loop\n>> ");
    for(Object animal: zoo) System.out.print(animal + " ");
    System.out.println();

    System.out.println("\nLetting all the animals escape");
    while (zoo.size()>0) zoo.remove(0);
    printZoo();

    System.out.printf("\nTesting the iterator with an empty list\n>> ");
    it = zoo.iterator();
    while (it.hasNext()) {
        System.out.print(it.next() + " ");
    }
    System.out.println();

    System.out.println("\nTest complete");


}
 }

Right now when I run this it come out with error:

The zoo now holds 6 animals: Cheetah Jaguar Leopard Lion Panther Tiger 
Exception in thread "main" 
Testing the iterator
>> java.lang.NullPointerException
at adding.main(adding.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at        sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

Not sure how to go on from here, appreciate all help as I'm a new Java programmer.

 public class adding{

static MyArrayList zoo = new MyArrayList() {


public static void printZoo() {
    System.out.print("The zoo now holds " + zoo.size() + " animals: ");
    for (int j = 0; j < zoo.size(); j++) System.out.print(zoo.get(j) + " ");
    System.out.println();
}
public static void main(String[] args) {

    String[] zooList = {"Cheetah", "Jaguar", "Leopard", "Lion", "Panther", "Tiger"};

    for (String x: zooList) zoo.add(x);
    printZoo();

    System.out.printf("\nTesting the iterator\n>> ");
    Iterator it = zoo.iterator();
    while (it.hasNext()) {
        System.out.print(it.next() + " ");
    }
    System.out.println();

    System.out.printf("\nTesting the iterator again without resetting\n>> ");
    while (it.hasNext()) {
        System.out.print(it.next() + " ");
    }
    System.out.println();

    System.out.printf("\nTesting the iterator again after resetting\n>> ");
    it = zoo.iterator();
    while (it.hasNext()) {
        System.out.print(it.next() + " ");
    }
    System.out.println();

    System.out.printf("\nTesting for-each loop\n>> ");
    for(Object animal: zoo) System.out.print(animal + " ");
    System.out.println();

    System.out.println("\nLetting all the animals escape");
    while (zoo.size()>0) zoo.remove(0);
    printZoo();

    System.out.printf("\nTesting the iterator with an empty list\n>> ");
    it = zoo.iterator();
    while (it.hasNext()) {
        System.out.print(it.next() + " ");
    }
    System.out.println();

    System.out.println("\nTest complete");


}
 }

OUTPUT

 The zoo now holds 6 animals: Cheetah Jaguar Leopard Lion Panther Tiger 
 
 Testing the iterator
 >> Cheetah Jaguar Leopard Lion Panther //Not posting everything in array.

 Testing the iterator again without resetting
 >> 

Testing the iterator again after resetting
>> Cheetah Jaguar Leopard Lion Panther 

Testing for-each loop
>> Cheetah Jaguar Leopard Lion Panther 

Letting all the animals escape
The zoo now holds 0 animals: 

Testing the iterator with an empty list
>> Tiger 

Pretty sure the logic of my iterator from the MyArrayList class is not accurate.

added 284 characters in body
Source Link
bob9123
  • 755
  • 2
  • 11
  • 31
Loading
Source Link
bob9123
  • 755
  • 2
  • 11
  • 31
Loading