1 / 7

JAC444: Intro to Java Arrays and Vectors

JAC444: Intro to Java Arrays and Vectors. Tim McKenna Seneca@York. Arrays in Java. arrays are objects declare an array object reference int[] iAr; String[] strAr; // i.e. type[] new creates an array object specify the number of elements iAr = new int[3]; strAr = new String[5];

arlenedean
Télécharger la présentation

JAC444: Intro to Java Arrays and Vectors

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JAC444: Intro to Java Arrays and Vectors Tim McKenna Seneca@York

  2. Arrays in Java • arrays are objects • declare an array object reference • int[] iAr; String[] strAr; // i.e. type[] • new creates an array object • specify the number of elements • iAr = new int[3]; strAr = new String[5]; • auto initialization of array elements • primitive types: zero value • reference type: null

  3. Arrays in Java • length: public final instance variableiAr.length == 3; strAr.length == 5; • compile-time or runtime assignment of array element values • java.util.Arrays – utility methods to • copy, search, sort, fill, convert toString • ArrayIndexOutBoundException • Example: ArrayDemo.java, ArrayDemoStrings, ArrayDemoObjects

  4. Two-Dimensional Arrays • an array of arrays (i.e. an array of objects) • an irregular / unbalanced array • Examples: ArrayDemo2.java ArrayDemo3.java

  5. VectorClass • Java package: java.util, • part of the Java Collection Framework • vector: like an array that shrinks and grows • vector stores generic object references • ArrayList class is like a Vector but is not synchronized – less overhead for the JVM. • must be synchronized to support Threads

  6. Vector Class • useful methods: • isEmpty(), size() • add(), remove(), insertElementAt() • get(), set() • methods using the object's equals() method: contains(), indexOf(), lastIndexOf(), remove() • Examples: VectorDemoObjects.java, VectorDemoCars.java, ParkingLot.java, ParkingLotCars.java

  7. Collections passing • do not give out references to your collection – preserve encapsulation • java.util.Collections.unmodifiableXXX • allows modules to provide users with "read-only" access to internal collections.

More Related