Chapter 5: ARRAYS ARRAYS. Why Do We Need Arrays? Java Programming: From Problem Analysis to Program Design, 4e 2  We want to write a Java program that.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Arrays, A1 COMP 401, Fall 2014 Lecture 4 8/28/2014.
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.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 14, 2005.
1 One-Dimensional (1-D) Array Overview l Why do we need 1-D array l 1-D array declaration and Initialization l Accessing elements of a 1-D array l Passing.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
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.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 9 Arrays.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006.
COMP 14 Introduction to Programming Miguel A. Otaduy June 1, 2004.
Chapter 5: METHODS USER-DEFINED METHODS. user-defined  We learned that a Java application program is a collection of classes, and that a class is a collection.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
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 8 Arrays and Strings
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
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.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 8: Arrays.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Chapter 9: Arrays J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
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.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CSE 501N Fall ‘09 08: Arrays 22 September 2009 Nicholas Leidenfrost.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
8-1 Chapter-7 Part1 Introduction to Arrays –Array Types –Creating/Declaring Arrays –Initializing Arrays –Processing Array Contents –Passing Arrays as Arguments.
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
Arrays An array is an indexed data structure which is used to store data elements of the same data type. An array is an indexed data structure which is.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
Java Programming: Chapter 9: Arrays
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
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.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 7 Arrays…. 7-2 Arrays An array is an ordered list of values An array of size N is indexed from.
Introduction to programming in java Lecture 21 Arrays – Part 1.
1 Why do we need arrays? Problem - Input 5 test scores => int test1,test2,test3,test4,test5 100 test scores? 10,000 employees? A structured data type is.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Lesson 9 Arrays. Miscellaneous About Arrays An array is an object. Because of this, the array name is a reference variable Therefore, in order to start.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Array 1 ARRAY. array 2 Learn about arrays. Explore how to declare and manipulate data into arrays. Understand the meaning of “array index out of bounds.”
1 Arrays and Variable Length Parameter List  The syntax to declare a variable length formal parameter (list) is: dataType... identifier.
Chapter 9: Arrays J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 8 Arrays and the ArrayList Class Introduction to Arrays.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Computer Programming BCT 1113
Chapter 7 Part 1 Edited by JJ Shepherd
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.
int [] scores = new int [10];
Lecture 9 Objectives Learn about arrays.
Dr. Sampath Jayarathna Cal Poly Pomona
Java Programming: Program Design Including Data Structures
Presentation transcript:

Chapter 5: ARRAYS ARRAYS

Why Do We Need Arrays? Java Programming: From Problem Analysis to Program Design, 4e 2  We want to write a Java program that reads five numbers, finds their sum, and prints the numbers in reverse order. System.out.println("Enter five integers: "); item0 = console.nextInt(); item1 = console.nextInt(); item2 = console.nextInt(); item3 = console.nextInt(); item4 = console.nextInt(); sum = item0 + item1 + item2 + item3 + item4; System.out.println("The sum of the numbers = " + sum); System.out.print("The numbers in reverse order are: "); System.out.println(item4 + " " + item3 + " " + item2 + " " + item1 + " " + item0); System.out.println("Enter five integers: "); item0 = console.nextInt(); item1 = console.nextInt(); item2 = console.nextInt(); item3 = console.nextInt(); item4 = console.nextInt(); sum = item0 + item1 + item2 + item3 + item4; System.out.println("The sum of the numbers = " + sum); System.out.print("The numbers in reverse order are: "); System.out.println(item4 + " " + item3 + " " + item2 + " " + item1 + " " + item0);

Why Do We Need Arrays? Java Programming: From Problem Analysis to Program Design, 4e 3 item0 item1 item2 item3 item4 Note the following in the preceding program: 1. Five variables must be declared because the numbers are to be printed in reverse order. 2. All variables are of type int—that is, of the same data type. 3. The way in which these variables are declared indicates that the variables to store these numbers have the same name except for the last character, which is a number. item[0] item[1] item[2] item[3] item[4] in Java is called an array.

