zoukankan      html  css  js  c++  java
  • 【笔记】Eclipse and Java for Total Beginners—009

    Lesson 09 – MyLibrary Class and ArrayList

    • How can we hold books, etc. in a collection?
    • MyLibrary object to hold Person & Entry objects
    • Introduce ArrayList in scrapbook
    • Introduce Java Generics
    • Method chaining

    使用ArrayList前,要添加java.util.

    1 ArrayList<Book> list = new ArrayList<Book>();
    2
    3 Book b1 = new Book("Great Expectations");
    4 Book b2 = new Book("War and Peace");
    5 list.add(b1);
    6 list.add(b2);
    7
    8 Person p1 = new Person();
    9 p1.setName("Fred");
    10 b1.setPerson(p1);
    11
    12 list.get(0).getPerson().getName();
    13
    14 list.remmove(b1);
    15 list

    问题:Inspect提示:

    Book cannot be resolved to a type
    Book cannot be resolved to a type
    Book cannot be resolved to a type
    Book cannot be resolved to a type
    Book cannot be resolved to a type
    Book cannot be resolved to a type

    问题解决方法:

    1> 在编辑代码时输入new后,按ALT+/ 自动补齐,无反应。

         windows / preferences / Java / Editor / Templates / 选new / Edit / 勾选 Automatically insert / 在context里选 Java。

    2>Book cannot be resolved to a type,添加Book

        1) Click the "Set Import Declarations" button right next to red square "Stop evaluation" button.

        2) Select "org.totalbeginner.tutorial* "-> click the "Add Type" button  

        3) Search for class 'Book' and 'Person' and add them to your MyScrapbook.jpage

    小结:

      在上面的代码里,用ArrayList列出了Library Class的特性,用Scrapbook验证时,可灵活调用各种方法,比如,根据Book对象去查询:

          list.indexOf(b2);

         或是由书查读者:

          list.get(0).getPerson().getName();

  • 相关阅读:
    递归的小实例
    try-catch-finally实例
    集合的排序(正序,倒序,从大到小排序)
    数组的排序(sort和冒泡)
    拦截器的使用,不登录用户不能进行其他操作
    把日志从数据库导出到Excel表格中(poi)
    Java 对Excel进行导入操作
    java 面试题集锦
    端口被占用解决办法
    (转)Java 最常见的 200+ 面试题汇总
  • 原文地址:https://www.cnblogs.com/halflife/p/2078558.html
Copyright © 2011-2022 走看看