JAVA Arrays. Objectives Be able to declare and initialize arrays Be able to conceptualize (draw) how arrays are represented in computer memory. Be able.

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

Arrays.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
For use of IST410 Students only Arrays-1 Arrays. For use of IST410 Students only Arrays-2 Objectives l Declaring arrays l Instantiating arrays l Using.
Chapter 7 – Arrays.
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
ECE122 L13: Arrays of Objects March 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 13 Arrays of Objects.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
CS1061 C Programmuing Lecture 12 Arrays A. O’Riordan, 2004.
More Arrays Length, constants, and arrays of arrays By Greg Butler.
Java Unit 9: Arrays Declaring and Processing Arrays.
© Calvin College, All our knowledge brings us nearer to our ignorance, All our ignorance brings us nearer to death, But nearness to death, no nearer.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 8 Arrays and Strings
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
Topics to be covered  Introduction to array Introduction to array  Types of array Types of array  One dimensional array One dimensional array  Declaration.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
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 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.
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.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 8: Arrays.
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.
SINGLE-DIMENSION ARRAYS. Data Structures It is a collection of scalar data types that are all referenced or accessed through one identifier – scalar type.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
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.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
IN THE NAME OF ALLAH WHO IS THE MOST BENEFICENT AND MOST MERCIFUL.
Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
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.
Arrays.
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
Arrays.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
Arrays Chapter 7. 2 Declaring and Creating Arrays Recall that an array is a collection of elements all of the _____________ Array objects in Java must.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
ARRAYS Multidimensional realities Image courtesy of
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
Asserting Java © Rick Mercer Chapter 7 The Java Array Object.
© Rick Mercer Chapter 7 The Java Array Object.  Some variables store precisely one value: a double stores one floating-point number a double stores one.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
1 Why do we need arrays? Problem - Input 5 test scores => int test1,test2,test3,test4,test5 100 test scores? 10,000 employees? A structured data type is.
Arrays Chapter 7.
Computer Programming BCT 1113
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
Data Structures (CS212D) Week # 2: Arrays.
Arrays Chapter 7.
Arrays Week 2.
Arrays.
Presentation transcript:

JAVA Arrays

Objectives Be able to declare and initialize arrays Be able to conceptualize (draw) how arrays are represented in computer memory. Be able to process arrays (especially using for loops) Understand when to use an array

3 Representing Lists of Objects Frequently, applications must store lists of objects of the same type. Variables represent one object at a time so a list would require separate variables: String country1, country2, country3; int population1, population2, population3; We can represent lists of objects using arrays.

Array Definition Pattern 4 ElementType[] arrayName; or ElementType[] arrayName = new ElementType[length]; or ElementType[] arrayName = arrayInitializer;  ElementType is any type (including an array type);  arrayName is the handle for the array object being defined – if there is no assignment clause in the statement, the handle is set to null ;  length is an expression specifying the number of elements in the array;  arrayInitializer is a list of literals of type ElementType, enclosed in curly braces ( { } ).

5 double[] array1; final int SIZE = 4; int[] array2 = new int[SIZE]; String[] array3 = { "Bashful", "Doc" }; array2[0] [1] [2] [3] ? ? array1 [0] [1]array3 “Bashful” “Doc” Array Definitions

6 String[] anArray = new String[2]; anArray[0] = "Grumpy"; anArray[1] = "Happy"; println(anArray[1]); [0] [1]anArray “Grumpy” “Happy” Array Subscripts

7 String[] anArray = new String[2]; anArray[0] = "Grumpy"; anArray[1] = "Happy"; for (int i = 0; i < anArray.length; i++) { println(anArray[i]); } [0] [1]anArray “Grumpy” “Happy” Working with Arrays

Example: Population by Country 8 String[] countries = { "Belize", "Costa Rica", "El Salvador", "Guatemala", "Honduras", "Nicaragua", "Panama" }; int[] populations = { , , , , , , }; for (int i = 0; i < countries.length; i++) { println(countries[i] + ": " + populations[i]); }

9 public static int computeTotal(int[] values) { int result = 0; for (int i = 0; i < values.length; i++) { result += values[i]; } return result; } Arrays as Parameters

10 public static void main(String[] args) { String[] sa = {"Grumpy", "Happy"}; changeStringArray(sa); System.out.println(sa[0] + ” ” + sa[1]); } public static void changeStringArray (String[] stringArray) { sa[0] = "Dopey"; sa[1] = "Sleepy"; System.out.println(sa[0] + ” ” + sa[1]); } Reference Values as Parameters

11 public static void main(String[] args) { String[] sa = {"Grumpy", "Happy"}; changeStringArray(sa); System.out.println(sa[0] + ” ” + sa[1]); } public static void changeStringArray (String[] sA1) { sA1[0] = "Dopey"; sA1[1] = "Sleepy"; System.out.println(sA1[0] + ” ” + sA1[1]); } Ref. Values as Parameters (corrected)

12 Exercises

Search Linear Search: 1. Receive a non-null list of values and a target value. 2. Loop for each element in list a. If value equals list[i] then i. Return true 3. Return false.

Binary Search 1. Receive a non-null, sorted list of values and a target value. 2. If list is null a. Return Set first = 0 and last = length of the list Loop while first <= last a. Set middle to the integer quotient (first + last) / 2. b. If value < list[middle] then i. Set last = middle – 1; c. else if value > list[middle] then i. Set first = middle + 1; d. else i. Return middle; 5. Return -1.

Multi-Dimensional Data Some data sets cannot be represented with single- dimensional arrays. Examples: Matrixes, sudoku puzzles, tictactoe games, chess, checkers, etc. Spreadsheets are generally two dimensional. Databases are generally X dimensional where X > 2. 15

16 Multi-Dimensional Arrays Some data collections, like the Sudoku grid can be viewed as multi-dimensional data. Declaring 2-D arrays: type[][] identifier = arrayExpression; Constructing 2-D arrays: new type[totalRows][totalColumns] Accessing 2-D array elements: identifier[someRow][someColumn] This can be generalized to more dimensions.