开发者社区 问答 正文

java遍历list问题


        for (int i = 0; i <= max+1; i++) {
            list.add(i,0);
        }

        int c=0;
        for (Interval l : airplanes) {

            int start = l.start;
            int end = l.end;

            c++;
            System.out.println("第"+c+"次遍历开始");

            System.out.println("init data in ("+start+" , "+end+")");

            for (int i = start; i < end; i++) {

                list.add(i, list.get(i)+1);
                System.out.println("end ("+i+" <=> "+list.get(i)+")");
            }
        }
        return Collections.max(list);

screenshot

展开
收起
蛮大人123 2016-03-10 15:46:46 2075 分享 版权
2 条回答
写回答
取消 提交回答
  • java8前可以用Interator和for以及index下标,java8后可以用stream
    2019-07-17 18:57:47
    赞同 展开评论
  • 我说我不帅他们就打我,还说我虚伪

    Class AbstractList中 add 方法的文档:
    public void add(int index,E element)
    Inserts the specified element at the specified position in this list. Shifts the element currently at
    that position (if any) and any subsequent elements to the right (adds one to their indices).

    2019-07-17 18:57:47
    赞同 展开评论