1 Arrays Spring 2006 UVA - CS 101 Aaron Bloomfield Edited for WLCS by Paul Bui.

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
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.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Introduction to arrays Data in economy size packages.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
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.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 Pearson Education, Inc. All rights reserved Arrays.
1 Arrays Chapter 8 Spring 2007 CS 101 Aaron Bloomfield.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Arrays Jiafan Zhou. 2 Background  Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
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.
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.
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 Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with 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.
1 Arrays Chapter 8 Spring 2006 CS 101 Aaron Bloomfield.
CSE 501N Fall ‘09 08: Arrays 22 September 2009 Nicholas Leidenfrost.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 7 Arrays 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Arrays. Background  Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional  Java.
Arrays. Background  Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional  Java.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Computer Programming for Engineers
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
1 Arrays. 2 Background  Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional 
1 Arrays. 2 Background  Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional 
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Visual Puns 1Vickie C. Ball, Harlan High School. “eggplant” 2Vickie C. Ball, Harlan High School.
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
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.
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
 2005 Pearson Education, Inc. All rights reserved Arrays.
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.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Arrays of Objects October 9, 2006 ComS 207: Programming I (in Java)
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Computer Programming BCT 1113
Arrays.
Arrays Continued.
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.
Chapter 8 Fall 2006 CS 101 Aaron Bloomfield
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
MSIS 655 Advanced Business Applications Programming
Chapter 8 Spring 2006 CS 101 Aaron Bloomfield
Arrays in Java.
Consider Write a program that prompts a user to enter the number of students and then, their names and grades. The program will then outputs the average.
Arrays of Objects October 8, 2007 ComS 207: Programming I (in Java)
Presentation transcript:

1 Arrays Spring 2006 UVA - CS 101 Aaron Bloomfield Edited for WLCS by Paul Bui

2 Background  Programmers often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional  Java provides arrays and the collection classes

3 Example  Definitions char[] c; int[] value = new int[10];  Causes Array object variable c is un-initialized Array object variable value references a new ten element list of integers  Each of the integers is default initialized to 0 value c …

4 An array example int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); 8 is displayed int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); Suppose 3 is extracted int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt();

5 Array variable definition styles  Without initialization Type of values in list Name of list Brackets indicate array variable being defined ElementType [ ] id; int [] a; int a[];

6 Array variable definition styles  With initialization ElementType [] id = new ElementType [n]; Nonnegative integer expression specifying the number of elements in the array A new array of n elements

7 Basic terminology  List is composed of elements  Elements in a list have a common name Example: a[3] = 5; The common name is ‘a’  The list as a whole is referenced through the common name  List elements are of the same type — the base type  Elements of a list are referenced by subscripting (indexing) the common name

8 Java array features  Subscripts are denoted as expressions within brackets: [ ]  Base (element) type can be any type  Size of array must be specified  Index type is integer and the index range must be 0... n-1 Where n is the number of elements Just like Strings indexing!  Automatic bounds checking Ensures any reference to an array element is valid  Data field “.length” specifies the number of elements in the list

9 Consider  Segment int[] b = new int[100]; b[-1] = 0; b[100] = 0;  Causes Array variable to reference a new list of 100 integers  Each element is initialized to 0 Two exceptions to be thrown  -1 is not a valid index – too small  100 is not a valid index – too large IndexOutOfBoundsException

10 Consider Point[] p = new Point[3]; p[0] = new Point(0, 0); p[1] = new Point(1, 1); p[2] = new Point(2, 2); p[0].setX(1); p[1].setY(p[2].getY()); Point vertex = new Point(4,4); p[1] = p[0]; p[2] = vertex; p p[0]p[1]p[2] null Point: (0, 0) p p[0]p[1] Point: (1, 1)Point: (2, 2) p[2] Point: (1, 0) p p[0]p[1] Point: (1, 1)Point: (2, 2) p[2] Point: (1, 0) p p[0]p[1] Point: (1, 2)Point: (2, 2) p[2] Point: (1, 0) p p[0]p[1] Point: (1, 2)Point: (2, 2) p[2] vertex Point: (4, 4) Point: (1, 0) p p[0]p[1] Point: (2, 2) p[2] vertex Point: (4, 4) Point: (1, 0) p p[0]p[1]p[2] vertex Point: (4, 4) Point[] p = new Point[3]; p[0] = new Point(0, 0); p[1] = new Point(1, 1); p[2] = new Point(2, 2); p[0].setX(1); p[1].setY(p[2].getY()); Point vertex = new Point(4,4); p[1] = p[0]; p[2] = vertex;

11 New 2005 demotivatiors!

12 Explicit initialization  Syntax ElementType [] id = { exp 0, 1,... exp n }; id references an array of n elements. id[0] has value exp 0, id[1] has value exp 1, and so on. Each exp i is an expression that evaluates to type ElementType

13 Explicit initialization  Example String[] puppy = { “pika”, “mila”, “arlo”, “nikki” }; int[] unit = { 1 };  Equivalent to String[] puppy = new String[4]; puppy[0] = “pika"; puppy[1] = “mila"; puppy[2] = “arlo"; puppy[3] = “nikki"; int[] unit = new int[1]; unit[0] = 1;

14 Array members  Member length Size of the array for (int i = 0; i < puppy.length; ++i) { System.out.println(puppy[i]); }

15 Review of arrays  Creating an array: int[] foo = new int[10];  Accessing an array: foo[3] = 7; System.out.print (foo[1]);  Creating an array: String[] bar = new String[10];  Accessing an array: bar[3] = “qux”; System.out.println (bar[1]);

16 How Java represents arrays  Consider int[] a = { 1, 2, 3, 4, 5 }; a … Array - length = 5 - data = 12345

17 More about how Java represents Arrays  Consider int[] a; int[] b = null; int[] c = new int[5]; int[] d = { 1, 2, 3, 4, 5 }; a = c; d = c; a - b null cd int[] a; int[] b = null; int[] c = new int[5]; int[] d = { 1, 2, 3, 4, 5 }; a = c; d = c;

18 What do these pictures mean? Light beer Light beer Dandy lions Dandy lions Assaulted peanut Assaulted peanut Eggplant Eggplant Dr. Pepper Dr. Pepper Pool table Pool table Tap dancers Tap dancers Card shark Card shark King of pop King of pop I Pod I Pod Gator aide Gator aide Knight mare Knight mare Hole milk Hole milk

19 Java Array Code Examples  To print the array: public void printArray(int[] data) { for (int i = 0; i < data.length; ++i) { System.out.println(data[i]); } }  Consider int[] score = { 6, 9, 82, 11, 29, 85, 11, 28, 91 }; printArray(score);

20 Java Array Code Examples  Returns a reversed version of the int array public int[] reverse(int[] data) { int[] clone = data.clone(); for ( int i = 0; i < clone.length; ++i ) { clone[i] = data[data.length-1-i]; } return clone; }  Consider int[] foo = { 1, 2, 3, 4, 5 }; printArray( reverse (foo));