© 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.

Slides:



Advertisements
Similar presentations
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Advertisements

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.
Chapter 10 Introduction to Arrays
Chapter 7 – Arrays.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
©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.
Datalogi A 8: 27/10. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[] data.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
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”
Chapter 9: Arrays and Strings
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
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.
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
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 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional 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.
Chapter 8 Arrays and Strings
Chapter 11 Arrays Continued
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
© 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.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
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.
Pemrograman Dasar Arrays PTIIK - UB. Arrays  An array is a container object that holds a fixed number of values of a single type.  The length of an.
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.
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.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
Enhanced For Loops (For Each Loops) Less Code and Less Overhead for Writing For Loops.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Objectives You should be able to describe: One-Dimensional Arrays
Arrays Chapter 7.
Can store many of the same kind of data together
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.
Can store many of the same kind of data together
Object Oriented Programming in java
Can store many of the same kind of data together
Chapter 8 Arrays Objectives
Arrays.
Presentation transcript:

© 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 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:

© 2007 Lawrenceville Press Slide 2 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"};

© 2007 Lawrenceville Press Slide 3 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";

© 2007 Lawrenceville Press Slide 4 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); }

© 2007 Lawrenceville Press You do Pg 239 Review: StudentRoster Review: Squares Review: Reverse

© 2007 Lawrenceville Press Slide 6 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.

© 2007 Lawrenceville Press Slide 7 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

© 2007 Lawrenceville Press Slide 8 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()

© 2007 Lawrenceville Press Slide 9 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 , which translates to  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

© 2007 Lawrenceville Press Slide 10 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.

© 2007 Lawrenceville Press Slide 11 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:

© 2007 Lawrenceville Press Slide 12 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

© 2007 Lawrenceville Press Slide 13 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

© 2007 Lawrenceville Press Slide 14 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()

© 2007 Lawrenceville Press Slide 15 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;