Arrays.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
CS100A, Fall 1997, Lecture 111 CS100A, Fall 1997 Lecture 11, Tuesday, 7 October Introduction to Arrays Concepts: Array declaration and allocation Subscripting.
Programming with Collections Collections in Java Using Arrays Week 9.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Arrays.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
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.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
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.
Introduction to Computing Concepts Note Set 21. Arrays Declaring Initializing Accessing Using with Loops Sending to methods.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
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];
Chapter 9 Introduction to Arrays Fundamentals of Java.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Windows Programming Lecture 03. Pointers and Arrays.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
(Java Looping and Conditional Statements). Flow of control Sequential Executes instructions in order Method Calls Transfer control to the methods, then.
Processing Sequences of Elements
Array in C# Array in C# RIHS Arshad Khan
Elementary Programming
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Introduction to Computer Science / Procedural – 67130
Introduction to programming in java
Chapter 7 Part 1 Edited by JJ Shepherd
Lecture-5 Arrays.
Arrays Part-1 Armen Keshishian.
C++ Data Types Simple Structured Address Integral Floating
CS 1430: Programming in C++.
DAYS OF THE WEEK.
7 Arrays.
Sunday Monday Tuesday Wednesday Sunday Monday Tuesday Wednesday
Lists in Python.
Arrays.
CS-161 Computer Programming Lecture 14: Arrays I
Arrays November 8, 2017.
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
Arrays in Java What, why and how Copyright Curt Hill.
Computer Science Review
INC 161 , CPE 100 Computer Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
Starting Out with Programming Logic & Design
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
Arrays ICS2O.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays Week 2.
Announcements Lab 6 was due today Lab 7 assigned this Friday
CISC181 Introduction to Computer Science Dr
Why did the programmer quit his job?
Chapter 9: Data Structures: Arrays
JavaScript: Arrays.
Arrays in Java.
Programming Logic and Design Fifth Edition, Comprehensive
Standard Version of Starting Out with C++, 4th Edition
Time 1.
Contact
Arrays and Pointers CSE 2031 Fall May 2019.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
2011年 5月 2011年 6月 2011年 7月 2011年 8月 Sunday Monday Tuesday Wednesday
Arrays and Pointers CSE 2031 Fall July 2019.

Interfaces, Enumerations, Boxing, and Unboxing
Week 7 - Monday CS 121.
Presentation transcript:

Arrays

Array Basics Declare variables to store and add 3 blood pressures: int bp1, bp2, bp3; bp1 = myScanner.nextInt(); bp2 = myScanner.nextInt(); bp3 = myScanner.nextInt(); int total = bp1 + bp2 + bp3; What if you wanted to store and total 1000 blood pressures? Declare an array of 1000 int values.

Array Basics An array is a collection of individual variables that are usually related. Each individual variable called an element. Arrays can be composed of any data type, but all of the elements are the same type. An array is given a single name, and stored in consecutive memory locations.

Array Basics Each element of an array is accessed by the array name followed by an index in brackets []. The index is also known as subscript and indicates the position of the element. In Java, the first array element always has subscript 0, the second element has subscript 1, and so on ...

Array Declaration Example int myArray[] = new int[5]; Or int[] myArray = new int[5]; This declaration creates an array of five elements, all of which are integers. Consecutive memory locations are allocated for the five elements of the array. The base address of an array is its beginning address in memory.

Array Declaration (Cont ..) Index/Subscript int scores[] = new int[5]; score[1] score[3] score[4] score[0] score[2] Number of elements in the array

Array Initialization Elements can be initialized at declaration: int scores[] = {78,84,62,93,97}; 84 93 97 78 62 score[1] score[3] score[4] score[0] score[2]

0-based indexing Arrays are 0-based, meaning they begin at 0 rather than 1. Example: int scores[] = {99, 85, 43, 72, 82 }; Position 1 2 3 4 5 Index Value 99 85 43 72 82

Assigning values to individual array elements int scores[] = new int[5]; int i = 4; score[2] = 98; score[3] = 83; score[i] = 79; score[0] = score[3] + 5; 83 79 88 98 score[1] score[3] score[4] score[0] score[2]

Example public static void main(String[] args) { final int NUM_OF_STUDENTS = 5; int score[NUM_OF_STUDENTS] = {78,84,62,93,97}; for (int count = 0; count < NUM_OF_STUDENTS; count++) System.out.println("Score# “ + score[count]); } Score#0 78 Score#1 84 Score#2 62 Score#3 93 Score#4 97

Tips Array index has to be an integer type. It is the programmer’s responsibility to make sure that an array index does not go out of bounds. The index must be within the range 0 through the declared array size minus one. Using an index value outside this range causes the program to access memory locations outside the array => Program crashes! The index value determines which memory location is used

Common Uses of Arrays Sometimes when we want to encode a series of values, we need a series of if statements. With arrays we can “index” the values so it moves quicker This technique is widely used for its speed/efficiency and is known as hashing Ex. public string translate( int dayOfWeek ) { if( dayOfWeek < 0 || dayOfWeek > 6)//error return “error: day out of range”;//error else String[] daysOfWeek = {“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”}; return daysOfWeek[dayOfWeek]; }

Why Hashing Alternative is slow: public String translate(int dayOfWeek) { if( dayOfWeek == 0 ) return “Monday”; else if(dayOfWeek == 1) return “Tuesday”; else if(dayOfWeek == 2) return “Wednesday”; else if(dayOfWeek == 3) return “Thursday”; else if(dayOfWeek == 4) return “Friday”; else if(dayOfWeek == 5) return “Saturday”; else if(dayOfWeek == 6) return “Sunday”; else return “Error”; }

Common Uses of Arrays Sometimes when we want to encode a series of values, we need a series of if statements. With arrays we can “index” the values so it moves quicker Ex. public string translate( int dayOfWeek ) { if( dayOfWeek < 0 || dayOfWeek > 6)//error return “error: day out of range”;//error else String daysOfWeek[7] = {“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”}; return daysOfWeek[dayOfWeek]; }

Arrays in functions Arrays are ALWAYS passed by reference If you change the array, you will change the values associated with it after the functions returns. Ex. void fill( int array[], int size ) { for( int n = 0; n < size; n++ ) array[ n ] = 0;//set all values to 0 }