COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 13 – Having Fun with Arrays in Java Webpage: www.cise.ufl.edu/~mssz/JavaNM/Top-Level.html.

Slides:



Advertisements
Similar presentations
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 09 – Programming with Java Datatypes and Flow Control.
Advertisements

Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
1 2-D Arrays Overview l Why do we need Multi-dimensional array l 2-D array declaration l Accessing elements of a 2-D array l Declaration using Initializer.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Arrays CS Feb Announcements Exam 1 Grades on Blackboard Project 2 scores: end of Class Project 4, due date:20 th Feb –Snakes & Ladders Game.
Arrays. 2 The array data structure An array is an indexed sequence of components Typically, the array occupies sequential storage locations The length.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
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.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
R-1 University of Washington Computer Programming I Lecture 17: Multidimensional Arrays © 2000 UW CSE.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Chapter 8 Arrays and Strings
Building Java Programs Chapter 7.5
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
1 Sorting Arrays Chapter Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.
1 Sorting Arrays Chapter Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.
Matrices Jordi Cortadella Department of Computer Science.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Chapter 8 Arrays and Strings
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),
Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.
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.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 10 – Programming with Java Datatypes Type Casting.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
© 2004 Pearson Addison-Wesley. All rights reserved October 13, D Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor:
Multi-Dimensional Arrays Arrays that have more than one index: Example of differences between basic data types and arrays using integers: Basic integer:
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 35 – Overview of Java Web Programming Webpage:
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
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.
© 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
***** SWTJC STEM ***** Chapter 7 cg 68 What Are Arrays? An array is a simple but powerful way to organize and store large amounts of data and information.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 11 – How to Get Started on Assignment #2 Webpage:
Introduction to Programming (in C++) Multi-dimensional vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 20 – Inheritance and Polymorphism in Java Webpage:
CSC Programming for Science Lecture 28: Multi-dimensional Arrays.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays.
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 06 – Java Datatypes Webpage:
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 39 – Command Line Args & Recursion Webpage:
2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006.
Multidimensional Arrays Computer and Programming.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
Arrays and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 14 – How to Complete Assignment 2, Part III Webpage:
Arrays. Arrays are objects that help us organize large amounts of information.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
Introduction to programming in java Lecture 21 Arrays – Part 1.
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 08 – Hand-On Work: Assignment 1, Part III Webpage:
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
COP2800 – Computer Programming Using JAVA
INC 161 , CPE 100 Computer Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 13 – Having Fun with Arrays in Java Webpage:

COP2800 – Programming in JAVA Course Objectives – Basic Knowledge of Computers & Programming – Specific Knowledge of JAVA Programming – Practical Programming Projects Build Skills Today’s Class – What Can We Do With Arrays? – Applications of Arrays in Java Parameterization Ordering Integers: “Injection Sort”

Review: Java Program Structure HIGH-LEVEL VIEW JAVA Units: -Packages -Classes (Instances) -Methods -Instructions -Variables PICTURE CREDIT:

Review: Java Package Structure PICTURE CREDIT:

What Is An Array? Arrays Are Regular, periodic data structures 1-D : A vector of values (1,4,37,3) 2-D : A matrix (like mailboxes at the Post Office) Arrays Are Used For Storing Values for Regular Accessibility Sorting and Searching Regularizing Variable-Length Data

Review: 1-D Array One-Dimensional Array: Image Credit: 123codegenerator.blogspot.com ARRAY ELEMENT VALUES Array Name = “a” a[1] = 13 a[5] = 8 JAVA Arrays Are Zero-Indexed: 0, 1, …, N-1 JAVA Arrays Are Zero-Indexed: 0, 1, …, N-1

Let’s Do a 1-D Array with For Loop One-Dimensional Array: Array Name = “a” // Declare the array (1-D, 6 elements) int a[] = new int[6] ; // Put the loop index into the array for (int i = 0, i < a.length, i++) { a[i] = 2 * i; } RESULT: a = (0, 2, 4, 6, 8, 10)

Let’s Do Sorting with a 1-D Array What is Sorting? Sorting is an operation that orders values in a predetermined (e.g., ascending or descending) order. Ex: {3, 4, 0, 2, 1, 4}  (0, 1, 2, 3, 4, 4) The simplest sorting algorithm is injection sort. SORT Set Sequence

Injection Sorting with a 1-D Array One-Dimensional Array: Input Array = “a” // 1: Declare the array (1-D, 6 elements) int a[] = {3, 4, 0, 2, 1, 4}; int b[] = {0, 0, 0, 0, 0, 0}; // 2: Inject the values of a as loop indices for (int i = 0, i < a.length, i++) { b[a[i]]++; } // 3: Print sorted values (ascending order) for (int i = 0, i < b.length, i++) { if b[i] != 0 {System.out.print(i)}; } b = (1, 1, 1, 1, 2, 0)

Review: 2-D Array Two-Dimensional Array: Array Name = “a2” Image Credit: 123codegenerator.blogspot.com ARRAY ELEMENT VALUES a2[0,2] = JAVA Arrays Are Zero- Indexed: 0, 1, …, N-1 JAVA Arrays Are Zero- Indexed: 0, 1, …, N-1

Let’s Do a 2-D Array with For Loop Two-Dimensional Array: Array Name = “a2” // Declare the array (2-D, 2 rows x 4 cols) int Nrows = 2; int Ncols = 4; int a2[][] = new int[Nrows][Ncols] ; // Put the sum of loop indices into array for (int i = 0, i < Nrows, i++) { for (int j = 0, j < Nrows, j++) { a2[i][j] = i + j; } RESULT: Row 0 of a2 = (0, 1, 2, 3) Row 1 of a2 = (1, 2, 3, 4)

Case 1: the “Bins” Array (Asn-2) Two-Dimensional Array: Array Name = “Bins” // Declare array (2-D, NBins rows x 4 cols) float a2[][] = new float[NBins][3] ; Anatomy: Dimension #1 = Bin Number (from 0 to Nbins-1) Dimension #2 = Parameters: 1 – Lower Bound of Test Interval 2 – Upper Bound of Test Interval 3 – Count of Numbers in Bin

The “Bins” Array (Asn-2), cont’d Two-Dimensional Array: Array Name = “Bins” Bin Number Parameter #1 Parameter #2 Parameter #3 Lesson Learned: Arrays can be used for storing input parameters and results.

Case 2: TicTacToe Array (Asn-?) Two-Dimensional Array: Array Name = “TTT” // Declare array (2-D, 3 rows x 3 cols) char TTT[][] = new char[3][3] ; Anatomy: Dimension #1 = Row Number (from 0 to 2) Dimension #2 = Column Number (from 0 to 2) Contents of Array? char = “–” or “X” or “O”

Case 2: TicTacToe Array (Asn-?) Two-Dimensional Array: Array Name = “TTT” // Declare array (2-D, 3 rows x 3 cols) char TTT[][] = new char[3][3] ; Here’s Some Pseudocode !! Initialize Array TTT with “–” To insert an X or O at location (i,j): input_char= “X”; TTT[2][1]= input_char; - O X O X X O X - Array TTT

This Week: Arrays and Java READING ASSIGNMENT: D. Liang: Chapters 6 & 7 By now, you should have read textbook Ch. 1-8 Next Class (Friday 08 February) How to Do Assignment #2 – Part III More on Multidimensional Arrays in Java