8. Hafta İçeriği ArrayList TheArrayList class supports dynamic arrays, which can grow or shrink as needed. In C#, standard arrays are of a fixed length,

Slides:



Advertisements
Similar presentations
Data Structures and Collections
Advertisements

1 Generic Collections Chapter Objectives You will be able to Use generic collection classes available in the.NET framework.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Collections. 2 Objectives Explore collections in System.Collections namespace –memory management –containment testing –sorting –traversal.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
TCSS 342, Winter 2005 Lecture Notes
C# Programming: From Problem Analysis to Program Design1 Advanced Collections C# Programming: From Problem Analysis to Program Design 3 rd Edition 8.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
1.  Collections are data structures that holds data in different ways for flexible operations  C# Collection classes are defined as part of the ◦ System.Collections.
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
Windows Programming Using C# Arrays, Collections.
Programming With Java ICS201 University Of Ha’il1 Chapter 14 Generics and The ArrayList Class.
Copyright © Texas Education Agency, Advanced Computer Programming Data Structures: Collections.
Arrays, Lists, Stacks, Queues Static and Dynamic Implementation Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
ARRAYLIST.. Hazen High School. Vocabulary to Know ArrayList Generic Class ArrayList Operations ArrayList Methods ArrayList Searching For-Each Wrapper.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
ArrayList, Multidimensional Arrays
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.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
Evolution of.NET Collections (C#) Chen Dong
Data structures Abstract data types Java classes for Data structures and ADTs.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 8 Collection.
1 Chapter 20 Lists, Stacks, Queues Lecture 7 Dr. Musab Zghoul برمجة هيكلية.
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
1 COMP3100e Developing Microsoft.Net Applications for Windows (Visual Basic.Net) Class 6 COMP3100E.
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.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Understanding Data Types and Collections Lesson 2.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Arrays and Collections Tonga Institute of Higher Education.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
Interface: (e.g. IDictionary) Specification class Appl{ ---- IDictionary dic; dic= new XXX(); application class: Dictionary SortedDictionary ----
Object Oriented Programming Generic Collections and LINQ Dr. Mike Spann
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
Generics Generics vs. heterogeneous collections Doing your own generics FEN 2014UCN Teknologi/act2learn1.
C# Collections & Generics C#.NET Software Development Version 1.0.
Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke.
1 9/22/05CS360 Windows Programming Arrays, Collections, Hash Tables, Strings.
Data Structures and Collections Principles.NET: –Two libraries: System.Collections System.Collections.Generics FEN 2014UCN Teknologi/act2learn1 Deprecated.
CSE 1201 Object Oriented Programming ArrayList 1.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Generics & Collection Classes Version 1.0. Topics Generic Methods and Classes Generic Collection Classes List Enumerators Queue Stack LinkedList.
1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.
Collections Dwight Deugo Nesa Matic
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
C# Collections & Generics
Module 5: Programming with C#. Overview Using Arrays Using Collections Using Interfaces Using Exception Handling Using Delegates and Events.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 2 nd Lecture Pavel Ježek
Understanding Data Types and Collections Lesson 2.
Advanced .NET Programming I 2nd Lecture
Advanced Data Collections
Computing with C# and the .NET Framework
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Collections 24: Collections Programming C# © 2003 DevelopMentor, Inc.
CS313D: Advanced Programming Language
CS1S467 GUI Programming LECTURE 11 Collections
Programming in Java Lecture 11: ArrayList
Fundaments of Game Design
Basic Collections.
Hashing in java.util
Web Design & Development Lecture 6
Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7.
Presentation transcript:

8. Hafta İçeriği

ArrayList TheArrayList class supports dynamic arrays, which can grow or shrink as needed. In C#, standard arrays are of a fixed length, which cannot be changed during program execution. This means you must know in advance how many elements an array will hold. But sometimes you may not know until runtime precisely how large an array you will need. To handle this situation, use ArrayList. An ArrayList is a variable-length array of object references that can dynamically increase or decrease in size. An ArrayList is created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array can be shrunk. ArrayList is currently in wide use in existing code. For this reason, it is examined in depth here. However, many of the same techniques that apply to ArrayList apply to the other collections as well, including the generic collections. ArrayList implements ICollection, IList, IEnumerable, and ICloneable. ArrayList has the constructors

PropertyDescription CapacityGets or sets the number of elements that the ArrayList can contain. CountGets the number of elements actually contained in the ArrayList. IsFixedSizeGets a value indicating whether the ArrayList has a fixed size. IsReadOnlyGets a value indicating whether the ArrayList is read-only. ItemGets or sets the element at the specified index.

Sr.No.Methods 1public virtual int Add(object value);Adds an object to the end of the ArrayList. 2public virtual void AddRange(ICollection c);Adds the elements of an ICollection to the end of the ArrayList. 3public virtual void Clear();Removes all elements from the ArrayList. 4public virtual bool Contains(object item);Determines whether an element is in the ArrayList. 5public virtual ArrayList GetRange(int index, int count);Returns an ArrayList which represents a subset of the elements in the source ArrayList. 6public virtual int IndexOf(object);Returns the zero-based index of the first occurrence of a value in the ArrayList or in a portion of it. 7public virtual void Insert(int index, object value);Inserts an element into the ArrayList at the specified index. 8public virtual void InsertRange(int index, ICollection c);Inserts the elements of a collection into the ArrayList at the specified index. 9public virtual void Remove(object obj);Removes the first occurrence of a specific object from the ArrayList. 10public virtual void RemoveAt(int index);Removes the element at the specified index of the ArrayList. 11public virtual void RemoveRange(int index, int count);Removes a range of elements from the ArrayList. 12public virtual void Reverse();Reverses the order of the elements in the ArrayList. 13public virtual void SetRange(int index, ICollection c);Copies the elements of a collection over a range of elements in the ArrayList. 14public virtual void Sort();Sorts the elements in the ArrayList. 15public virtual void TrimToSize();Sets the capacity to the actual number of elements in the ArrayList.

SAÜ Bilgisayar Mühendisliği Dr. Cemil Öz using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace listarray1 { // ArrayList demosu. class ArrayListDemo { static void Main() { // bir array list oluştur. ArrayList al = new ArrayList(); Console.WriteLine(" ilk eleman sayısı : " + al.Count); Console.WriteLine(); Console.WriteLine(" 6 eleman ekleme "); // array list'e eleman ekleme al.Add('C'); al.Add('A'); al.Add('E'); al.Add('B'); al.Add('D'); al.Add('F'); Console.WriteLine(" Eleman sayısı : " + al.Count); // dizi indisi kullanılarak dizi elemanlarını görster Console.Write(" Su andaki elemanlar "); for (int i = 0; i < al.Count; i++) Console.Write(al[i] + " "); Console.WriteLine("\n"); Console.WriteLine(" 2 elemanı silme "); // Remove elements from the array list. al.Remove('F'); al.Remove('A'); Console.WriteLine(" eleman sayısı: " + al.Count);

SAÜ Bilgisayar Mühendisliği Dr. Cemil Öz // Use foreach loop to display the list. Console.Write(" elemanlar: "); foreach (char c in al) Console.Write(c + " "); Console.WriteLine("\n"); Console.WriteLine(" 20 ilave eleman ekleme "); // Add enough elements to force al to grow. for (int i = 0; i < 20; i++) al.Add((char)('a' + i)); Console.WriteLine(" mevcut kapasite: " + al.Capacity); Console.WriteLine(" 20 eleman ekledikten sonra eleman sayısı: " + al.Count); Console.Write(" elemanlar : "); foreach (char c in al) Console.Write(c + " "); Console.WriteLine("\n"); // Change contents using array indexing. Console.WriteLine(" ilk üç elemanı değiştirme "); al[0] = 'X'; al[1] = 'Y'; al[2] = 'Z'; Console.Write(" elemanlar: "); foreach (char c in al) Console.Write(c + " "); Console.WriteLine(); Console.ReadKey(); }

The SortedList class represents a collection of key-and-value pairs that are sorted by the keys and are accessible by key and by index. A sorted list is a combination of an array and a hash table. It contains a list of items that can be accessed using a key or an index. If you access items using an index, it is an ArrayList, and if you access items using a key, it is a Hashtable. The collection of items is always sorted by the key value.

using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace listarray1 { class SortedListDemo { static void Main() { SortedList sl = new SortedList(); sl.Add("001", "Zara Ali"); sl.Add("002", "Abida Rehman"); sl.Add("003", "Joe Holzner"); sl.Add("004", "Mausam Benazir Nur"); sl.Add("005", "M. Amlan"); sl.Add("006", "M. Arif"); sl.Add("007", "Ritesh Saikia"); if (sl.ContainsValue("Nuha Ali")) { Console.WriteLine("This student name is already in the list"); } else { sl.Add("008", "Nuha Ali"); } // get a collection of the keys. ICollection key = sl.Keys; foreach (string k in key) { Console.WriteLine(k + ": " + sl[k]); } Console.ReadKey(); }

The Hashtable class The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. It uses the key to access the elements in the collection. A hash table is used when you need to access elements by using key, and you can identify a useful key value. Each item in the hash table has a key/value pair. The key is used to access the items in the collection.

PropertyDescription CountGets the number of key-and-value pairs contained in the Hashtable. IsFixedSizeGets a value indicating whether the Hashtable has a fixed size. IsReadOnlyGets a value indicating whether the Hashtable is read-only. ItemGets or sets the value associated with the specified key. KeysGets an ICollection containing the keys in the Hashtable. ValuesGets an ICollection containing the values in the Hashtable. Sr.No.Method 1public virtual void Add(object key, object value);Adds an element with the specified key and value into the Hashtable. 2public virtual void Clear();Removes all elements from the Hashtable. 3public virtual bool ContainsKey(object key);Determines whether the Hashtable contains a specific key. 4public virtual bool ContainsValue(object value);Determines whether the Hashtable contains a specific value. 5public virtual void Remove(object key);Removes the element with the specified key from the Hashtable. The following table lists some of the commonly used methods of the Hashtable class:

using System; using System.Collections; namespace CollectionsApplication { class Program { static void Main(string[] args) { Hashtable ht = new Hashtable(); ht.Add("001", "Zara Ali"); ht.Add("002", "Abida Rehman"); ht.Add("003", "Joe Holzner"); ht.Add("004", "Mausam Benazir Nur"); ht.Add("005", "M. Amlan"); ht.Add("006", "M. Arif"); ht.Add("007", "Ritesh Saikia"); if (ht.ContainsValue("Nuha Ali")) { Console.WriteLine("This student name is already in the list"); } else { ht.Add("008", "Nuha Ali"); } // Get a collection of the keys. ICollection key = ht.Keys; foreach (string k in key) { Console.WriteLine(k + ": " + ht[k]); } Console.ReadKey(); }

deque. It represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue, and when you remove an item, it is called deque.

PropertyDescription CountGets the number of elements contained in the Queue. Sr.No.Methods 1public virtual void Clear();Removes all elements from the Queue. 2public virtual bool Contains(object obj);Determines whether an element is in the Queue. 3public virtual object Dequeue();Removes and returns the object at the beginning of the Queue. 4public virtual void Enqueue(object obj);Adds an object to the end of the Queue. 5public virtual object[] ToArray();Copies the Queue to a new array. 6public virtual void TrimToSize();Sets the capacity to the actual number of elements in the Queue. methods of the Queueclass:

using System; using System.Collections; namespace CollectionsApplication { class Program { static void Main(string[] args) { Queue q = new Queue(); q.Enqueue('A'); q.Enqueue('M'); q.Enqueue('G'); q.Enqueue ('W'); Console.WriteLine("Current queue: "); foreach (char c in q) Console.Write(c + " "); Console.WriteLine(); q.Enqueue('V'); q.Enqueue('H'); Console.WriteLine("Current queue: "); foreach (char c in q) Console.Write(c + " "); Console.WriteLine(); Console.WriteLine("Removing some values "); char ch = (char)q.Dequeue(); Console.WriteLine("The removed value: {0}", ch); ch = (char)q.Dequeue(); Console.WriteLine("The removed value: {0}", ch); Console.ReadKey(); } currrent queue: A M G W Current queue: A M G W V H Removing values The removed value: A The removed value: M

Stack It represents a last-in, first out collection of object. It is used when you need a last-in, first-out access of items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item.

PropertyDescription CountGets the number of elements contained in the Stack. Sr.No.Methods 1public virtual void Clear();Removes all elements from the Stack. 2public virtual bool Contains(object obj);Determines whether an element is in the Stack. 3public virtual object Peek();Returns the object at the top of the Stack without removing it. 4public virtual object Pop();Removes and returns the object at the top of the Stack. 5public virtual void Push(object obj);Inserts an object at the top of the Stack. 6public virtual object[] ToArray();Copies the Stack to a new array. methods of the Stackclass:

using System; using System.Collections; namespace CollectionsApplication { class Program { static void Main(string[] args) { Stack st = new Stack(); st.Push('A'); st.Push('M'); st.Push('G'); st.Push('W'); Console.WriteLine("Current stack: "); foreach (char c in st) { Console.Write(c + " "); } Console.WriteLine(); st.Push('V'); st.Push('H'); Console.WriteLine("The next poppable value in stack: {0}", st.Peek()); Console.WriteLine("Current stack: "); foreach (char c in st) { Console.Write(c + " "); } Console.WriteLine(); Console.WriteLine("Removing values "); st.Pop(); Console.WriteLine("Current stack: "); foreach (char c in st) { Console.Write(c + " "); } Current stack: W G M A The next poppable value in stack: H Current stack: H V W G M A Removing values Current stack: G M A