Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 Arrays Chapter 6.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Click to edit Master title style. Click to edit Master subtitle style.
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
Kernighan/Ritchie: Kelley/Pohl:
Chapter 10 Introduction to Arrays
Lecture 05 - Arrays. Introduction useful and powerful aggregate data structure Arrays allow us to store arbitrary sized sequences of primitive values.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Chapter 9 Introduction to Arrays
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
C Static Arrays Pepper. What is an array? Memory locations – same type – next to each other (contiguous) – Same name – Indexed by position number of type.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
Java Arrays [Lists] in 10 Minutes. Declaring an array (and its types) int[] myIntArray; double[] myDoubleArray; String[] myStrings; //What’s the pattern?
1 Chapter 8 Multi-Dimensional Arrays. 2 1-Dimentional and 2-Dimentional Arrays In the previous chapter we used 1-dimensional arrays to model linear collections.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
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.
Chapter 8: Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
1 Arrays An array is a collection of data values, all of which have the same type. The size of the array is fixed at creation. To refer to specific values.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 ArrayLists Section 9.9.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples:
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Click to edit Master title style Click to edit Master text styles –Second level Third level –Fourth level »Fifth level 1 Fundamentals of Programming Most.
Java Programming Language Lecture27- An Introduction.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Headline sample style Intro sample style Click to edit Master text styles –Second level Third level –Fourth level o Fifth level.
Chapter VII: Arrays.
Click to Add Title Click to Add Subtitle.
Click to Add Title Click to Add Subtitle.
Using local variable without initialization is an error.
Peer Instruction 6 Java Arrays.
Click to edit Master text styles
Click to Add Title Click to Add Subtitle.
Click to Add Title Click to Add Subtitle.
Author names here Author association names here
Click to edit Master text styles
TITLE SLIDE GOES HERE Optional subhead.
CS2011 Introduction to Programming I Arrays (I)
Click to edit Master text styles
Slide Title Edit Master text styles Second level Third level
ОПШТЕСТВО ТЕМА: МЕСТОТО ВО КОЕ ЖИВЕАМ Скопје
Author names here Author associations here
Author names here Author associations here
Click to edit Master text styles
Author names here Author associations here
Click to edit Master text styles
Presentation transcript:

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 Arrays Chapter 6

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 2 Each variable only holds one item if > 1 item wanted, need an array array that holds a word arrays hold elements all of the same type char[ ] word = new char[4]; holds 4 elements of type char Arrays 0132 word

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 3 char[ ] word = new char[4]; two parts to an array: 1. index -- integer element – type inside array 'h''e' 0132 'h' 0132 word[1] = 'e'; 'h''e''o''r' 0132 'h''e''o' 0132 word[3] = 'o'; word[2] = 'r'; word[0] = 'h'; two parts to an array: 1. index -- integer two parts to an array: 1. element – any type inside

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 4 Can use variables for index OR elements int i=3; char newc = 'd'; word[i] = newc; can find length word.length // is 4 largest index is always length – 1 word[4] is RUN time error Array manipulation 'h''e''o''r' 0132 'h''e''d''r' 0132

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 5 arrays and new char[ ] word; creates word that is of type char array that points to nothing word = new char[4]; creates array of 4 elements initialized to \u0000 (Java always initializes instance variables to 0)

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 6 Myarray example public class Myarray { private Circle[ ] circles; private double[ ] area; // other stuff in the class }

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 7 Myarray gets elements allocated Create an object circles = new Circle[4]; area = new double[4];

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 8 createcircles( ) circles[0] = new Circle();

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 9 array creation summary char[ ] word; creates a space named word that contains null word = new char [4]; allocates 4 chars, initialized, word points to them classes: Circle[ ] mycircles; same as word mycircles = new Circle[4]; allocates 4 spaces that contain null mycircles[0] = new Circle( ); creates an actual circle new [ ] for arrays, NOT ( )

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 10 array creation summary use new to create an array: –char[ ] word = new char [4]; –Circle[ ] circles = new Circle[4]; if the type of element is an object, each item must be new-ed –word[1] = ‘r’; –circles[1] = new Circle( );

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 11 Repetition in arrays arrays often do the same thing (e.g., for each Circle in array, create a Circle) for (int i=0; i<circles.length; i++) circles[i] = new Circle( ); memorize this line

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 12 More on Loops Java 5 for loop/for each/expanded for for (Circle c : circles)c.changeColor(“red”); No explicit i; cannot reference individual elements (e.g., a[i] = d; won't work) Read ‘for (’ as “for each” and ‘:’ as “in the array” Type of element name of array variable c is name for each element in loop, NOT each index

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 13 Practice 1.Write code to declare a 4 character word array, then write a loop to initialize chars in word to be 'A' 2.Write code to declare a 26 character array, then write a loop to initialize chars in word to be ABCDEFGHIJKLMNOPQRSTUVWXYZ (do this in a loop). Hint: use a separate variable for the element value (start with 'A') ++ and += works for chars 3.Declare an int array with 1000 integers and write a loop to put the value of the index into the element (e.g., intarray[3] should have the value 3)

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 14 Array Specifics: passing as a parameter Passing arrays as parameters int[ ] grades = new grades[20]; int sum = sumgrades(grades); double average = 0.0; if (grades.length > 0) average = sum * 1.0 / grades.length; int index; index = findmatches(grades[0], grades, 1, grades.length); if (index == -1) System.out.println("Match not found"); To call a method with an array as a parameter, use just the name of the array. NO [ ] To pass an element of an array, access the element as you always would

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 15 Write sumgrades Call is: int sum = sumgrades(grades);

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 16 Arrays as Parameters public int sumgrades(int[ ] myarray) { int sum = 0; for (int element : myarray) sum += element; return sum; } or public int sumgrades(int[ ] myarray) { int sum = 0; for (int i=0;i<myarray.length;i++) sum = sum + myarray[i]; return sum; } Declare array parameters using []s just like when you declare them as variables

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 17 Write findmatches Call is: index = findmatches(grades[0], grades, 1, grades.length);

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 18 Searching Arrays public int findmatches(int item, int[ ] anarray, int start, int finish) { for (int i=start;i<finish;i++) if (item == anarray[i]) return i; return -1; // if done with loop, not found }

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 19 Initializing and Copying arrays Can initialize arrays –int[ ] myarrray = {3, 5, 7, 9}; Copying arrays –int yourarray[ ] = new int[myarray.length]; –yourarray = myarray; –copies reference, not values. –must copy individual elements: for (int i=0;i<myarray.length;i++) yourarray[i] = myarray[i]; 3579 myarray

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 20 Two-dimensional Arrays int students = 5; int nbrgrades = 3; int[ ][ ] grades = new int[students][nbrgrades]; [0] [1] [2] [4] [3] [1][0][2] Student Number Grades

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 21 Two-dimensional Arrays int students = 5; int nbrgrades = 3; int[ ][ ] grades = new int[students][nbrgrades]; for (int i=0;i<grades.length;i++) { System.out.print("Student number " + i + " "); for (int j=0;j<grades[i].length;j++) System.out.print(grades[i][j] + " "); System.out.println(); }

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 22 Write a method that returns the average of all grades int students = 5; int nbrgrades = 3; int grades = new int[students][nbrgrades]; header is: public double average(int[ ] [ ] myarray)

Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 23 More array practice Write a method that finds the largest element in an array of integers Write a method that will print all of the scores greater than the average