第七次作业ArrayList集合

来源:互联网 发布:126邮箱imap端口 编辑:程序博客网 时间:2024/06/06 01:27
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.management.RuntimeErrorException;
public class test {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        List<Integer> arrayList = new ArrayList<Integer>();
        for(int i = 0; i < 100 ; i++){
            arrayList.add(new Integer((int) (Math.random() * 1000)));
        }
        System.out.println("迭代器输出结果:");
        Iterator<Integer> iterator = arrayList.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next().intValue());
        }
        try {
            System.out.print("调用get()读取索引位置为50的元素:");
            System.out.println(arrayList.get(50));
            System.out.print("调用get()读取索引位置为102的元素:");
            System.out.println(arrayList.get(102));
        } catch (IndexOutOfBoundsException e) {
            // TODO Auto-generated catch block
            System.out.println("数组越界了");
            e.printStackTrace();
        }
    }
}
原创粉丝点击