秋日的晚霞 2021-05-21 16:37 采纳率: 94.4%
浏览 170
已采纳

ArrayList与Collection有什么区别吗

public class StudentDome {
    public static void main(String[] args) {
        ArrayList<Student> c=new ArrayList<>();
        Student s1 = new Student("张三",19);
        Student s2 = new Student("李四",20);
        c.add(s1);
        c.add(s2);
        Iterator<Student> sc=c.iterator();
        while (sc.hasNext()){
            Student i=sc.next();
            System.out.println(i.getName()+i.getAge());

        }

    }
}

与
public class StudentDome {
    public static void main(String[] args) {
        Collection<Student> c=new ArrayList<>();
        Student s1 = new Student("张三",19);
        Student s2 = new Student("李四",20);
        c.add(s1);
        c.add(s2);
        Iterator<Student> sc=c.iterator();
        while (sc.hasNext()){
            Student i=sc.next();
            System.out.println(i.getName()+i.getAge());

        }

    }
}

输出不是完全一样的吗

两者有什么区别吗  用途不一样吗

  • 写回答

1条回答 默认 最新

  • 封印di恶魔 2021-05-21 17:18
    关注

    Collection是接口,ArrayList是实体类

    接口不能自己实例化,实体类可以实例化,类的实例化对象可以用实现的接口接收

    ArrayList a = new ArrayList() 可以调用ArrayList所有的方法

    Collection a = new ArrayList()   只能调用Collection中有的方法

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料