I'm quite new to Java and have come accross to a strange behaviour that I can not explain why this happens or where is the mistake in my code.
Here's the code:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
abstract class Shape {
public abstract void printMe(String no);
}
final class Circle extends Shape {
@Override
public void printMe(String no){
System.out.println("This is Circle no: " + no);
}
}
final class Square extends Shape {
@Override
public void printMe(String no) {
System.out.println("This is Square no: " + no);
}
}
final class Triangle extends Shape {
@Override
public void printMe(String no) {
System.out.println("This is Triangle no: " + no);
}
}
public class Foo {
private ArrayList<Shape> shapes;
public Foo(){
this.shapes = new ArrayList<Shape>();
this.shapes.add(new Circle());
this.shapes.add(new Square());
this.shapes.add(new Triangle());
}
public void printShapes(ArrayList<String> numbers){
for(String s:numbers){
Iterator<Shape> iter = this.shapes.iterator();
Shape shape = iter.next();
shape.printMe(s);
}
}
public static void main(String[] args) {
ArrayList<String> numbers = new ArrayList<String>(Arrays.asList("1", "2", "3"));
Foo foo = new Foo();
foo.printShapes(numbers);
}
}
The output I'd expect would be:
This is Circle no: 1
This is Square no: 2
This is Triangle no: 3
However, the output I get is:
This is Circle no: 1
This is Circle no: 2
This is Circle no: 3
What am I doing wrong?
将此行拉出循环:
Iterator<Shape> iter = this.shapes.iterator();
You're always getting a new iterator--instead of using the same one.
It's not clear to me why you're doing it like this anyway; either pass an integer and loop until it runs out, or iterate over the shapes and keep a counter. Passing a string array strikes me as clumsy.
public void printShapes() {
int i = 1;
for (Shape shape : shapes) {
shape.printMe(i++); // And modify the method to take an int.
}
}
I'm uncomfortable with a shape needing to be aware it can have a position. If this is a requirement, create a "PositionalShape" or something (but ew), or have shapes output a string representation that can be composited with additional info like a list position, or create a shape decorator, etc.
// (If you're really trying to print the first n shapes)
public void printShapes(int n) {
Iterator<Shape> iter = shapes.iterator();
for (int i = 0; i < n; i++) {
Shape shape = iter.next();
shape.printMe("" + i+1);
}
}
Look at Iterator<Shape> iter
inside your loop.
public void printShapes(ArrayList<String> numbers){
for(String s:numbers){
Iterator<Shape> iter = this.shapes.iterator();
Shape shape = iter.next();
shape.printMe(s);
}
}
You are always grabbing the first shape (initialize iterator, grab next)
I suspect you need to see this in your debugger however with
Iterator<Shape> iter = this.shapes.iterator();
Shape shape = iter.next();
shape.printMe(s);
You are using the first share every time (which is a Circle)
You can move the iter
declaration outside the loop to fix it.
The following 2 lines need to be in a loop: Shape shape = iter.next(); shape.printMe(s);
Shape shape = iter.next(); shape.printMe(s);
您总是重置迭代器:
Iterator<Shape> iter = this.shapes.iterator();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.