Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 9: Arrays; Revision Session.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Chapter 7: Arrays In this chapter, you will learn about
Searching for Data Relationship between searching and sorting Simple linear searching Linear searching of sorted data Searching for string or numeric data.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
Chapter 7: User-Defined Functions II
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
1 Lecture Today’s topic Arrays Reading for this Lecture: –Chaper 11.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 12: Arrays.
Chapter 8 Arrays and Strings
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Arrays.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Programming Arrays. Question Write a program that reads 3 numbers from the user and print them in ascending order. How many variables do we need to store.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
CS1101: Programming Methodology Aaron Tan.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
INFO Problem Solving and Programming Logic INFO Problem Solving and Programming Logic Arrays Sorting.
Lists in Python.
Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 12: Programming Project.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming.
1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
Static block can be used to check conditions before execution of main begin, Suppose we have developed an application which runs only on Windows operating.
BIT115: Introduction to Programming
An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
Chapter 7: User-Defined Functions II
Chapter 7 Part 1 Edited by JJ Shepherd
User-Defined Functions
Introduction To Programming Information Technology , 1’st Semester
Object Oriented Programming in java
Suggested self-checks: Section 7.11 #1-11
Java Programming Language
Arrays.
Presentation transcript:

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 9: Arrays; Revision Session

Revision of session 8 Java is distributed with a collection of ready-to-use classes, called the Core Class Libraries. A full list of the classes available can be found on the Java Core Class Libraries documentation page: To use external classes: import classname; String class contains many methods for manipulating Strings. Math class contains mathematical constants and operations. Many String and Math methods are static!

Session 9 - aims & objectives An introduction to arrays. Useful for storing large amounts of similarly structured data. Revision of Sessions 1-8.

Arrays – 1 These are like lists or a tables of data. Contains only one data type e.g. integers, Strings etc. Array size must be defined before you use it i.e. how many elements (or list items) do you want to have in it? Arrays start counting from 0 e.g. element 0, element 1, element 2 etc. This is called the index of the element. Each element in the array can be addressed by its index number.

Arrays – 2 To create an array, we need to specify the data type and the name of the array, and then use the new keyword in the usual way: int[] year = new int[3]; Square brackets signify that we are refering to an array. Can think of “int[]” as a bit like it’s own data type int[] = “an array of ints” Above, 3 is the number of elements in the array. Can refer to elements using square brackets again: year[0]=1980; year[1]=1990; year[2]=2000;

Arrays – 3 You can actually do either of the following: int[] year = new year[3]; int year[] = new year[3]; Both lines do the same thing. However, in the first line “int[]” stands out more clearly as an array of ints! We’ll try to stick to the syntax of the first line, but you might see either in general.

Arrays – 4 Here’s some typical array coding: int[] year = new year[3]; int[1]=2011; int[2]=2010; int[3]=2009; What will happen when you compile this code?

Arrays – 5 Use of arrays is quite limited. Can only store one data type. Need to know the max number of elements to store in advance. If this is inconvenient, we can use other approaches… We’ll see an example in Session 15.

Example code: Take a look at example codes ex9_01 and ex9_02, which demonstrate the use of arrays. You can try exercise 9.1 after today’s session, to practice working with arrays.

Revision task – Bubble sort! Task: Write a program which asks the user to enter a list of numbers, stores these numbers in an array, sorts them into ascending numerical order, and prints out the sorted list. There are many ways to sort a list of numbers. We will use the Bubblesort algorithm, which is easy to understand, but quite inefficient. The bubble sort works by comparing each item in the list with the item next to it, and swapping them if required. This process is repeated until it goes all the way through the list without swapping any items (i.e. all items are in the correct order).

Coming up in Session Constructors Useful methods for creating objects more quickly. The String class revisited Creating Strings using constructors. Note: Strings are a little bit like char[]’s, except Strings are objects!