Chengyu Sun California State University, Los Angeles

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Data Structures and Collections
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
1 Generic Collections Chapter Objectives You will be able to Use generic collection classes available in the.NET framework.
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 21 Generics.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L16 (Chapter 22) Java Collections.
Chapter 19 Java Data Structures
Java Collections. Collections, Iterators, Algorithms CollectionsIteratorsAlgorithms.
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Collections. Why collections? Collections are used to hold a collection of objects. List holds objects based on order of insertion and can hold non unique.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Generics Collections. Why do we need Generics? Another method of software re-use. When we implement an algorithm, we want to re-use it for different types.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Chapter 18 Java Collections Framework
Data structures and algorithms in the collection framework 1.
Collection 105 Yola. To store data in RAM Variables (name all the types with their length) Arrays (one, two or more) Collections and Maps.
Generics Collections. Why do we need Generics? Another method of software re-use. When we implement an algorithm, we want to re-use it for different types.
Interface: (e.g. IDictionary) Specification class Appl{ ---- IDictionary dic; dic= new XXX(); application class: Dictionary SortedDictionary ----
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
13 Collections Framework. 2 Contents What is Collection? Collections Framework Collections Hierarchy Collections Implementations Set List Map.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
More Java: Static and Final, Abstract Class and Interface, Exceptions, Collections Framework 1 CS300.
Data Structures and Collections Principles.NET: –Two libraries: System.Collections System.Collections.Generics FEN 2014UCN Teknologi/act2learn1 Deprecated.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 21 Sets and Maps.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
CS202 Java Object Oriented Programming Introduction to Collection Classes Chengyu Sun California State University, Los Angeles.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 2 nd Lecture Pavel Ježek
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Chapter 21 Sets and Maps Jung Soo (Sue) Lim Cal State LA.
Slides by Donald W. Smith
Using the Java Collection Libraries COMP 103 # T2
Building Java Programs
Advanced .NET Programming I 2nd Lecture
Some Collections: BAGS, SETS, and STACKS
Chapter 19 Java Data Structures
Wednesday Notecards. Wednesday Notecards Wednesday Notecards.
Software Development Java Collections
JAVA COLLECTIONS LIBRARY
JAVA COLLECTIONS LIBRARY
University of Central Florida COP 3330 Object Oriented Programming
Inheritance & Polymorphism
CS240: Advanced Programming Concepts
Road Map CS Concepts Data Structures Java Language Java Collections
Java Collections Overview
Building Java Programs
Generics A Brief Review 16-Nov-18.
Introduction to Collections
Introduction to Collections
Introduction to Collections
Introduction to Collections
Welcome to CSE 143! Go to pollev.com/cse143.
CS2110: Software Development Methods
Building Java Programs
Introduction to Collections
Introduction to Collections
Fundaments of Game Design
CS 240 – Advanced Programming Concepts
Chengyu Sun California State University, Los Angeles
Interfaces, Enumerations, Boxing, and Unboxing
Presentation transcript:

Chengyu Sun California State University, Los Angeles CS4540 Special Topics in Web Development C# for Java Programmers: Exceptions and Collections Chengyu Sun California State University, Los Angeles

Exception Handling Exception handling is mostly like in Java No checked exception in C# (Yay!) static void FutureFeature() {throw new NotImplementedException(); } static void Main(string[] args) { try { FutureFeature(); } catch (NotImplementedException e) { Console.WriteLine(e.Message); } finally { Console.WriteLine("Done!"); }

What's Inside an Exception Important properties in Exception Message StackTrace TargetSite: can be used to obtain information about the method that threw the exception Data: additional data (a collection of key-value pairs) associated with the exception

Using Standard Exceptions There is already a large collection of exceptions defined in the System namespace Some exceptions for common programming problems

Creating A Custom Exception Minimum for a custom exception Inherit from Exception or ApplicationException class Have a no-argument constructor Have a constructor that takes a string message Or, use the ex code snippet in Visual Studio

Common Collections in Java Interface Class Characteristics List ArrayList, LinkedList Ordered; allow duplicates Set HashSet, LinkedHashSet, TreeSet Un-ordered or ordered in a specific way; no duplicates Map HashMap, LinkedHashMap, TreeMap Key-Value pairs Queue, Deque PriorityQueue, Stack Common data structures

Generics Type Parameter public class GenericList<T> { public void Add(T input) { } } var list = new GenericList<int>(); Type Argument Most commonly used to ensure all the elements in a collection is of the same type

Why Not Use Collection of Objects Performance: has to convert back and forth between value types and reference types (aka boxing/unboxing) Type safety Cannot prevent different types of data getting into the same collection Require explicit cast when accessing data

About Collections in C# Non-generic collections are defined in System.Collections Generic collections are defined in System.Collections.Generic

Common Collections in C# Interface Class Characteristics IList List, LinkedList, SortedList Ordered; allow duplicates ISet HashSet, SortedSet Un-ordered or ordered in a specific way; no duplicates IDictionary Dictionary, SortedDictionary Key-Value pairs Queue, Stack Common data structures

Example: English-Spanish Translator Download the dictionary file from the Internet Dictionary Project Read the file and store the dictionary in a Dictionary<string, List<string>> Get an English word from input and displays its Spanish translations

Reading Text Files Using C# Read the whole file using System.IO.File Read one line at a time using System.IO.StreamReader

Element Access Add, Remove, RemoveAt, RemoveAll … Dictionary.TryGetValue() The "Item" property in Dictionary and List More on indexer methods later

A Couple of Important Interfaces IEnumerable<T>: foreach can be used on anything implementing this interface ICollection<T> Implements IEnumerable<T> Count, Add, Remove, Contains

Readings Pro C# 7: Chapter 7, 8, 9