Introduction to Programming Lecture 5

Slides:



Advertisements
Similar presentations
CS 141 Computer Programming 1 1 Arrays. Outline  Introduction  Arrays  Declaring Arrays  Examples Using Arrays  Sorting Arrays  Multiple-Subscripted.
Advertisements

EC-111 Algorithms & Computing Lecture #7 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Introduction to arrays
Introduction to Programming Lecture 39. Copy Constructor.
Arrays. What is an Array? An array is a way to structure multiple pieces of data of the same type and have them readily available for multiple operations.
1 Visual Basic Programming II Lecture 3 MIS233 Instructor – Larry Langellier.
 “Regular” variable ◦ Holds a single value ◦ For example Dim Student1 as String Dim Student2 as String Dim Student3 as String … Dim Studentn as String.
Alice in Action with Java
Chapter 7: Working with Arrays
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
ArrayList, Multidimensional Arrays
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
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.
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
6-1 Chapter 6 Working with Arrays in VB.NET. 6-2 Learning Objectives Understand the use of list and table arrays in VB.NET projects and the difference.
Arrays1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
Introduction to Programming Lecture 2 Msury Mahunnah, Department of Informatics, Tallinn University of Technology.
CN2180 Chapter 4 Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Chapter 8: Understanding Collections Textbook: Chapter 4.
Arrays 1.
Introduction to Computing Dr. Nadeem A Khan. Lecture 24.
Arrays and Collections
CMSC 202 ArrayList Aug 9, 2007.
Introduction to Programming Lecture 2
PROGRAMMING ARRAYS.
Sixth Lecture ArrayList Abstract Class and Interface
VBA - Excel VBA is Visual Basic for Applications
VBA - Excel VBA is Visual Basic for Applications
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
IS 350 Arrays.
Array, Strings and Vectors
Data Structure and Algorithms
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Data Structures Data Structure is a way of collecting and organising data in such a way that we can perform operations on these data in an effective.
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.
2. Understanding VB Variables
JavaScript: Functions.
Objects First with Java CITS1001
CMSC 341 Lecture 5 Stacks, Queues
EKT150 : Computer Programming
CMSC 202 ArrayList Aug 9, 2007.
ArrayLists.
Introduction To Programming Information Technology , 1’st Semester
Object Oriented Programming in java
JavaScript Arrays.
Classes and Objects.
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
CMSC 202 ArrayList Aug 9, 2007.
MSIS 655 Advanced Business Applications Programming
Arrays.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
int [] scores = new int [10];
Managing Collections of Data
Arrays Week 2.
CIS16 Application Development and Programming using Visual Basic.net
Introduction to Data Structure
JavaScript: Arrays.
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
Submitted By : Veenu Saini Lecturer (IT)
Data Structures & Algorithms
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
CS149D Elements of Computer Science
Programming Fundamental
Arrays © 2014 Goodrich, Tamassia, Goldwasser Arrays Vectors
Arrays Prepared By Paritosh Srivastava PGT (CS) KV NHPC Banbasa.
Lecture Set 9 Arrays, Collections, and Repetition
Presentation transcript:

Introduction to Programming Lecture 5 Msury Mahunnah, Department of Informatics, Tallinn University of Technology

Storing set of data Arrays ArrayLists

Variable vs Array Variable consists of a cell to keep (store) the value, provided with the name Array consists of cells to keep (store) the values, provided with the name and the indexes.

Why? Let’s set up an assignment to find maximum salary among the 5 employee. Dim salary1%, salary2%, salary3%, salary4%, salary5% or Dim salaries%(4) Example on the whiteboard!

Main properties 1. Name For names, same rules as variables 2. Dimensions It will determine how the items are organised? 3. Boundaries of indexes Boundaries for every dimension 4. Dynamism Static memory (Dim) and Dynamic memory (ReDim) Dim – allocate memory before the work of a program Redim – allocate memory during the program work 5. Type Integer, String, Date, Boolean, ... Every property is determined directly or indirecty with an array declaration!!!

Main properties - example Dim A(5) As Integer Name – A Dimesions – 1 (how many boundaries do we have?) Boundaries of indexes – (for first dimension) 0 to 5 Dynamism – static (Dim) Type – Integer

Main properties - example Dim B( , ) As String Redim B(3, 7) Name – B Dimesions – 2 (how many boundaries do we have?) Boundaries of indexes for first dimension – 0 to 3 for second dimension – 0 to 7 Dynamism – dynamical (ReDim) Type – String

Declaring a dynamic array At first declare it as usual with Dim statement (or Public or Private), but do not specify boundaries for its dimensions: Dim DynArray() As Integer Later in the program, when you know how many elements you want to store in the array, use the ReDim statement: ReDim DynArray(UserCount)

Declaring a dynamic array Dim A( , , ) As Integer ReDim A(3, 7, 4) How to know some essentials features? Dimensions in array A.Rank - > 3 Total elements in array A.Length - > 160 Elements in first dimension A.GetLength(0) -> 4 Elements in second dimension A.GetLength(1) -> 8 Elements in third dimension A.GetLength(2) -> 5 Last index in the first dimension A.GetUpperBound(0) - > 3 Last index in the second dimension A.GetUpperBound(1) - > 7 A.GetUpperBound(2) - > 4

Initializing Arrays General constructor: Dim arrayname() As type = {entry0, entry1, ... …, entryN} Two ways to do this (real example): Dim Names() As String = {“Peter”, “Kathy”} Dim Names(1) As String Names(0)=“Peter” Names(1)=“Kathy”

Initializing Arrays – real example Dim Matrix ( , ) As Integer = {{10, 20, 30}, {40, 50, 60}, {70, 80, 90}} Or {{10, 20, 30}, _ {40, 50, 60}, _ {70, 80, 90}} 1 2 10 20 30 40 50 60 70 80 90

Repeated ReDim declaration Each time you execute the ReDim statement, all the values currently stored in the array are lost!!! Q: But If I need to do it for enlarging an array? A: Use keyword Preserve

Preserve example To enlarge Vector: Dim Vector() As Byte Redim Vector(7) To enlarge Vector: ReDim Preserve Vector (12) Or ReDim Preserve (Vector.GetUpperBound(0)+5)

Initializing array with random number Const max%=3, a%=1, b%=9 ‘random numbers from 1 to 9 Dim V%(max), i% ‘Before (for cycle) -> V={0, 0, 0, 0} For i=0 to max V(i)=Int(Rnd()*(b-a+1)-a) Next i ‘After (for cycle) - > V = {6, 2, 5, 8}

An additional possibility to store data ... The ArrayList Collection ... ... is a dynamical structure Advantages: Adding new items simply Removing items (by index and also by value) Storing there different type of values More convinient - grow automatically as you add elements

The ArrayList Collection Declaration: Dim aList As New ArrayList Adding items: Dim words() As String = {"Just", "a", "few", "words"} aList.Add(words): aList.Add("Hello") aList.Add(777): aList.Add(True)

The ArrayList Collection First item index (sequence number) is 0 Removing items: By value: aList.Remove(“Hello”) By index: aList.RemoveAt(2) All items at the same time: aList.Clear()