您的位置:首页 > 其它

第七次作业ArrayList集合

2017-11-27 10:32 253 查看
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();

        }

    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: