Chapter 9 Introduction to Arrays Fundamentals of Java.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Chapter 10 Introduction to Arrays
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Arrays.
Arrays part 3 Multidimensional arrays, odds & ends.
Arrays Chapter 6 Chapter 6.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Programming with Collections Collections in Java Using Arrays Week 9.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
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: Arrays and Strings
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Chapter 9 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.
Chapter 8 Arrays and Strings
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
ArrayList, Multidimensional Arrays
Chapter 11 Arrays Continued
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
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.
 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.
Java Programming: From Problem Analysis to Program Design, 4e
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.
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.
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.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
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.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
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.
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
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.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Arrays Chapter 7.
Chapter VII: Arrays.
Lesson 8: Introduction To Arrays
Sections 10.1 – 10.4 Introduction to Arrays
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Java How to Program, Late Objects Version, 10/e
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.
Topics Covered: Arrays, 1-D & 2-D Passing & Returning Arrays
Object Oriented Programming in java
Arrays.
Presentation transcript:

Chapter 9 Introduction to Arrays Fundamentals of Java

2 Conceptual Overview There are situations in which programs need to manipulate many similar items. Most programming languages provide a data structure called an array, which consists of an ordered collection of similar items. An array has a single name, and the items in the array are referred to in terms of their position within the array.

3 Conceptual Overview

4 Elements: The items in an array All array elements have the same data type. – Can be primitive or reference type Every array has a fixed length (size). Every array element has an index or subscript. – Appear within square brackets ( [] )

5 Simple Array Manipulations Declaring an array of 500 integers: – int[] abc = new int[500]; Syntax for referring to an array element: – [ ] – must be between 0 and the array’s length minus 1 Subscript operator: []

6 Simple Array Manipulations (cont.) Examples:

7 Simple Array Manipulations (cont.) Range-bound error: Referring to a non-existent array index – ArrayIndexOutOfBoundsException thrown abc[-1] = 48; abc[500] = 48;

8 Simple Array Manipulations (cont.) Interchanging adjacent array elements:

9 Looping Through Arrays Using a loop to iterate through each element in an array is a common task. – Loop counter used as array index – Example applications: Sum the elements of an array Count occurrences of a value in an array Search for a value in an array Find first occurrence of a value in an array

10 Looping Through Arrays (cont.) An array’s length instance variable yields the size of the array. Example of loop structure for looping through an array of any size:

11 Declaring Arrays Arrays are objects. – Must be instantiated before use – Declaration example: int[] abc; – Instantiation example: abc = new int[5]; – Declare and instantiate example: int[] abc = new int[5];

12 Declaring Arrays (cont.) Because arrays are objects, all rules that apply to objects apply to arrays. – Two array variables may refer to same array. – Arrays may be garbage collected. – Array variables may be set to null. – Arrays are passed by reference to methods.

13 Declaring Arrays (cont.) Example of two array variables pointing to same array object:

14 Declaring Arrays (cont.) Example of two array variables pointing to same array object:

15 Declaring Arrays (cont.) Arrays can be declared, instantiated, and initialized using an initializer list. Arrays contain collections of similar items. – Primitive (int, double, boolean) or reference types (String, Student) Once set, the size of an array is fixed.

16 Declaring Arrays (cont.) Example:

17 Example Write segments of code that will do the following tasks: – Declare and instantiate and array of 50 doubles.

18 Example Write segments of code that will do the following tasks: – Declare and instantiate and array of 50 doubles. – Read values into the array.

19 Example Write segments of code that will do the following tasks: – Declare and instantiate and array of 50 doubles. – Read values into the array. – Sum the elements of the array

20 Example Write segments of code that will do the following tasks: – Declare and instantiate and array of 50 doubles. – Read values into the array. – Sum the elements of the array – Count the number of negative numbers in the array

21 Example Write segments of code that will do the following tasks: – Declare and instantiate and array of 50 doubles. – Read values into the array. – Sum the elements of the array – Count the number of negative numbers in the array – Search for a value in the array