Array Java Programming: From Problem Analysis to Program Design, 4e 4  Definition: structured data type with a fixed number of elements  Elements of an array are also called components of the array  Every element is of the same type  Elements are accessed using their relative positions in the array

One-Dimensional Arrays Java Programming: From Problem Analysis to Program Design, 4e 5

One-Dimensional Arrays (continued) Java Programming: From Problem Analysis to Program Design, 4e 6

Array num: int[] num = new int[5]; 7 Java Programming: From Problem Analysis to Program Design, 4e Arrays When an array is instantiated, Java automatically initializes its elements to their default values. numeric arrays are initialized to 0, char arrays are initialized to the null character, which is '\u0000', boolean arrays are initialized to false.

Array num: int[] num = new int[5]; Java Programming: From Problem Analysis to Program Design, 4e 8 Arrays (continued) To save space, we also draw an array, as shown in Figures 9-2(a) and 9-2(b).

One-Dimensional Arrays (continued) Java Programming: From Problem Analysis to Program Design, 4e 9  intExp = number of components in array >= 0  0 <= indexExp < intExp

Array List Java Programming: From Problem Analysis to Program Design, 4e 10

Array List (continued) Java Programming: From Problem Analysis to Program Design, 4e 11

Array List (continued) Java Programming: From Problem Analysis to Program Design, 4e 12

Array List (continued) 13 Java Programming: From Problem Analysis to Program Design, 4e

14 Specifying Array Size During Program Execution

Java Programming: From Problem Analysis to Program Design, 4e 15  The initializer list contains values, called initial values, that are placed between braces and separated by commas  sales[0]= 12.25, sales[1]= 32.50, sales[2]= 16.90, sales[3]= 23.00, and sales[4]= Array Initialization During Declaration

Array Initialization During Declaration (continued) Java Programming: From Problem Analysis to Program Design, 4e 16 int[] list = {10, 20, 30, 40, 50, 60};  When declaring and initializing arrays, the size of the array is determined by the number of initial values within the braces.  If an array is declared and initialized simultaneously, we don’t use the operator new to instantiate the array object

Arrays and the Instance Variable length Java Programming: From Problem Analysis to Program Design, 4e 17  Associated with each array that has been instantiated, there is a public ( final ) instance variable length  The variable length contains the size of the array  The variable length can be directly accessed in a program using the array name and the dot operator

Arrays and the Instance Variable length (continued) Java Programming: From Problem Analysis to Program Design, 4e 18  int[] list = {10, 20, 30, 40, 50, 60}; This statement creates the array list of six components and initializes the components using the values given  Here list.length is 6  int[] numList = new int[10]; This statement creates the array numList of 10 components and initializes each component to 0

Arrays and the Instance Variable length (continued) Java Programming: From Problem Analysis to Program Design, 4e 19  The value of numList.length is 10 numList[0] = 5; numList[1] = 10; numList[2] = 15; numList[3] = 20;  These statements store 5, 10, 15, and 20, respectively, in the first four components of numList  You can store the number of filled elements, that is, the actual number of elements, in the array in a variable, say numOfElement  It is a common practice for a program to keep track of the number of filled elements in an array

Array Index Out of Bounds Exception double[] num = double[10]; int i;  The element num[i] is valid, that is, i is a valid index if i = 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9.  The index—say, index—of an array is in bounds if index >= 0 and index <= arraySize - 1.  If either index arraySize - 1, then we say that the index is out of bounds. Java Programming: From Problem Analysis to Program Design, 4e 20

Reading Assignment What is the difference between the following declaration int alpha[], beta; int[ ] gamma, delta; Java Programming: From Problem Analysis to Program Design, 4e 21

Processing One-Dimensional Arrays Java Programming: From Problem Analysis to Program Design, 4e 22  Loops used to step through elements in array and perform operations int[] list = new int[100]; int i; for (i = 0; i < list.length; i++) //process list[i], the (i + 1)th //element of list for (i = 0; i < list.length; i++) list[i] = console.nextInt(); for (i = 0; i < list.length; i++) System.out.print(list[i] + " ");

