Data Structures and Database Applications Arrays And Lists

Slides:



Advertisements
Similar presentations
Generics, Lists, Interfaces
Advertisements

Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Arrays.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Arrays in Java Selim Aksoy Bilkent University Department of Computer Engineering
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
1 Dr. Seuss again: "Too Many Daves"  Did I ever tell you that Mrs. McCave Had twenty-three sons, and she named them all Dave?  Well, she did. And that.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
CSE 501N Fall ‘09 08: Arrays 22 September 2009 Nicholas Leidenfrost.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 7 Arrays 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
1 Objects for Organizing Data -- Introduction zAs our programs get more sophisticated, we need assistance organizing large amounts of data zChapter 6 focuses.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
The ArrayList Data Structure The Most Important Things to Review.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Arrays. Arrays are objects that help us organize large amounts of information.
Chapter 7 Arrays…. 7-2 Arrays An array is an ordered list of values An array of size N is indexed from.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Lecture 5 array declaration and instantiation array reference
Arrays of Objects October 9, 2006 ComS 207: Programming I (in Java)
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Chapter 6: Arrays Java Software Solutions
Lecture 10 Collections Richard Gesick.
Chapter 6: Using Arrays.
Two-Dimensional Arrays
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
ARRAYLIST AND VECTOR.
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.
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Can store many of the same kind of data together
Chapter 8 Slides from GaddisText
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Can store many of the same kind of data together
Object Oriented Programming in java
Can store many of the same kind of data together
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Arrays in Java.
Fundaments of Game Design
Visual Programming COMP-315
Review: libraries and packages
Arrays of Objects October 8, 2007 ComS 207: Programming I (in Java)
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
Arrays and ArrayLists.
Arrays.
Presentation transcript:

Data Structures and Database Applications Arrays And Lists

Each value has a numeric index Arrays An array is an ordered list of values scores The entire array has a single name Each value has a numeric index 0 1 2 3 4 5 6 7 8 9 79 87 94 82 67 98 87 81 74 91 An array of size N is indexed from zero to N-1 This array holds 10 values that are indexed from 0 to 9

Arrays A particular value in an array is referenced using the array name followed by the index in brackets For example, the expression scores[2] refers to the value 94 (the 3rd value in the array) That expression represents a place to store a single integer and can be used wherever an integer variable can be used

Arrays For example, an array element can be assigned a value, printed, or used in a calculation: scores[2] = 89; scores[first] = scores[first] + 2; mean = (scores[0] + scores[1])/2;

Arrays The values held in an array are called array elements An array stores multiple values of the same type – the element type The element type can be a primitive type or an object reference Therefore, we can create an array of integers, an array of characters, an array of String objects, an array of Coin objects, etc. In Java, the array itself is an object that must be instantiated

Arrays Another way to depict the scores array: 79 87 94 82 67 98 81 74 91

int[] scores = new int[10]; Declaring Arrays The scores array could be declared as follows: int[] scores = new int[10]; The type of the variable scores is int[] (an array of integers) Note that the array type does not specify its size, but each object of that type has a specific size The reference variable scores is set to a new array object that can hold 10 integers

Declaring Arrays Some other examples of array declarations: bool[] flags; // just declaring it flags = new bool[20]; // just creating it char[] codes = new char[1750]; // doing both float[] prices = new float[500]; // doing both

Array Storage Array data storage for ten doubles:

The Length of an Array You can find its size using the Length attribute: arrayRefVar.Length For example, for the array from the last slide myList.Length returns 10

System.Collections Contains interfaces and classes that define various collections of objects, such as different types of lists, queues, bit arrays, hash tables and dictionaries. The list type collections assign an integer (the index) to each element stored. Indices of elements are 0 for the element at the beginning of the list, 1 for the next element, and so on. The list type collections permit duplicate elements, which are distinguished by their position in the list.

ArrayList System.Collections.ArrayList Can hold any data type: (hybrid) Internally: array object Automatic resizing Not type safe: casting  errors detected only at runtime Boxing/unboxing: extra-level of indirection  affects performance Not homogeneous

ArrayList Example: ArrayList ls = new ArrayList(); // instantiates ls object ls.Add(3); // adds 3 to the end of ls ls.Add("Hi"); // adds "Hi" to the end of ls ls.Insert(1,7); // adds 7 after 3 and before "Hi" in ls foreach (Object x in ls) { Console.Write("\t" + x); } // prints: 3 7 Hi Console.WriteLine(); ls[0] = 12; // replaces first element with 12 for (int i = 0; i < ls.Count; ++i) Console.Write("\t" + ls[i]); } // prints: 12 7 Hi

List System.Collections.Generic.List Automatic resizing homogeneous Array Mostly has the same methods as ArrayList Unlike with ArrayList, the List class is type-safe through the use of required Generics

What are Generics? Generics give a class or a method the capability to parameterize types. This means Generics gives you the ability to assign a specific type of data that a particular class or method will deal with in a particular instance. The Generic data type is assigned when the class is defined, and it is given a concrete value when the class is instantiated to an object that will use the class to handle that particular data type. The parameter identifying the Generic type is placed between angle brackets < … >

Generics Example of defining a class called Widget to use a Generic type: public class Widget<E> { private E someField; public Widget(E initField) { someField = initField; } public void ShowWidget() { Console.WriteLine(“The Widget is: {0}”, someField); } Examples of creating a Widget object with a Generic type: Widget<int> intWidget = new Widget<int>(1001); intWidget.ShowWidget(); Widget<DateTime> dateWidget = new Widget<DateTime>(DateTime.Now); dateWidget.ShowWidget();

Data Types assigned using Generics Type-safe collections Reusability Example: List<String> studentNames = new List<String>(); studentNames.Add("John"); … String name = studentNames[3]; studentNames[2] = "Mike";

List Example: List<string> ls = new List<string>(); // instantiates ls object ls.Add("Joe"); // adds "Joe " to the end of ls ls.Add("Mary"); // adds "Mary" to the end of ls ls.Insert(1, "Jane"); // adds "Jane" after "Joe" before “Mary" foreach (string x in ls) { Console.Write("\t" + x); } // prints: Joe Jane Mary Console.WriteLine(); ls[0] = "Fred"; // replaces first element with "Fred" for (int i = 0; i < ls.Count; ++i) Console.Write("\t" + ls[i]); } // prints: Fred Jane Mary Console.ReadKey();

Common ArrayList and List Methods Add() Insert() Remove() Clear() Contains() IndexOf() LastIndexOf() BinarySearch() Sort()