22 Example Write segments of code that will do the following tasks: – Declare and instantiate and array of 50 doubles. – Read values into the array. – Sum the elements of the array. – Count the number of negative numbers in the array. – Search for a value in the array. – Find first occurrence of a value in the array

23 Working with Arrays That Are Not Full When an array is instantiated, it is filled with default values. An application might not fill all cells. Physical size: The maximum number of elements that an array can contain Logical size: The actual number of elements stored in an array

24 Working with Arrays That Are Not Full (cont.) To work with arrays that are not full, the programmer must track the logical array size. – Declare an integer counter that will always indicate the number of elements. – Every time an element is added or removed, adjust the counter accordingly. – The counter indicates the logical size of the array and the next open position in the array.

25 Working with Arrays That Are Not Full (cont.) To work with arrays that are not full, the programmer must track the logical array size. – Declare an integer counter that will always indicate the number of elements. – Every time an element is added or removed, adjust the counter accordingly. – The counter indicates the logical size of the array and the next open position in the array.

26 Working with Arrays That Are Not Full (cont.) Adding an element: Simply add the element to the end of the array

27 Working with Arrays That Are Not Full (cont.) Removing an element: To be discussed in chapter 11

28 Parallel Arrays Two or more arrays of the same size that store complementary data Example: one array stores first names and a second stores corresponding ages.

29 Using the Enhanced for Loop Provided in Java 5.0 to simplify loops in which each element in an array is accessed – From the first index to the last – Frees programmer from having to manage and use loop counters Syntax:

30 Using the Enhanced for Loop (cont.)

31 Using the Enhanced for Loop (cont.) Cannot be used to: – Move through an array in reverse, from the last position to the first position – Assign elements to positions in an array – Track the index position of the current element in an array – Access any element other than the current element on each pass

Parallel Arrays 32

33

34

35

36

37

38

39 Text Files To read from a text file: import java.io.*; import java.util.Scanner; public class IOExample { public static void main (String[ ] args) { Scanner reader = new Scanner(new File(“h:\\2008\\81234\\java\\filename.txt”); if (reader.hasNext()) int num = reader.readInt(); else System.out.println(“error in the file”);

40 Text Files To write to a text file: Instead of System.out.println(“ hello world “); import java.io.*; import java.util.Scanner; public class IOExample { public static void main (String[ ] args) { PrintWriter writer = new PrintWriter(new File(“filename.txt”); writer.println(“hello world“);

41 Arrays and Methods Because arrays are objects, they are passed by reference to methods. – The method manipulates the array itself, not a copy. Changes to array made in the method exist after method is complete. The method may create a new array and return it.

42 Arrays and Methods (cont.) Method to search for a value in an array: Method to sum the rows in a 2-D array:

43 Arrays and Methods (cont.) Method to make a copy of an array and return it:

44 Arrays of Objects Arrays can hold references to objects of any type. – When instantiated, each cell has a value of null. Example:

45 Two-Dimensional Arrays Every array discussed so far is a one- dimensional array. – Visualized as a list of items or values Arrays may have multiple dimensions. Two-dimensional arrays can be visualized as tables with rows and columns. – An “array of arrays” Each element of a one-dimensional array contains another array.

46 Two-Dimensional Arrays (cont.)

47 Two-Dimensional Arrays (cont.) Declaring and instantiating a two-dimensional array example:

48 Two-Dimensional Arrays (cont.)

49 Two-Dimensional Arrays (cont.) Looping through a two-dimensional array:

50 Two-Dimensional Arrays (cont.) Initializer lists for two-dimensional arrays: The rows in a two-dimensional array may have varying lengths. – Ragged arrays

51 Summary Arrays are collections of similar items or elements ordered by position. Arrays are useful when a program needs to manipulate many similar items, such as a group of students or a number of test scores. Arrays are objects. – Must be instantiated – Can be referred to by more than one variable

52 Summary (cont.) An array can be passed to a method as a parameter and returned as a value. Parallel arrays are useful for organizing information with corresponding elements. Two-dimensional arrays store values in a row-and-column arrangement.

53 Summary (cont.) An enhanced for loop is a simplified version of a loop for visiting each element of an array from the first position to the last position.