Chapter 7 Arrays and Vectors Introducing Arrays Introducing Arrays Declaring Arrays, Creating Arrays, and Initializing Arrays Declaring Arrays, Creating.

Slides:



Advertisements
Similar presentations
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Advertisements

Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Introduction to Computers and Programming Lecture 17: Arrays (cont) Professor: Evan Korth New York University.
Chapter 7 Arrays.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
1 Chapter 21 Generics. 2 Objectives F To know the benefits of generics (§21.1). F To use generic classes and interfaces (§21.2). F To declare generic.
Chapter 19 Java Data Structures
INF120 Basics in JAVA Programming AUBG, COS dept, Spring 2014 Lecture 10 Title: Arrays, Part 2 Multidimensional Arrays Reference: MalikFarrell, chap 1,
1 Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
1 Chapter 7 Single-Dimensional Arrays. 2 Arrays Array is a data structure that represents a collection of the same types of data elements. A single-dimensional.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
Arrays and Strings Introducing Arrays Declaring Arrays Creating Arrays Initializing Arrays Array of Objects Copying Arrays Multidimensional Arrays Command-Line.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Programming Fundamentals I (COSC-1336), Lecture 8 (prepared after Chapter 7 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 8.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Liang, Introduction to Java Programming1 Arrays Gang Qian Department of Computer Science University of Central Oklahoma.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Generics.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Arrays.
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.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
Arrays F Introducing Arrays F Declaring Array Variables F Creating Arrays, and Initializing Arrays F Copying Arrays F Multidimensional Arrays.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 6 Arrays 1.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 7 Single-Dimensional Arrays.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Lecture 7: Arrays Michael Hsu CSULA 3 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.
Single Dimensional Arrays
Chapter 7: Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Multidimensional Arrays
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Chapter 6 Arrays.
Chapter 6 Arrays.
Chapter 6 Arrays.
Chapter 6 Arrays Solution Opening Problem
Introducing Arrays Array is a data structure that represents a collection of the same types of data.
Chapter 5 Arrays Introducing Arrays
Java Arrays & Strings.
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Chapter 6 Arrays.
Single-Dimensional Arrays chapter6
Single-Dimensional Arrays
Chapter 6 Arrays.
Chapter 5 Arrays.
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
Presentation transcript:

Chapter 7 Arrays and Vectors Introducing Arrays Introducing Arrays Declaring Arrays, Creating Arrays, and Initializing Arrays Declaring Arrays, Creating Arrays, and Initializing Arrays Array of Objects Array of Objects Copying Arrays Copying Arrays Multidimensional Arrays Multidimensional Arrays Numeric Wrapper Classes Numeric Wrapper Classes Command-Line Parameters Command-Line Parameters Creating Generic Classes Creating Generic Classes The Vector Class The Vector Class

Introducing Arrays double[] myList = new double[10] Array is a data structure that represents a collection of the same types of data. Java treats these arrays as objects. An Array of 10 Elements of type double

Declaring Arrays datatype[] arrayname; datatype[] arrayname;Example: int[] myList; datatype arrayname[]; datatype arrayname[];Example: int myList[];

Creating Arrays arrayName = new datatype[arraySize]; Example: myList = new double[10];

Declaring and Creating in One Step datatype[] arrayname = new datatype[] arrayname = new datatype[arraySize]; datatype[arraySize]; double[] myList = new double[10]; double[] myList = new double[10]; datatype arrayname[] = new datatype[arraySize]; datatype arrayname[] = new datatype[arraySize]; double myList[] = new double[10];

Initializing Arrays Using a loop: Using a loop: for (int i = 0; i < myList.length; i++) myList[i] = (double)i; Declaring, creating, initializing in one step: Declaring, creating, initializing in one step: double[] myList = {1.9, 2.9, 3.4, 3.5};

Example 7.1 Assigning Grades Objective: read student scores (int) from the keyboard, get the best score, and then assign grades based on the following scheme: Objective: read student scores (int) from the keyboard, get the best score, and then assign grades based on the following scheme:  Grade is A if score is >= best–10;  Grade is B if score is >= best–20;  Grade is C if score is >= best–30;  Grade is D if score is >= best–40;  Grade is F otherwise. AssignGrade Run

Example 7.2 Using Arrays in Sorting Objective: Use the selectionSort method to write a program that will sort a list of double floating-point numbers. Objective: Use the selectionSort method to write a program that will sort a list of double floating-point numbers. SelectionSortRun

Example 7.3 Testing Linear Search Objective: Implement and test the linear search method by creating an array of 10 elements of int type randomly and then displaying this array. Prompt the user to enter a key for testing the linear search. Objective: Implement and test the linear search method by creating an array of 10 elements of int type randomly and then displaying this array. Prompt the user to enter a key for testing the linear search. LinearSearchRun

Array of Objects Declaring and creating: Declaring and creating: Circle[] circleArray = new Circle[10]; Initializing: Initializing: for (int i=0; i<circleArray.length; i++) { circleArray[i] = new Circle(); circleArray[i] = new Circle();}

Example 7.5 Summarizing the Areas of Circles Objective: Summarize the areas of an array of circles Objective: Summarize the areas of an array of circles TotalAreaRun

Copying Arrays Using a loop: int[] sourceArray = {2, 3, 1, 5, 10}; int[] targetArray = new int[sourceArray.length]; for (int i = 0; i < sourceArrays.length; i++) targetArray[i] = sourceArray[i]; targetArray[i] = sourceArray[i];

The arraycopy Utility arraycopy(sourceArray, src_pos, targetArray, tar_pos, length); Example: System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);

