黑马程序员技术交流社区

标题: ArrayList存储字符串并遍历 [打印本页]

作者: Over_Mstuang    时间: 2015-8-18 20:13
标题: ArrayList存储字符串并遍历
public class ArrayListDemo {
public static void main(String[] args) {
  // 创建集合对象
  ArrayList array = new ArrayList();
  // 创建并添加元素
  array.add("hello");
  array.add("world");
  array.add("java");
  // 遍历集合
  Iterator it = array.iterator();
  while (it.hasNext()) {
   String s = (String) it.next();
   System.out.println(s);
  }
  System.out.println("-----------");
  // for (int x = 0; x < array.size(); x++) {
  // String s = (String) array.get(x);
  // System.out.println(s);
  // }
  int length = array.size();
  for (int x = 0; x < length; x++) {
   String s = (String) array.get(x);
   System.out.println(s);
  }
}
}






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2