Does anyone here know how to print elements from the array one by one? I have this code here:
import java.util.ArrayList;
import java.util.Iterator;
public class SimpleCollection {
private String name;
private ArrayList<String> elements;
public SimpleCollection(String name) {
this.name = name;
this.elements = new ArrayList<>();
}
public void add(String element) {
this.elements.add(element);
}
public ArrayList<String> getElements() {
return this.elements;
}
public String toString(){
String printOutput = "The collection "+ this.name+ " has "+ elements.size()+ " elements: ";
String e = "";
if(elements.isEmpty()){
return(printOutput+ " is empty.");
}if(elements.size()==1){
return "The collection "+ this.name+ " has "+ elements.size()+" element:"+ "\n"+ elements.get(0)+ "\n";
}
e = printOutput + "\n"+ getElements() + "\n";
return e;
}
}
The toString method of mine prints elements as an array: how can i print them one by one? Something like: The collection characters has 3 elements: magneto mystique phoenix