Can store many of the same kind of data together

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
©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.
© The McGraw-Hill Companies, 2006 Chapter 5 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.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
© 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.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Chapter 11 Arrays Continued
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.
© 2012 EMC Publishing, LLC Slide 1 Chapter 8 Arrays  Can store many of the same type of data together.  Allows a collection of related values to be stored.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
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.
© 2007 Lawrenceville Press Slide 1 Chapter 10 Arrays  Can store many of the same kind of data together  Allows a collection of related values to be stored.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Arrays BCIS 3680 Enterprise Programming. Overview 2  Array terminology  Creating arrays  Declaring and instantiating an array  Assigning value to.
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.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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.
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.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Data Types Chapter 6: Data Types Lectures # 11. Topics Introduction Primitive Data Types Character String Types Array Types Associative Arrays Record.
Arrays Chapter 7.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Pointers and Linked Lists
Pointers and Linked Lists
Arrays.
Arrays 1. One dimensional arrays - Review 2. Using arrays
Computer Programming BCT 1113
Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Chapter 7 Part 1 Edited by JJ Shepherd
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
JavaScript: Functions.
C# Programming Arrays in C# Declaring Arrays of Different Types Initializing Array Accessing Array Elements Creating User Interfaces Using Windows Standards.
CS313D: Advanced Programming Language
Peer Instruction 6 Java Arrays.
Can store many of the same kind of data together
Chapter 8 Slides from GaddisText
Chapter 8 Arrays Objectives
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.
Object-Oriented Programming Paradigm
Object Oriented Programming in java
Grouped Data Arrays, and Array Lists.
MSIS 655 Advanced Business Applications Programming
Data Structures (CS212D) Week # 2: Arrays.
Arrays Week 2.
Can store many of the same kind of data together
Introduction to Data Structure
Chapter 8 Arrays Objectives
Java Programming Language
Dr. Sampath Jayarathna Cal Poly Pomona
Creating and Modifying Text part 3
Review: libraries and packages
Arrays.
Presentation transcript:

Can store many of the same kind of data together Chapter 9 Arrays Can store many of the same kind of data together Allows a collection of related values to be stored together with a single descriptive name A data structure with a fixed length Each item is called an element and each element has an index value. The friends array has 5 elements: In the diagram, Kermit is the first element and has index 0. The last and fifth element is Myah with index 4. © 2012 EMC Publishing, LLC

Chapter 9 Creating and Initializing Arrays 12/29/2018 4:37 AM Chapter 9 Creating and Initializing Arrays Declare and then allocate space: String[] friends; //declare array friends = new String[5]; //allocate space Declare and allocate: String[] friends = new String[5]; Declare, allocate, and initialize: String[] friends = {"Jo","Ed","Ty","Lu","KC"}; © 2012 EMC Publishing, LLC

Chapter 9 Accessing Array Elements 12/29/2018 4:37 AM Chapter 9 Accessing Array Elements An array element is accessed by including its index in brackets after the array name: System.out.println(friends[2]); An array element is changed through assignment: friends[2] = "Sunshine"; © 2012 EMC Publishing, LLC

Chapter 9 Traversing an Array 12/29/2018 4:37 AM Chapter 9 Traversing an Array The length attribute is used to determine the length of an array: numElements = friends.length; A for loop is one way to traverse an array: for (int i = 0; i < friends.length; i++) { System.out.println(friends[i]); } A for-each loop is also used to traverse an array: for (String element : friends) { System.out.println(element); } Note that the length attribute should not include parentheses after it. length is an attribute, not a method. Traversing an array with a for loop requires determining the length of the array. The loop iterates from 0 to one less than the length. The for-each loop does not require a loop control variable and helps prevent the exception ArrayIndexOutOfBoundsException. However, it cannot be used in situations where the array index value is needed. For example, when the elements of an array are to be listed in reverse order. © 2012 EMC Publishing, LLC

Chapter 9 Array Parameters A method declaration can include array parameters: public static void changeArray(int[] nums) Passing the whole array to a method passes the reference to the elements, allowing the actual array elements to be changed. A method declaration can include an array element: public static void useElement(int num) Passing just an element to a method passes a copy of the value, preventing the element in the array from being changed. © 2012 EMC Publishing, LLC

Chapter 9 Arrays with Meaningful Indexes 12/29/2018 4:37 AM Chapter 9 Arrays with Meaningful Indexes Use the index value of an array element for determining the storage location of a value Simplifies storage and retrieval of data For ranges of data that start at a high value, offset array indexes are used. When using offset array indexes, the index value is calculated with the formula: highValue – lowValue + 1 © 2012 EMC Publishing, LLC

Chapter 9 The String Class 12/29/2018 4:37 AM Chapter 9 The String Class A String object can be converted to an array of characters: toCharArray() An individual character of a String object can be converted to char value: charAt() © 2012 EMC Publishing, LLC

Chapter 9 Unicode and char 12/29/2018 4:37 AM Chapter 9 Unicode and char Uses a set of sixteen 1s and 0s to form a sixteen-bit binary code for a symbol. For example, the Unicode symbol for V is 00000000 01010110, which translates to 8610. When a letter is assigned to a char variable, the variable actually stores the Unicode representation of the letter. Because char is a primitive data type, char values can be compared with relational operators Type casting can be used to produce the Unicode equivalent character for a number © 2012 EMC Publishing, LLC

Simplest searching algorithm 12/29/2018 4:37 AM Chapter 9 Linear Search Simplest searching algorithm Checks each element of an array, one after the other, until a specified value has been found or until the entire array has been checked. © 2012 EMC Publishing, LLC

Chapter 9 Two-Dimensional Arrays 12/29/2018 4:37 AM Chapter 9 Two-Dimensional Arrays Represents data that corresponds to a grid An element is referred to by its row and column. The tttBoard[1][2] element stores an X: © 2012 EMC Publishing, LLC

Chapter 9 Two-Dimensional Arrays 12/29/2018 4:37 AM Chapter 9 Two-Dimensional Arrays Declaration includes the type followed by two sets of brackets ([][]) The number of rows is determined with a statement similar to: arrayName.length The number of columns is determined with a statement similar to: arrayName[0].length Nested for loops are often used to access the elements of a two-dimensional array © 2012 EMC Publishing, LLC

Chapter 9 The ArrayList Class 12/29/2018 4:37 AM Chapter 9 The ArrayList Class Part of the java.util package A class for implementing a collection of objects (primitive types cannot be stored) Used for implementing a dynamic array ArrayList methods include: add() remove() get() set() indexOf() size() indexOf() method compares its object parameter to each element of the array using the object's equals() method A collection is a group of related objects, or elements, that are stored together as a single unit. An array is a collection. The ArrayList class is a class for implementing an array. The ArrayList class implements a dynamic array. A dynamic array varies in size during run time and is used in applications where the size of an array may need to grow or shrink. Arrays are a fixed-length structure, but ArrayLists are dynamic. The indexOf() method compares objects. Therefore, relational operators cannot be used to determined how one object relates to another. The indexOf() method calls the object's equals() method to compare objects. Objects stored in an ArrayList must have an appropriately overridden equals() method. © 2012 EMC Publishing, LLC

Chapter 9 Wrapper Classes 12/29/2018 4:37 AM Chapter 9 Wrapper Classes Used to "wrap" primitive values in an object Integer and Double classes implement the Comparable interface Integer Class includes methods: compareTo() intValue() Double class includes methods: compareTo() doubleValue() © 2012 EMC Publishing, LLC