Can store many of the same kind of data together

Slides:



Advertisements
Similar presentations
Hip Hip Array! AP Computer Science. Remember Strings? Strings are an array of characters An array is a collection of variables all of the same type. Arrays.
Advertisements

Chapter 10 Introduction to Arrays
Arrays.
Chapter 7 – Arrays.
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.
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.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
AP CS Workshop ArrayList It is very common for applications to require us to store a large amount of data. Array lists store large amounts of data.
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.
AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
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.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
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.
Arrays Chapter 7.
CMSC 202 ArrayList Aug 9, 2007.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Pointers and Linked Lists
Chapter 4 Assignment Statement
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 20 Generic Classes and Methods
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
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.
CMSC 202 ArrayList Aug 9, 2007.
Can store many of the same kind of data together
Object Oriented Programming in java
CMSC 202 ArrayList Aug 9, 2007.
Grouped Data Arrays, and Array Lists.
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
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 10 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: Refer to page 239 in the text. In the diagram, Kermit is the first element and has index 0. The last and fifth element is Myah with index 4. © 2007 Lawrenceville Press

Chapter 10 Creating and Initializing Arrays 11/23/2018 5:18 AM Chapter 10 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"}; Refer to pages 239 and 240 in the text. © 2007 Lawrenceville Press

Chapter 10 Accessing Array Elements 11/23/2018 5:18 AM Chapter 10 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"; Refer to page 240 in the text. © 2007 Lawrenceville Press

Chapter 10 Traversing an Array 11/23/2018 5:18 AM Chapter 10 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); } Refer to pages 240 and 241 in the text. 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. © 2007 Lawrenceville Press

Chapter 10 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. Refer to pages 241 and 242 in the text. © 2007 Lawrenceville Press

Chapter 10 Arrays with Meaningful Indexes 11/23/2018 5:18 AM Chapter 10 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 Refer to pages 242 and 243 in the text. © 2007 Lawrenceville Press

Chapter 10 The String Class 11/23/2018 5:18 AM Chapter 10 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() Refer to page 244 in the text. © 2007 Lawrenceville Press

Chapter 10 Unicode and char 11/23/2018 5:18 AM Chapter 10 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 Refer to pages 244 and 245 in the text. © 2007 Lawrenceville Press

Simplest searching algorithm 11/23/2018 5:18 AM Chapter 10 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. Refer to pages 246 and 247 in the text. © 2007 Lawrenceville Press

Chapter 10 Two-Dimensional Arrays 11/23/2018 5:18 AM Chapter 10 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: Refer to pages 247 and 248 in the text. © 2007 Lawrenceville Press

Chapter 10 Two-Dimensional Arrays 11/23/2018 5:18 AM Chapter 10 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 Refer to page 248 in the text. © 2007 Lawrenceville Press

Chapter 10 The ArrayList Class 11/23/2018 5:18 AM Chapter 10 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 Refer to pages 252 and 253 in the text. 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. © 2007 Lawrenceville Press

Chapter 10 Wrapper Classes 11/23/2018 5:18 AM Chapter 10 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() Refer to pages 253 and 254 in the text. © 2007 Lawrenceville Press

Chapter 10 Autoboxing and Auto-unboxing 11/23/2018 5:18 AM Chapter 10 Autoboxing and Auto-unboxing Eliminates the need to manually wrap and unwrap primitives for use in a collection Offers simpler, cleaner code because the wrapper class methods are no longer needed. Before autoboxing/unboxing: After autoboxing/unboxing: numbers.add(new Integer(5)); numbers.add(5); element = numbers.get(0); element = numbers.get(0); sum += element.intValue(); sum += element; Refer to pages 253 and 254 in the text. © 2007 Lawrenceville Press