08-Basic-Syntax-II

08-Basic-Syntax-II - 2006 Marty Hall Java 5 Syntax and...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
3 JSP, Servlet, Struts, JSF & Java 5 Training: http://courses.coreservlets.com © 2006 Marty Hall Java 5: Syntax and Utilities II 4 JSP, Servlet, Struts, JSF & Java 5 Training: http://courses.coreservlets.com © 2006 Marty Hall For live Java training, please see training courses at http://courses.coreservlets.com/. Servlets, JSP, Struts, JSF, AJAX, Java 5, etc. Taught by the author of Core Servlets and JSP , More Servlets and JSP , and this tutorial. Available at public venues, or customized versions can be held on-site at your organization.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
5 J2EE training: http://courses.coreservlets.com Agenda Mutating vs. returning results Data structures ArrayList LinkedList HashMap Generics printf varargs String vs. StringBuilder 6 J2EE training: http://courses.coreservlets.com Maintenance and Debugging: Return Results Return results, don't modify data secretly Unless there is an efficiency reason to modify data E.g., to avoid copying arrays Yes private String lastName = "Gates"; public void foo() { lastName = makeUpperCase(lastName); } No private String lastName = "Gates"; public void foo() { makeLastNameUpperCase(); }
Background image of page 2
7 J2EE training: http://courses.coreservlets.com Stretchable Ordered Data Create empty list new ArrayList() or new LinkedList() Note that you need "import java.util.*;" at the top of file Add entry to end add(value) (adds to end) or add(index, value) Retrieve n th element get(index) Check if element exists in list contains(element) Remove element remove(index) or remove(element) Number of elements size() 8 J2EE training: http://courses.coreservlets.com ArrayList Example import java.util.*; // Don't forget this import!! public class ListTest1 { public static void main(String[] args) { System.out.print("How many entries? "); Scanner inputScanner = new Scanner(System.in); int n = inputScanner.nextInt(); List entries = new ArrayList(); for(int i=0; i<n; i++) { entries.add ("This is entry " + i + "."); } String entry; for(int i=0; i< entries.size() ; i++) { entry = (String)entries.get(i); System.out.println(entry); } } }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
9 J2EE training: http://courses.coreservlets.com ArrayList Example: Explicit Typecasts (Output) > java ListTest1 How many entries? 8 This is entry 0. This is entry 1. This is entry 2. This is entry 3. This is entry 4. This is entry 5. This is entry 6. This is entry 7. 10 J2EE training: http://courses.coreservlets.com Comparing ArrayList and LinkedList Performance O(N) O(1) Access Nth Element O(1) O(1) if space O(N) if not O(1) amortized time Insert at end O(1) O(N) Insert at beginning List of Pointers ( LinkedList ) Array with Copying ( ArrayList )
Background image of page 4
11 J2EE training: http://courses.coreservlets.com Using Generics Find a data structure that accepts Object(s) ArrayList, LinkedList, HashMap, HashSet, Stack Not arrays! Declare the data structure with the type(s) in
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/16/2009 for the course CSCI 571 taught by Professor Papa during the Summer '07 term at USC.

Page1 / 14

08-Basic-Syntax-II - 2006 Marty Hall Java 5 Syntax and...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online