使用ArrayList集合,對其新增100個不同的元素
阿新 • • 發佈:2019-02-20
package 集合; import java.util.ArrayList; import java.util.Iterator; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub ArrayList list = new ArrayList<>(); for(int i =1;i<101;i++) list.add(i); Iterator it = list.iterator(); while(it.hasNext()) System.out.print(it.next()+" "); System.out.println(list.get(50).toString()); try { System.out.println(list.get(102).toString()); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); System.out.println("下標越界異常"); } } }