Arrays (review) CSE 2011 Winter 2011 31 May 2018.

Slides:



Advertisements
Similar presentations
Chapter 21 Implementing lists: array implementation.
Advertisements

Basic Algorithms on Arrays. Learning Objectives Arrays are useful for storing data in a linear structure We learn how to process data stored in an array.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Chapter 9 Introduction to Arrays
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Stacks - 2 Nour El-Kadri ITI Implementing 2 stacks in one array and we don’t mean two stacks growing in the same direction: but two stacks growing.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Chapter 8: Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
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.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Pointer Data Type and Pointer Variables III CS1201: Programming Language 2 By: Nouf Aljaffan Edited by : Nouf Almunyif.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Pointers and Arrays Dynamic Variables and Arrays.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Chapter 16: Linked Lists.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Lecture 10 Collections Richard Gesick.
CHP - 9 File Structures.
Data Structures I (CPCS-204)
Introduction to Linked Lists
Linked Lists Chapter 6 Section 6.4 – 6.6
Big-O notation Linked lists
Review Deleting an Element from a Linked List Deletion involves:
12 C Data Structures.
CS522 Advanced database Systems
Computer Programming BCT 1113
Array, Strings and Vectors
Arrays 2/4 By Pius Nyaanga.
Chapter 7 Part 1 Edited by JJ Shepherd
Data Structures Interview / VIVA Questions and Answers
Chapter 8 Arrays Objectives
Java Review: Reference Types
Introduction to Linked Lists
Dynamic Memory Allocation
Phil Tayco Slide version 1.0 Created Oct 16, 2017
Arrays and Linked Lists
Lecture 10 List Richard Gesick.
Chapter 8 Arrays Objectives
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.
Review of Arrays and Pointers
Pointer Data Type and Pointer Variables III
Object Oriented Programming in java
Arrays .
Tim Ehrlich Growing Arrays in C.
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
Linked Lists Chapter 4.
CS2011 Introduction to Programming I Arrays (I)
Data Structures (CS212D) Week # 2: Arrays.
Arrays Week 2.
Single-Dimensional Arrays chapter6
Introduction to Data Structure
Chapter 8 Arrays Objectives
Dynamic Memory.
Data Structures & Algorithms
Fundaments of Game Design
SPL – PS2 C++ Memory Handling.
Week 7 - Monday CS 121.
Problem Understanding
Presentation transcript:

Arrays (review) CSE 2011 Winter 2011 31 May 2018

Arrays A common programming task is to keep track of a group of related objects! Array – sequence of indexed components with the following properties:  array size is fixed at the time of array’s construction int[] numbers = new numbers [10];  array elements are placed contiguously in memory  address of any element can be calculated directly as its offset from the beginning of the array  consequently, array components can be efficiently inspected or updated in O(1) time, using their indices randomNumber = numbers[5]; numbers[2] = 100; Index = 0 1 2 3 4 5 6 7 Element at position 5 Array of length 8

Arrays (cont.) Arrays in Java – Properties For an array of length n, the index bounds are 0 to (n-1). (2) Java arrays are homogeneous - all array components must be of the same (object or primitive) type.  but, an array of an object type can contain objects of any respective subtype (3) An array is itself an object.  it is allocated dynamically by means of “new”, it is automatically deallocated when no longer referred to (4) when an array is first created, all values are initialized with  0 – for an array of int[] or double[] type  false – for a boolean[] array  null – for an array of objects Example 1 [ common error – uninitialized arrays ] int[] numbers; numbers[2] = 100;

Array is defined at the time of its ‘declaration’. Arrays (cont.) Arrays in Java – Properties (cont.) The length of any array (object) can be accessed through its instance variable ‘length’.  the cells of an array A are numbered: 0, 1, .., (A.length-1) !!! (6) ArrayIndexOutOfBoundsException - thrown at an attempt to index into array A using a number larger than (A.length-1).  helps Java avoid ‘buffer overflow attacks’ Example 2 [ declaring, defining and determining the size of an array ] int[] A={12, 24, 37, 53, 67} for (int i=0; i < A.length; i++) { … } Array is defined at the time of its ‘declaration’.

Arrays (cont.) Arrays in Java – Properties (cont.) (7) Since an array is an object, the name of the array is actually a reference (pointer) to the place in memory where the array is stored.  reference to an object holds the address of the actual object Example 3 [ arrays as objects ] int[] A={12, 24, 37, 53, 67}; int[] B=A; B[3]=5; A 12 24 37 53 67 B A 12 24 37 5 67 B http://www.bearcave.com/software/garbage.htm Java Foundations, Lewis, pp. 75 (about creating objects and references) Example 4 [ cloning an array ] int[] A={12, 24, 37, 53, 67}; int[] B=A.clone(); B[3]=5; A 12 24 37 53 67 B 12 24 37 53 67 A 12 24 37 53 67 B 12 24 37 5 67

Arrays (cont.) Arrays in Java: a few useful methods (java.util.Arrays) equals(A,B) – returns true if A and B have an equal number of elements and every corresponding pair of elements in the two arrays are equal fill(A,x) – store element x into every cell of array A sort(A) – sort the array A in the natural ordering of its elements http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html binarySearch([int] A, int key) – search the specified array of ints for the specified value using the binary search algorithm

Arrays (cont.) Example [ What does get printed out ?! ] … int[] A={12, 24, 37, 53, 67}; int[] B=A.clone(); if (A==B) System.out.println(“ Superman ”); if (A.equals(B)) System.out.println(“ Snow White ”);

Arrays (cont.) Example 4 [ 2D array in Java = array of arrays !!! ] int[][] nums = new int[5][4]; int[][] nums; nums = new int[5][]; for (int i=0; i<5; i++) { nums[i] = new int[4]; } Example 5 [ 2D array of objects in Java = an array of arrays of references !!! ] Square[][] board = new Square[2][3]; http://www.willamette.edu/~gorr/classes/cs231/lectures/notes.htm

Arrays (cont.) Arrays in General – Major Limitations! static data structure – size must be fixed at the time the program creates the array – once set, array size cannot be changed  if: number of entered items > declared array size  out of memory  fix 1: use array size > number of expected items  waste of memory  fix 2: increase array size to fit the number of items  extra time insertion / deletion in an array is time consuming – all the elements following the inserted element must be shifted appropriately Example 5 [ time complexity of “growing” an array ] if (numberOfItems > numbers.length) { int[] newNumbers = new int[2*numbers.length]; System.arraycopy(numbers, 0, newNumbers, 0, numbers.length); numbers = newNumbers; } Number of elements to be copied. Example from Computing Concepts with Java – pp. 537 http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#arraycopy(java.lang.Object,%20int,%20java.lang.Object,%20int,%20int) Starting position in source array. Starting position in destination array.

Next topic ... Linked lists (3.2, 3.3)