Java中list顯示的三種方法

terryisme發表於2009-03-05

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

public class TestList {
private List list = new ArrayList();

private void initializelist() {
for (int i = 1; i < 5; i++) {
list.add("111");
}
}
private void showIterator() {
for (Iterator it = list.iterator(); it.hasNext();) {
String str = (String) it.next();
System.out.println(str);
}
}
private void showlistwhile() {
Iterator it = list.iterator();
while (it.hasNext()) {
String str = (String) it.next();
System.out.println(str);
}
}
private void showlistsize() {
for (int i = 0; i < list.size(); i++) {
String str = list.get(i).toString();
System.out.println(str);
}
}
public static void main(String[] args) {
TestList testlist = new TestList();
testlist.initializelist();
// testlist.showIterator();
// testlist.showlistlength();
testlist.showlistwhile();
}
}
[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/93029/viewspace-1018104/,如需轉載,請註明出處,否則將追究法律責任。

相關文章