11 Chapter 8 ARRAYS Continued. 22 AN ARRAY OF STRINGS We can create arrays of strings or other types of objects.

Slides:



Advertisements
Similar presentations
Chapter 8: Arrays.
Advertisements

1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 25 Thanks for Lecture Slides: Dr. Sadaf Tanveer Dr. Sadaf Tanveer,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
An Introduction to Programming with C++ Fifth Edition
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Data modelling uses two main techniques Entity relationship (E-R) modelling: a top- down approach Normalisation: a bottom-up approach.
Introduction to Programming with C++ Fourth Edition
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
Chapter 11 Structure. 2 Objectives You should be able to describe: Structures Arrays of Structures Structures as Function Arguments Dynamic Structure.
CS 1400 Chapter 7 ARRAYS. Array variables Simple variables can hold single values int x;// x can hold one integer float y; // y can hold one float Array.
11 Chapter 5 METHODS. 22 INTRODUCTION TO METHODS A method is a named block of statements that performs a specific task. Other languages use the terms.
1 Chapter 2 JAVA FUNDAMENTALS CONT’D. 2 PRIMITIVE DATA TYPES Computer programs operate on data values. Values in Java are classified according to their.
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
Chapter 8 ARRAYS Continued
Chapter 8: String Manipulation
Programming with Microsoft Visual Basic th Edition
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
SHOPPING CARTS CHAPTER 19. E-COMMERCE Typically, an e-commerce site will have public pages and admin pages.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
11 Chapter 5 METHODS CONT’D. 22 MORE ON PASSING ARGUMENTS TO A METHOD Passing an Object Reference as an Argument to a Method Objects are passed by reference.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Understanding Structures tMyn1 Understanding Structures In order to describe virtually anything in the real world, you need to define several values that.
Chapter 8: Arrays.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
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.
Arrays Chapter 8. Chapter 8 - Part 12 Variable and Variable Array Variable Stores only one value Variable Array Variable that has 1 symbolic name but.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
A FIRST BOOK OF C++ CHAPTER 16 DATA STRUCTURES. OBJECTIVES In this chapter, you will learn about: Single Structures Arrays of Structures Structures as.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Chapter 15 Strings as Character Arrays
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.
Computer Programming for Engineers
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
1 CS161 Introduction to Computer Science Topic #15.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Structured Data Objects (Structs). array Recall our definition of an array: fixedcontiguousall of the same data type an array is a fixed number of contiguous.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs Structured Data Objects (Structs) Chapter 7.
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.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Week 9 – Lesson 1 Arrays – Character Strings
Strings A collection of characters taken as a set:
Data Structures & Algorithms
Presentation transcript:

11 Chapter 8 ARRAYS Continued

22 AN ARRAY OF STRINGS We can create arrays of strings or other types of objects.

33 AN ARRAY OF STRINGS Example: We can create an array of String objects containing the strings "Texas", "Oklahoma", "Kansas", and "Nebraska" by writing: String[ ] states = {"Texas", "Oklahoma", "Kansas", "Nebraska"}; states[0] is the first string in the list, “Texas”. states[1] is the second string in the list, “Oklahoma”..

44 AN ARRAY OF STRINGS ***See the program DisplayStringArray.java on webct

55 AN ARRAY OF STRINGS ***See the program DemoStringArray.java on webct

66 AN ARRAY OF STRINGS In order to use a String object you need a reference to the String object, so an array of strings is really an array of references to String objects.

77 AN ARRAY OF STRINGS The declaration String[ ] studentNames creates a variable that can store a reference to an array of String references. String[ ] studentNames = new String[5]; address studentNames

88 AN ARRAY OF STRINGS The new operator is used to create an array of five references to String objects. The address of this array is assigned to studentNames. String[ ] studentNames = new String[5]; null [0] [1] [2] [3] address studentNames null [4]

99 AN ARRAY OF STRINGS So far we have not created any String objects. The key word null indicates that the String reference variables do not reference String objects. String[ ] studentNames = new String[5]; null [0] [1] [2] [3] address studentNames null [4] The key word null indicates that the reference variables do not reference an object.

10 AN ARRAY OF STRINGS We can create a String object and assign the address of the object to one of the String reference variables in the array as shown below: String[ ] studentNames = new String[5]; studentNames[0] = "Jane Doe"; address null [0] [1] [2] [3] address studentNames null [4] Jane Doe

11 AN ARRAY OF STRINGS We can also use the nextLine method of a Scanner object or the showInputDialog method of the JOptionPane class to read a string entered by the user and store the address of the String object returned by these methods in one of the reference variables in the array. String[ ] studentNames = new String[5]; studentNames[1] = JOptionPane.showInputDialog(”Enter the 2nd student’s name.”); address null [0] [1] [2] [3] address studentNames null [4]

12 AN ARRAY OF STRINGS In DemoStringArray.java the String objects are created when we call the nextLine method of a Scanner object. String[ ] studentNames = new String[5]; null [0] [1] [2] [3] address studentNames null [4]

13 AN ARRAY OF STRINGS The member methods of a String object can be used on array elements that reference a String object.

14 AN ARRAY OF STRINGS Given the following declaration: String[ ] states = {"Texas", "Oklahoma", "Kansas", "Nebraska"}; The statement below assigns the variable firstLetter the value of ‘T’. Remember states[0] is a reference to a String object and every String object has a member method named charAt. char firstLetter = states[0].charAt(0);

15 AN ARRAY OF STRINGS Given the following declaration: String[ ] states = {"Texas", "Oklahoma", "Kansas", "Nebraska"}; The variable named equalStrings is assigned the value false in the segment below. Every String object has a member method named equalsIgnoreCase. boolean equalStrings = states[1].equalsIgnoreCase(”Texas");

16 AN ARRAY OF STRINGS Given the following declaration: String[ ] states = {"Texas", "Oklahoma", "Kansas", "Nebraska"}; The statement below displays NEBRASKA on a line on the computer screen. Notice we are not storing the reference to the uppercase copy of the string that was created, we are simply displaying this string on the screen. System.out.println(states[3].toUpperCase( ));

17 PARALLEL ARRAYS In many applications there are several pieces of data of different data types that are related to each other. For example: –A employee's ID number, name, address, telephone number, pay rate –A student's ID number, name, major, and GPA –A book’s ISBN number, title, author, publisher, price, and quantity available

18 PARALLEL ARRAYS To maintain the relationship between these pieces of data and process them in parallel, we can set up a system of parallel arrays. In a system of parallel arrays, data for a particular entity (employee, student, book) is stored at the same relative position/offset/subscript in a number of separate arrays.

19 PARALLEL ARRAYS *** See Case Study 4 on the CD that came with the textbook for a discussion of parallel arrays

20 PARALLEL ARRAYS *** See the program ParallelArrays.java on webct.