Ch 7 Arrays – The 1 st Data Structure : How Store Data ARRAYS: ARRAYS: Table of same type elements (Objects or Primitives). Table of same type elements.

Slides:



Advertisements
Similar presentations
Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Advertisements

Introduction to C Programming
Chapter 8: Arrays.
Selection Sort. Selection Sort Algorithm (ascending) 1.Find smallest element (of remaining elements). 2.Swap smallest element with current element (starting.
1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
Review BP Dari slide pak cahyo pertemuan 7 dan pertemuan 2 dengan sedikit modifikasi.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
1 Various Methods of Populating Arrays Randomly generated integers.
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.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Introduction to arrays Data in economy size packages.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 14, 2005.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
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.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
©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.
ECE122 L13: Arrays of Objects March 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 13 Arrays of Objects.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Java vs. You.
BUILDING JAVA PROGRAMS CHAPTER 7 Array Algorithms.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
Arrays.
COMP 14 Introduction to Programming Miguel A. Otaduy June 1, 2004.
One Dimensional Arrays. Declaring references to array objects How would you declare a variable somearray that is an array of ints? int[] somearray;
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Ch4 Data Types Every value has a type associated with it. The computer needs to know how much memory to allocate. Every value is either a primitive type.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is.
Programming Fundamentals I (COSC-1336), Lecture 8 (prepared after Chapter 7 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 8.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
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.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
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.
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
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.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Grouping objects Arrays. 2 Fixed-size collections The maximum collection size may be pre-determined with an upper limit Array is an fixed-size collection.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Chapter 6 Arrays 1 Fall 2012 CS2302: Programming Principles.
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];
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
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. Example Write a program to keep track of all students’ scores on quiz 1. Need a list of everyone’s score Declare 14 double variables? What about.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
Introduction to programming in java Lecture 21 Arrays – Part 1.
Lecture 7: Arrays Michael Hsu CSULA 3 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.
Chapter 5 Functions DDC 2133 Programming II.
More Object Oriented Programming
Chapter 6 Arrays Solution Opening Problem
METHODS (FUNCTIONS) By: Lohani Adeeb khan.
Defining methods and more arrays
Topics discussed in this section:
Chapter 6 Methods.
Chapter 6 Arrays.
© A+ Computer Science - Classes And Objects © A+ Computer Science -
Chapter 6 Arrays.
Presentation transcript:

Ch 7 Arrays – The 1 st Data Structure : How Store Data ARRAYS: ARRAYS: Table of same type elements (Objects or Primitives). Table of same type elements (Objects or Primitives). Static - Physical size cannot change (public field length) vs Logical size. Static - Physical size cannot change (public field length) vs Logical size. Access of an array component is through [index] notation. Access of an array component is through [index] notation. Positions in an array are indexed from 0 to length –1. Positions in an array are indexed from 0 to length –1. EXAMPLES: EXAMPLES: int[ ] scores;// scores will reference a block of ints int[ ] scores;// scores will reference a block of ints scores = new int[5];// int[ ] scores = new int[5]; scores = new int[5];// int[ ] scores = new int[5]; double[ ] wages = new double[numEmployees]; double[ ] wages = new double[numEmployees]; BankAcct[ ] accounts = new BankAcct[MAX_ACCTS]; BankAcct[ ] accounts = new BankAcct[MAX_ACCTS]; FILLING AN ARRAY. Literal, Direct, or Assignment. FILLING AN ARRAY. Literal, Direct, or Assignment. Literally Assigning Values: Literally Assigning Values: int[ ] tests = new int[3];//test.length = 3 tests[0] = 90; tests[1] = 84; tests[2] = 93; Direct Assignment : If values are known. Direct Assignment : If values are known. int[ ] primes = {2,3,5,7}; int[ ] primes = {2,3,5,7}; Assignment : Write the code to input scores into an array list until -999 is read. Assume theres a helper method getNum( ) for input. Keep track of the logical size called mySize. Assignment : Write the code to input scores into an array list until -999 is read. Assume theres a helper method getNum( ) for input. Keep track of the logical size called mySize.

Code to Fill Array: Code to Fill Array: int mySize = 0; num = getNum( ); while ( num != -999 ) { list[mySize] = num; mySize++; num = getNum( ); } DISPLAYING VALUES: DISPLAYING VALUES: int[ ] list = {5, 3, 2, 9, 1}; int[ ] list = {5, 3, 2, 9, 1}; for( int i = 0; i < list.length ; i++) for( int i = 0; i < list.length ; i++) System.out.print(list[i] + ); System.out.print(list[i] + ); EXAMPLES – Trace the following by hand: EXAMPLES – Trace the following by hand: int[ ] list = new int[5]; int[ ] list = new int[5]; for(int a = 0; a < 5; a++) for(int a = 0; a < 5; a++) list[a] = 2*a;//List contains what? for(int a = 0; a < 5; a++) for(int a = 0; a < 5; a++) list[a] = (1+a) * (1+a) ; //List contains what ?

EXAMPLES Cont. – Trace the following by hand: EXAMPLES Cont. – Trace the following by hand: list[0]=1; list[0]=1; for(int a = 1; a < 5; a++) list[a] = 2*list[a-1]; //List contains what? displayArray(list); //TBD displayArray(list); //TBD Random num = new Random( ); Random num = new Random( ); for(int a = 0; a < 5; a++) list[a] = 1+num.nextInt(100); //List contains what? displayArray(list); displayArray(list); ASSUME EXAMPLES ARE INVOKED BY: displayArray(list); ASSUME EXAMPLES ARE INVOKED BY: displayArray(list); public void displayArray(int[ ] arr)// Array as a parameter public void displayArray(int[ ] arr)// Array as a parameter { for( int i = 0; i < arr.length ; i++) System.out.print(arr[i] + ); for( int i = 0; i < arr.length ; i++) System.out.print(arr[i] + ); } MISC: What do you think the following would do? MISC: What do you think the following would do? 1. System.out.print(list[list.length]); 2. System.out.print(list); HOMEWORK HOMEWORK 1. Trace the 4 examples above 2. Run the examples in BlueJ invoking displayArray(list) for each.