目录

  • 1 Introduction
    • 1.1 Introduction
    • 1.2 Machine Lang, Assembly lang and High-level lang
    • 1.3 History of Java
    • 1.4 Characteristics of Java
    • 1.5 Typical Java Development Environment
    • 1.6 Introduction to Java Application
  • 2 Classes and Objects
    • 2.1 Primitive Types vs. Reference Types
    • 2.2 Classes and Objects
      • 2.2.1 Declaring a Class
      • 2.2.2 Local and Instance Variables
      • 2.2.3 Methods: A Deeper Look
      • 2.2.4 Constructors
  • 3 Control Statements
    • 3.1 Control Structures
    • 3.2 if Selection
    • 3.3 while Repetition
    • 3.4 for Repetition
    • 3.5 do…while repetition
    • 3.6 switch multiple-selection
  • 4 Arrays and Collections
    • 4.1 Arrays
    • 4.2 ArrayList
    • 4.3 Set
    • 4.4 Generic Programming
  • 5 Object-Oriented Programming: Inheritance
    • 5.1 Inheritance
    • 5.2 Superclasses and Subclasses
    • 5.3 Constructors in Subclasses
  • 6 Object-Oriented Programming: Polymorphism
    • 6.1 Polymorphism
    • 6.2 Polymorphic Behavior
    • 6.3 Enable and Disable Polymorphism
  • 7 Exception Handling
    • 7.1 Exceptions
    • 7.2 Error-Handling
  • 8 Java I/O
    • 8.1 Java I/O Streams
    • 8.2 Decorator Design Pattern
  • 9 GUI Components
    • 9.1 AWT and SWING
    • 9.2 Event Model
  • 10 Multithreading
    • 10.1 Life Cycle of a Thread
    • 10.2 Thread Synchronization
    • 10.3 Producer/Consumer
ArrayList

Introduction to Collections 

}Java API provides several predefined data structures, called collections, used to store groups of related objects. 

  §Each provides efficient methods that organize, store and retrieve your data without requiring knowledge of how the data is being stored. 

  §Reduce application-development time. 

}Arrays do not automatically change their size at execution time to accommodate additional elements. 

}ArrayList<T>(package java.util) can dynamically change its size to accommodate more elements. 

  §T is a placeholder for the type of elementstored in the collection. 

  §This is similar to specifying the type when declaring an array, except that only non primitive types can be used with these collection classes. 

}Classes with this kind of placeholder that can be used with any type are called generic classes

Interface Collection and Class Collections







}Interface Collection is the root interface from which interfaces Set, Queue and List are derived. 

}Interface Set defines a collection that does not contain duplicates. 

}Interface Queue defines a collection that represents a waiting line.

}Interface Collection contains bulk operations for adding, clearing and comparing objects in a collection. 

}A Collection can be converted to an array. 

}Interface Collection provides a method that returns an Iterator object, which allows a program to walk through the collection and remove elements from the collection during the iteration. 

}Class Collections provides static methods that search, sort and perform other operations on collections. 


Class ArrayList


}An  ArrayList’s capacity indicates how many items it can hold without growing. 

}When the ArrayList grows, it must create a larger internal array and copy each element to the new array. 

  §This is a time-consuming operation. It would be inefficient for the ArrayList to grow each time an element is added. 

  §An ArrayList grows only when an element is added and the number of elements is equal to the capacity—i.e., there is no space for the new element.

}Method add adds elements to the ArrayList

     §One-argument version appends its argument to the end of the ArrayList

  §Two-argumentversion inserts a new element at the specified position. 

  §Collection indices start at zero. 

}Method size returns the number of elements in the ArrayList.

}Method get obtains the element at a specified index. 

}Method remove deletes an element with a specificvalue. 

  §An overloaded version of the method removes the element at the specified index. 

}Method contains determines if an item is in the ArrayList