Multidimensional Arrays int[][] matrix = new int[10][10]; or or int matrix[][] = new int[10][10]; for (int i=0; i<matrix.length; i++) for (int j=0; j<matrix[i].length; j++) { for (int j=0; j<matrix[i].length; j++) { matrix[i][j] = (int)(Math.random()*1000); matrix[i][j] = (int)(Math.random()*1000); }

Example 7.7 Adding and Multiplying Two Matrices Objective: Use two-dimensional arrays to create two matrices, and then add and multiple the two matrices. Objective: Use two-dimensional arrays to create two matrices, and then add and multiple the two matrices. TestMatrixOperationRun

Numeric Wrapper Classes Boolean Boolean Character Character Short Short Byte Byte Long Float Double

The Integer Class and The Double Class Constructors Constructors Class Constants MAX_VALUE, MIN_VALUE Class Constants MAX_VALUE, MIN_VALUE Conversion Methods Conversion Methods

Command-Line Parameters class TestMain { public static void main(String[] args) public static void main(String[] args) {... } } java TestMain arg0, arg1, arg2,..., argn

Processing Command-Line Parameters In the main method, get the arguments from args[0], args[1],..., args[n], which corresponds to arg0, arg1,..., argn in the command line.

Example 7.8 Using Command-Line Parameters Objective: Write a program that will perform binary operations on integers. The program receives three parameters: an operator and two integers. Objective: Write a program that will perform binary operations on integers. The program receives three parameters: an operator and two integers. Calculator Run java Calculator Run java Calculator Run java Calculator / 2 3

Example 7.9 Designing Generic Classes Objective: This example gives a generic class for matrix arithmetic. This class implements matrix addition and multiplication common for all types of matrices. Objective: This example gives a generic class for matrix arithmetic. This class implements matrix addition and multiplication common for all types of matrices. GenericMatrix

Example 7.10 Extending Abstract Classes

Example 7.10 Extending Abstract Classes, cont. Objective: This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic. Objective: This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic. TestIntegerMatrix Run TestRationalMatrix Run RationalMatrix IntegerMatrix

The Vector Class Java provides the Vector class in the java.util package, which can be used to store an unspecified number of elements. A vector can grow or shrink dynamically as needed. Java provides the Vector class in the java.util package, which can be used to store an unspecified number of elements. A vector can grow or shrink dynamically as needed.

The Vector Class, cont. To create a vector, you can use its default constructor like this: To create a vector, you can use its default constructor like this: Vector vector = new Vector(); To add an element to the vector, use the addElement method as follows: To add an element to the vector, use the addElement method as follows:vector.addElement(anObject); The element in the vector must be an object. The element in the vector must be an object. The element is appended to the vector. The element is appended to the vector.

The Vector Class, cont. To retrieve an element, use the elementAt method as follows: To retrieve an element, use the elementAt method as follows: Object anObject = vector.elementAt(0); To find the number of elements in the vector, use the size method as follows: To find the number of elements in the vector, use the size method as follows: int numOfElements = vector.size();

Example 7.10 Assigning Grades Using a Vector Objective: Rewrites Example 7.1 using a vector, instead of using an array. The program reads student scores from the keyboard, stores the score in the vector, finds the best score, and then assigns grades for all the students. Objective: Rewrites Example 7.1 using a vector, instead of using an array. The program reads student scores from the keyboard, stores the score in the vector, finds the best score, and then assigns grades for all the students. AssignGradeUsingVector Run