Sections 10.1 – 10.4 Introduction to Arrays

Slides:



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

Chapter 8: Arrays.
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.
Chapter 10 Introduction to Arrays
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Chapter 9 Introduction to Arrays
CS0007: Introduction to Computer Programming Introduction to Arrays.
Java Unit 9: Arrays Declaring and Processing Arrays.
© 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.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
A First Book of ANSI C Fourth Edition
Arrays Mr. Smith AP Computer Science A.
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.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
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.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter Arrays, Timers, and More 8.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Comp 248 Introduction to Programming Chapter 6 Arrays Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Chapter 9 Introduction to Arrays Fundamentals of Java.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
1 Sections 3.1 – 3.2a Basic Syntax and Semantics Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Lecture 5 array declaration and instantiation array reference
Arrays of Objects October 9, 2006 ComS 207: Programming I (in Java)
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Lesson 8: Introduction To Arrays
Chapter 6: Using Arrays.
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Array, Strings and Vectors
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Section 11.1 Class Variables and Methods
JavaScript: Functions.
Arrays ICS 111: Introduction to Computer Science I
7 Arrays.
Lesson 9: Introduction To Arrays (Updated for Java 1
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
EKT150 : Computer Programming
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
int [] scores = new int [10];
Arrays in Java What, why and how Copyright Curt Hill.
Arrays .
int [] scores = new int [10];
CISC181 Introduction to Computer Science Dr
7 Arrays.
JavaScript: Arrays.
Java: Variables, Input and Arrays
Arrays of Objects October 8, 2007 ComS 207: Programming I (in Java)
Arrays.
Presentation transcript:

Sections 10.1 – 10.4 Introduction to Arrays Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne

Introduction An array is a data structure that consists of an ordered collection of similar items. An array has a single name. The items in an array are referred to in terms of their position in the array. Arrays are used to manipulate multiple values. 2 2

Conceptual Overview The items in an array are called elements. All of the elements need to be of the same type. The type can be any primitive or reference type. The length of an array is measured by the number of elements. The first element is element[0], the second is element[1], etc. An item’s position with an array is its index or subscript. 3 3

Conceptual Overview (continued) Three arrays, each containing five elements 4 4

Simple Array Manipulations The mechanics of manipulating arrays are fairly straightforward. First, declare and instantiate the array. <array name>[<index>] Index must be between 0 and the length minus 1. The subscript operator ([ ]) has the same precedence as the method selector (.). 5 5

Simple Array Manipulations (continued) The JVM checks the values of subscripts before using them. Throws an exception if they are out of bounds (less than 0 or greater than array length minus 1). The detection of a range-bound error is similar to the JVM’s behavior when a program attempts to divide by 0. An array’s length is stored in the public instance variable length. 6 6

Looping Through Arrays Traversal: a loop that iterates through an array one element at a time. Count the Occurrences: 7 7

Looping Through Arrays (continued) Other examples: Sum the elements Determine presence of absence of a number Determine first location To work with arrays of any size, use the length instance variable in the loop. 8 8

Declaring Arrays Example: declaring an array of 500 integers. Arrays are objects and must be instantiated before using. 9 9

Declaring Arrays (continued) Array variables are null before they are assigned array objects. Failure to assign an array object can result in a null pointer exception. Two variables can refer to the same array. To have two variables refer to two separate arrays that have the same values, copy all of the elements from one array to the other. 10 10

Declaring Arrays (continued) Two variables can refer to the same array object 11 11

Declaring Arrays (continued) Because arrays are objects, Java’s garbage collector sweeps them away when they are no longer referenced. Arrays can be declared, instantiated and initialized in one step. The list of numbers between the braces is called an initializer list. 12 12

Declaring Arrays (continued) Arrays can be formed from any collections of similar items. Booleans, doubles, characters, strings, and students. Once an array is instantiated, its size cannot be changed, so make sure the array is large enough from the outset. 13 13