Arrays.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms The C Programming Language.
Advertisements

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,
Visual C++ Programming: Concepts and Projects
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
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.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
Java Unit 9: Arrays Declaring and Processing Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
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.
Chapter 8: Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Sorting CS /02/05 L12: Sorting Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved The.
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
Arrays and Lists: Handling Infinite Data CS 21a: Introduction to Computing I First Semester,
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.
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 and Array Lists CS 21a. Problem 1: Reversing Input Problem: Read in three numbers and then print out the numbers in reverse order Straightforward.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Java: Base Types All information has a type or class designation
Arrays Chapter 7.
More on Arrays Review of Arrays of ints, doubles, chars
Chapter VII: Arrays.
Computer Organization and Design Pointers, Arrays and Strings in C
Java: Base Types All information has a type or class designation
Lecture 14 Searching and Sorting Richard Gesick.
Computer Programming BCT 1113
Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.
Array, Strings and Vectors
Programming Languages and Paradigms
Chapter 7 Part 1 Edited by JJ Shepherd
Programming Paradigms
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Programming Languages and Paradigms
Objects First with Java CITS1001
Java How to Program, Late Objects Version, 10/e
Can store many of the same kind of data together
Chapter 8 Slides from GaddisText
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.
Lecture 11 Searching and Sorting Richard Gesick.
Can store many of the same kind of data together
Data Structures (CS212D) Week # 2: Arrays.
Arrays and Array Lists CS 21a.
Building Java Programs
Can store many of the same kind of data together
A Wide Array of Possibilities
Java Programming Language
Dr. Sampath Jayarathna Cal Poly Pomona
Lecture 03 & 04 Method and Arrays Jaeki Song.
Programming Languages and Paradigms
Multidimensional Arrays Section 6.4
Arrays.
CS Problem Solving and Object Oriented Programming Spring 2019
Presentation transcript:

Arrays

Java Collections Uncovered Collection classes (particularly ArrayList) are implemented using arrays Arrays are structures that are part of the Java language If you need a collection of primitive types (e.g., ints or doubles), arrays are arguably more convenient to use than ArrayLists Wrapper classes can be avoided Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 2

Arrays Definition In Java, pronounced “ar-ray”, w/ stress on 2nd syllable Definition sequence of elements of the same type each element is accessed through an index In Java, declaration: double[] nums; creation: nums = new double[8]; use: nums[3] = 6.6; Note: starting index is 0 (0 to 7, above) double nums[] is also legal, but double[] nums is preferred, since it emphasizes that the type is double[] (“double array” or “array of doubles”) Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 3

Visualizing an Array nums null Declare: double[] nums; Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 4

Visualizing an Array nums null Declare: double[] nums; 0.0 1 2 3 4 5 6 7 null Declare: double[] nums; Create: nums = new double[8]; Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 5

Visualizing an Array nums 6.6 Declare: double[] nums; 0.0 1 2 3 4 5 6 7 6.6 Declare: double[] nums; Create: nums = new double[8]; Use: nums[3] = 6.6; Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 6

Array of Objects Declaration Creation of the Array Creation of Objects Circle[] circles; Creation of the Array circles = new Circle[8]; creates an array of references to Circles but no actual Circles yet Creation of Objects for ( i = 0; i < 8; i++ ) { circles[i] = new Circle( 10 + i * 10, 10, 5 ); } creates the Circles themselves and assigns them to the references Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 7

Visualizing an Array of Objects circles null Declare: Circle[] circles; Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 8

Visualizing an Array of Objects 1 2 3 4 null Circle-type references circles null Declare: Circle[] circles; Create array: circles = new Circle[5]; Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 9

Visualizing an Array of Objects Circle-type references Circle x y radius 10 10 5 circles null Circle x y radius 20 10 5 null 1 null Circle x y radius 30 10 5 2 null Circle x y radius 40 10 5 null 3 Declare: Circle[] circles; 4 Circle x y radius 50 10 5 null Create array: circles = new Circle[5]; Create objects: for ( i = 0; i < 5; i++ ) { circles[i] = new Circle( 10 + i * 10, 10, 5 ); } Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 10

