DOC PREVIEW
USC CSCI 571 - 03Java-ArraysEtc

This preview shows page 1-2-24-25 out of 25 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Basic Java SyntaxAgendaArraysThe Array length FieldBuilding ArraysBuilding Arrays, cont.Multidimensional ArraysData StructuresUseful Vector MethodsSlide 10Useful Hashtable MethodsCollections FrameworkCollection InterfacesCollections ClassWrapper ClassesWrapper UsesWrappers: Converting StringsError Handling: ExceptionsException HierarchyThrowable TypesMultiple Catch ClausesTry-Catch, ExampleThe finally ClauseThrown ExceptionsSummary1© 2001-2002 Marty Hall, Larry Brown http://www.corewebprogramming.comWebcoreprogram mingBasic Java SyntaxBasic Java Syntax2www.corewebprogramming.comAgenda•Building arrays•Vectors and Hashtables•Data structures introduced in Java 2•Using wrappers to convert primitive data types to objects•Handling exceptionsBasic Java Syntax3www.corewebprogramming.comArrays•Accessing arrays–Access arrays by supplying the index in square brackets after the variable name, variableName[index]–The first index is 0, not 1 •Example–Here, the argument to main is an array of Strings called args public class Test { public static void main(String[] args) { System.out.println("First argument: " + args[0]); }}> javac Test.java> java Test Hello ThereFirst argument is HelloBasic Java Syntax4www.corewebprogramming.comThe Array length Field•Arrays have a built-in field called length that stores the size of the array –The length is one bigger than the biggest index, due to the fact that the index starts at 0 •Example public class Test2 { public static void main(String[] args) { System.out.println("Number of args is " + args.length); } } > javac Test2.java > java Test2 Number of args is 0 > java Test2 Hello There Number of args is 2Basic Java Syntax5www.corewebprogramming.comBuilding Arrays•Arrays can be built in a one-step or two-step process 1. The one-step process is of the following form: type[] var = { val1, val2, ... , valN };•For example: int[] values = { 10, 100, 1000 }; Point[] points = { new Point(0, 0), new Point(1, 2), ... };Basic Java Syntax6www.corewebprogramming.comBuilding Arrays, cont.2. With the two-step process, first allocate an array of references: type[] var = new type[size];•For example: int[] values = new int[7]; Point[] points = new Point[length];•Second, populate the array points[0] = new Point(...); points[1] = new Point(...); ...Basic Java Syntax7www.corewebprogramming.comMultidimensional Arrays•Multidimensional arrays are implemented as an array of arrays int[][] twoD = new int[64][32]; String[][] cats = { { "Caesar", "blue-point" }, { "Heather", "seal-point" }, { "Ted", "red-point" } };Basic Java Syntax8www.corewebprogramming.comData Structures•Java 1.0 introduced two synchronized data structures in the java.util package–Vector•A stretchable (resizable) array of Objects•Time to access an element is constant regardless of position•Time to insert element is proportional to the size of the vector–Hashtable•Stores key-value pairs as Objects•Neither the keys or values can be null•Time to access/insert is proportional to the size of the hashtableBasic Java Syntax9www.corewebprogramming.comUseful Vector Methods•addElement/insertElementAt/setElementAt–Add elements to the vector•removeElement/removeElementAt–Removes an element from the vector•firstElement/lastElement–Returns a reference to the first and last element, respectively (without removing)•elementAt–Returns the element at the specified index•indexOf–Returns the index of an element that equals the object specified•contains–Determines if the vector contains an objectBasic Java Syntax10www.corewebprogramming.comUseful Vector Methods•elements–Returns an Enumeration of objects in the vector Enumeration elements = vector.elements(); while(elements.hasMoreElements()) { System.out.println(elements.nextElement()); } •size–The number of elements in the vector•capacity–The number of elements the vector can hold before becoming resizedBasic Java Syntax11www.corewebprogramming.comUseful Hashtable Methods•put/get–Stores or retrieves a value in the hashtable•remove/clear–Removes a particular entry or all entries from the hashtable•containsKey/contains–Determines if the hashtable contains a particular key or element •keys/elements–Returns an enumeration of all keys or elements, respectively•size–Returns the number of elements in the hashtable•rehash–Increases the capacity of the hashtable and reorganizes itBasic Java Syntax12www.corewebprogramming.comCollections Framework•Additional data structures added by Java 2 PlatformCollectionSetSortedSetListArrayListLinkedListVector†HashSetTreeSetMapSortedMapHashMapHashtable†TreeMapInterfaceConcrete class†Synchronized AccessBasic Java Syntax13www.corewebprogramming.comCollection Interfaces•Collection–Abstract class for holding groups of objects•Set–Group of objects containing no duplicates•SortedSet–Set of objects (no duplicates) stored in ascending order–Order is determined by a Comparator•List–Physically (versus logically) ordered sequence of objects•Map–Stores objects (unordered) identified by unique keys•SortedMap–Objects stored in ascending order based on their key value–Neither duplicate or null keys are permittedBasic Java Syntax14www.corewebprogramming.comCollections Class•Use to create synchronized data structures List list = Collection.synchronizedList(new ArrayList());Map map = Collections.synchronizedMap(new HashMap());•Provides useful (static) utility methods–sort•Sorts (ascending) the elements in the list–max, min•Returns the maximum or minimum element in the collection–reverse•Reverses the order of the elements in the list–shuffle•Randomly permutes the order of the elementsBasic Java Syntax15www.corewebprogramming.comWrapper Classes•Each primitive data type has a corresponding object (wrapper class)–The data is stored as an immutable field of the objectPrimitive CorrespondingData Type Object Class byte Byte short Short int Integer long Long float Float double Double char Character boolean BooleanBasic Java Syntax16www.corewebprogramming.comWrapper Uses•Defines useful constants for each data type –For example, Integer.MAX_VALUEFloat.NEGATIVE_INFINITY•Convert between


View Full Document

USC CSCI 571 - 03Java-ArraysEtc

Download 03Java-ArraysEtc
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view 03Java-ArraysEtc and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view 03Java-ArraysEtc 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?