Arrays I Savitch Chapter 6.1: Introduction to Arrays.

Slides:



Advertisements
Similar presentations
Chapter 8: Arrays.
Advertisements

Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
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.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Arrays Chapter 6 Chapter 6.
CS102--Object Oriented Programming Lecture 5: – Arrays – Sorting: Selection Sort Copyright © 2008 Xiaoyan Li.
Arrays CS Feb Announcements Exam 1 Grades on Blackboard Project 2 scores: end of Class Project 4, due date:20 th Feb –Snakes & Ladders Game.
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Lecture 12 Instructor: Craig Duckett ARRAYS. Announcements Assignment 3 Assignment 3 Revision Assignment 4 (and Final Exam) GRADED! RETURNED! Woot! NEXT.
One Dimensional Array. Introduction to Arrays Primitive variables are designed to hold only one value at a time. Arrays allow us to create a collection.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 10 l Array Basics l Arrays in Classes and Methods l Programming.
Chapter 61 Arrays Chapter 6. 2 Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define.
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.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Chapter 10. Arrays Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Computer Programming with JAVA.
JAVA Array 8-1 Outline  Extra material  Array of Objects  enhanced-for Loop  Class Array  Passing Arrays as Arguments to Methods  Returning Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
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.
Catie Welsh March 28,  Lab 7 due Friday, April 1st, 1pm 2.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
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.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Introduction to Collections Arrays. Collections Collections allow us to treat a group of values as one collective entity. The array is a collection of.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming.
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
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.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
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.
8-1 Chapter-7 Part1 Introduction to Arrays –Array Types –Creating/Declaring Arrays –Initializing Arrays –Processing Array Contents –Passing Arrays as Arguments.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
BIT115: Introduction to Programming
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Comp 248 Introduction to Programming Chapter 6 Arrays Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
COMP 110 Arrays Luv Kohli November 5, 2008 MWF 2-2:50 pm Sitterson 014.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Array in C# Array in C# RIHS Arshad Khan
Chapter 7 Part 1 Edited by JJ Shepherd
Something about Java Introduction to Problem Solving and Programming 1.
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
Announcements Lab 6 was due today Lab 7 assigned this Friday
CISC181 Introduction to Computer Science Dr
Dr. Sampath Jayarathna Cal Poly Pomona
Arrays in Java.
How do you do the following?
Presentation transcript:

Arrays I Savitch Chapter 6.1: Introduction to Arrays

Finding the Average int count; double sum; Scanner keyboard = new Scanner(System.in); System.out.println(“enter 7 integers”); sum = 0 for (count=0; count<7; count++) sum = sum + keyboard.nextDouble( ); System.out.println(“Average is” + sum/7);

Introduction to Arrays To calculate the average of a series of numbers we need just one variable to store the value of the numbers. However to calculate –the average, and also –the number of items below the average All the numbers must be stored in separate variables

Introduction to Arrays, cont. Arrays satisfy this need. An array is an ordered collection of variables of the same type. An array is also a class with –predefined methods and –instance variables

Array Details, cont. The type of the elements is called the base type. The base type of an array can be any type (i.e. primitive or class type). The number of elements is the length or size of the array.

Creating and Accessing Arrays example double[] temperature = new double[7]; is like declaring seven variables of type double, named temperature[0], temperature[1], temperature[2], temperature[3], temperature[4], temperature[5], temperature[6].

Creating and Accessing Arrays, cont. These variables can be used just like any other variables of type double. examples temperature[3] = 32.0; temperature[6] = temperature[3] + 5; System.out.println(temperature[6]); temperature[index+1] = 66.5; These variables are called indexed variables, elements, or subscripted variables.

Array Declaration Syntax syntax for creating an array Base_Type[] Array_Name =new Base_Type[Length]; example int[] pressure = new int[100]; or int[] pressure; pressure = new int[100];

Array Terminology

Brackets [] Brackets [] serve three purposes: –creating the type name example: int[] pressure; –creating the new array pressure = new int[100]; –naming an indexed variable of the array pressure[3] = keyboard.nextInt();

Classes, Objects, and Methods In some ways array resembles a class type (like String). String str; int[] pressure; Recall that class types have method For example the string class has the method charAt(N) This can be used by any instance of that class. str.charAt(5)

Instance Variables Apart from methods, classes can also have variables. These variables belong to each instance of the class. Hence they are called instance variables.

The length Instance Variable An array has only one public instance variable, namely length. The length variable stores the number of elements the array can hold. Using Array_Name.length typically produces clearer code than using an integer literal.

Indices and length The indices of an array start with 0 and end with Array_Name.length-1. When a for loop is used to step through an array, the loop control variable should start at 0 and end at length-1. example for (lcv = 0; lcv < temperature.length; lcv++)

Array Index Out of Bounds Every index must evaluate to an integer which is not less than 0 and not greater than Array_Name.length-1. Otherwise, the index is said to be out of bounds or invalid. An out-of-bounds index will produce a run- time error.

Incomplete Array Processing Loops fail to process an entire array correctly when they –begin with an index other than 0 –end with an index other than length-1. Examples for (i = 1; i < oops.length-1; index++) for (i = 1; i <= oops.length; index++) for (i = 0; i <= oops.length; index++) for (i = 0; i < oops.length-1; index++)

Initializing Arrays An array can be initialized at the time it is declared using an initialiser list. example double[] reading = {3,3, 15.8, 9.7}; –The size of the array is determined by the number of values in the initializer list.

Initializing Arrays, cont. Uninitialized array elements are set to the default value of the base type. The default value of the base type integer is 0. However, it’s better to use either an initializer list or a for loop. int[] count = new int[100]; for (int i = 0, i < count.length, i++) a[i] = 0;

Class Exercise class Exercise1 { public static void main ( String[] args ) { int[] val = {0, 1, 2, 3}; sum =.... System.out.println("Sum of all numbers = "); System.out.println(sum); } Complete the assignment statement so that it computes the sum of the numbers in the array.