Visualizing an Array of Objects Circle-type references Circle x y radius 10 10 5 circles null Circle x y radius 20 10 5 null 1 null Circle x y radius 30 10 5 2 null null 3 Declare: Circle[] circles; 4 Circle x y radius 50 10 5 null Create array: circles = new Circle[5]; Circle x y radius 40 10 5 Circle x y radius 40 10 5 Create objects: for ( i = 0; i < 5; i++ ) { circles[i] = new Circle( 10 + i * 10, 10, 5 ); } Use objects: e.g., circles[3].getX(); (returns 40) Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 11

More About Arrays Arrays are objects Special features the array variable is just a reference to the actual array that contains the values need to use “new” after declaring passed as a reference when used as method parameter Special features a public final int length field returns the array size in recent example, circles.length would return 5 [] operator only work with arrays ArrayIndexOutOfBounds exception valid indices for array of size n: 0 to n-1 any acces to other indices causes an error Array size can’t be changed after array is created To expand array, we need to create a new array, copy old array contents, then point array variable to new array Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 12

Arrays versus ArrayList Maximum size For arrays, need to establish establish this quantity upon creation For ArrayLists, underlying array grows and shrinks as needed Types and casting Arrays are “typed”; i.e., programmer indicates what kind of elements are allowed in the collection ArrayLists take any Object. Need to cast upon invoking the get() method. Need to use a wrapper class when storing primitives Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 13

Multi-dimensional Arrays A natural extension of simple (1D) arrays 2D declaration: char[][] grid; think “array of arrays” Array creation grid = new char[10][20]; // 10 rows, 20 columns Another way grid = new char[10][]; // creates array of 10 char[]’s for (i = 0; i < 10; i++) { grid[i] = new char[20]; // creates a size-20 array } This way allows for varying row sizes Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 14

Visualizing 2D Arrays char[][] null C Declare: char[][] grid; 1 2 3 4 char[]-type references char[][] null C Declare: char[][] grid; Create array of rows: grid = new char[5][]; Create rows: for ( i = 0; i < 5; i++ ) { grid[i] = new char[3]; } Use objects: e.g., grid[3][2] = ‘C’ Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 15

Using 2D Arrays To refer to individual element, use two indices e.g., grid[5][18] = ‘X’; Using only one index refers to a single dimensional array e.g., grid[5] refers to row 5 grid[5].length is the length of row 5 (in this case, it’s 20) The array variable by itself refers to the top-level array (i.e., the array of rows) grid.length is the length of the array of rows (i.e., it’s the number of rows) Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 16

Sorting Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 17

Sorting Goal: reorder array elements to be in a desired order Comparing Strings s1.compareTo( s2 ) – returns an int: 0 if s1 and s2 have same content > 0 if s1 > s2 (e.g., “Homer” > “Bart”) < 0 if s1 < s2 (e.g., “Lisa” < “Maggie” ) Note: Uses Unicode values, so all Capital letters are considered less than lowercase letters (e.g., “G” < “a”) Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 18

Bubble Sort Starting from beginning, swap adjacent pairs, putting greater value to right At the end of one pass, greatest value will “bubble” to rightmost position Repeat for next-to-greatest value, etc. Q W E R T Q W E R T Q E W R T Q E R W T Q E R T W Greatest Value is now at rightmost position Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 19

Bubble Sort end-1 end 1 2 3 4 Q W E R T First loop goes from i=0 to 3, 1 2 3 4 public void sort( String[] a ) { for ( int end = a.length-1; end > 0; end--) for ( int i = 0; i < end; i++ ) if ( a[i].compareTo( a[i+1] ) > 0 ) { // swap String temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; } Q W E R T First loop goes from i=0 to 3, repeat for i=0 to 2, i=0 to 1, i=0 Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 20

Sorting Objects assuming GradeRecord has public void sort( GradeRecord[] a ) { for ( int end = a.length-1; end > 0; end--) for (int i = 0; i < end; i++) if ( a[i].getName().compareTo( a[i+1].getName() ) > 0 ) { // swap the whole object (not just the Strings) GradeRecord temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; } assuming GradeRecord has a getName() method that returns a String Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 21

Performance of Sorting How long does BubbleSort take? N-1 turns + N-2 + N-3, etc. Roughly N2 for N items We say O(N2) time (“order N2” time) Not very efficient for large N There are faster sort algorithms Fastest comparison-based sort is O(N log N) Copyright © 2003, by the authors of these slides, and Ateneo de Manila University. All rights reserved L10: Arrays Slide 22