Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.

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.
Chapter 7 – Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
©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.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
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.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
More Arrays Length, constants, and arrays of arrays By Greg Butler.
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 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
A First Book of ANSI C Fourth Edition
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
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.
Pemrograman Dasar Arrays PTIIK - UB. Arrays  An array is a container object that holds a fixed number of values of a single type.  The length of an.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
First Data Structure Definition A data structure is a data type whose components are smaller data structures and/or simple data types.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
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.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
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.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
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.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
 2005 Pearson Education, Inc. All rights reserved Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Objectives You should be able to describe: One-Dimensional Arrays
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Chapter 6 Arrays in C++ 2nd Semester King Saud University
CSC 142 Computer Science II
Chapter 8: Collections: Arrays
Introduction To Programming Information Technology , 1’st Semester
Object Oriented Programming in java
Arrays in Java.
Presentation transcript:

Chapter 8: Collections: Arrays

2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The Collections Framework: ArrayLists Two-Dimensional Arrays Common Programming Errors

3 One-Dimensional Arrays Lists of related values with the same data type –Stored using a single group name Array declaration example: double prices[]; prices = new double[6];

4 One-Dimensional Arrays (continued) Using the new operator: –Array elements are automatically initialized to: Zero for numerical built-in types False for Boolean built-in types Null for reference types

5

6 One-Dimensional Arrays (continued) Common programming practice: –Define the number of array items as a symbolic constant Element –Item in an array Index –Position of an item in an array –Also called a subscript

7 Accessing Elements in One- Dimensional Arrays grade[0]: –Refers to the first value stored in grade array –Read as “grade sub zero” –Can be used anywhere that scalar variables are valid The subscript contained within brackets need not be an integer constant –Any expression that evaluates to an integer may be used

8 Accessing Elements in One- Dimensional Arrays (continued) An important advantage of using integer expressions as subscripts: –Allows sequencing through an array using a loop Example of looping through an array: sum = 0; // initialize the sum to zero for (i = 0; i < NUMELS; i++) sum = sum + grade[i]; // add in a grade –i is used both as a counter in the for loop and as a subscript

9 Accessing Elements in One- Dimensional Arrays (continued) When accessing an array element: –Java checks the value of the index being used at run time –If the index exceeds the length of the array, Java will notify you of an ArrayIndexOutOfBounds exception

10

11 One-Dimensional Array Length The size of an array is automatically stored in a variable named length Looping using for loop and array length: sum = 0; // initialize the sum to zero for (i = 0; i < grade.length; i++) sum = sum + grade[i]; // add in a grade

12 Input and Output of Array Values Input: –Individual array elements can be assigned values interactively using: readLine() showInputDialog() Output: –Array elements can be printed using: print() println()

13 Aggregate Data Types Any type whose: –Individual elements are other data types –Elements are related by some defined structure Also called: –Structured type –Data structure Arrays are aggregate data types

14 String Arrays Arrays of reference data types may also be constructed Declaring String array: –String names[] = new String[4]; Arrays of reference types are stored differently from arrays of built-in data types

15

16

17

18 Run-Time Dimensioning The size of an array can also be entered interactively at run time An entered value can be used to allocate space for an array using the new operator

19 Array Initialization Arrays can be initialized within declaration statements: –May continue across multiple lines –No method of indicating repetition of initialization value –No way to initialize later array elements without first specifying values for earlier elements Example: –int grade[] = {98, 87, 92, 79, 85};

20 import javax.swing.*; public class ElementInputAndDisplay { public static void main(String[] args) { final int NUMELS= 5; String s1; int i; int grade[]; // declare the array grade = new int[NUMELS]; // allocate the array for (i = 0; i < NUMELS; i++) // enter the grades { s1 = JOptionPane.showInputDialog("Enter a grade: "); grade[i] = Integer.parseInt(s1); } for (i = 0; i < NUMELS; i++) // print the grades System.out.println("grade[" +i +"] is " + grade[i]); System.exit(0); } }

21 import javax.swing.*; public class AccumulateElements { public static void main(String[] args) { final int NUMELS= 5; String s1; int i; int total = 0; // declare and allocate the array int grade[] = new int[NUMELS]; // allocate the array for (i = 0; i < NUMELS; i++) // enter the grades { s1 = JOptionPane.showInputDialog("Enter a grade: "); grade[i] = Integer.parseInt(s1); } System.out.print("The total of the grades"); for (i = 0; i < NUMELS; i++) // display and total the grades { System.out.print(" " + grade[i]); total = total + grade[i]; } System.out.print(" is " + total); System.exit(0); } }

22 public class StringArray { public static void main(String[] args) { int i; String names[]; // declare the array names = new String[4]; // allocate the array // assign values to each array element names[0] = "Joe"; names[1] = "Harriet"; names[2] = "Allyn"; names[3] = "Roberta"; // display the names for (i = 0; i < names.length; i++) System.out.println("names[" + i + "] is " + names[i]); } }