Arrays (continued) Java Programming: From Problem Analysis to Program Design, 4e 23  Some operations on arrays  Initialize  Input data  Output stored data  Find largest/smallest/sum/average of elements  Search for an element double[] sales = new double[10]; int index; double largestSale,sum,average,searchItem;

Code to Initialize Array to Specific Value (10.00) Java Programming: From Problem Analysis to Program Design, 4e 24 //Code to Initialize Array to Specific Value //(10.00) for(int index = 0; index < sales.length;index++) sales[index] = 10.00;

Code to Read Data into Array Java Programming: From Problem Analysis to Program Design, 4e 25 //Code to Read Data into Array for (int index = 0; index < sales.length;index++) sales[index] = console.nextDouble();

Code to Print Array Java Programming: From Problem Analysis to Program Design, 4e 26 //Code to Print Array for (int index = 0; index < sales.length;index++) System.out.print(sales[index] + " ");

What will happen if we print array name System.out.print(sales + " "); Java Programming: From Problem Analysis to Program Design, 4e 27

Code to Find Sum and Average of Array Java Programming: From Problem Analysis to Program Design, 4e 28 sum = 0; for (int index = 0; index < sales.length; index++) sum = sum + sales[index]; if (sales.length != 0) average = sum / sales.length; else average = 0.0;

Determining Largest Element in Array Java Programming: From Problem Analysis to Program Design, 4e 29 maxIndex = 0; for (int index = 1; index < sales.length; index++) if (sales[maxIndex] < sales[index]) maxIndex = index; largestSale = sales[maxIndex];

Determining Largest Element in Array (continued) 30 Java Programming: From Problem Analysis to Program Design, 4e

Determining Largest Element in Array (continued) Java Programming: From Problem Analysis to Program Design, 4e 31

32 Searching for specific  Search for 10  Search starts at the first element in the list, that is, at list[0]  This time, the search item, which is 10, is compared with every item in the list; eventually, no more data is left in the list to compare with the search item; this is an unsuccessful search Java Programming: From Problem Analysis to Program Design, 4e 32

33 searchItem = 10; //can be parameter to a method int listLength = sales.length; int loc; boolean found = false; loc = 0; while (loc < listLength && !found) if (sales[loc] == searchItem) found = true; else loc++; if (found) System.out.print( loc); else System.out.print(“not found”); Java Programming: From Problem Analysis to Program Design, 4e 33

Array Index Out of Bounds Java Programming: From Problem Analysis to Program Design, 4e 34  Array in bounds if: 0 <= index <= arraySize – 1  If index arraySize : ArrayIndexOutOfBoundsException exception is thrown  Base address: memory location of first component in array

The Assignment Operators and Arrays Java Programming: From Problem Analysis to Program Design, 4e 35

The Assignment Operators and Arrays (continued) Java Programming: From Problem Analysis to Program Design, 4e 36

The Assignment Operators and Arrays (continued) Java Programming: From Problem Analysis to Program Design, 4e 37

Relational Operators and Arrays Java Programming: From Problem Analysis to Program Design, 4e 38 if (listA == listB)... - The expression listA == listB determines if the values of listA and listB are the same and thus determines whether listA and listB refer to the same array - To determine whether listA and listB contain the same elements, you need to compare them component by component - You can write a method that returns true if two int arrays contain the same elements

Relational Operators and Arrays (continued) Java Programming: From Problem Analysis to Program Design, 4e 39 int[] firstArray; int[] secondArray; if (firstArray.length != secondArray.length) ………; for (int index = 0; index < firstArray.length; index++) if (firstArray[index] != secondArray[index]) ……;

Relational Operators and Arrays (example) Java Programming: From Problem Analysis to Program Design, 4e 40 boolean areEqualArray= true; if(ListA.length != ListB.length) { areEqualArray=false; } else for (int index = 0; index < ListA.length;index++) if (ListA[index] != ListB[index]) { areEqualArray=false; break; } if (areEqualArray) System.out.println("they are equals"); else System.out.println("they are not equals");