Arrays and Strings Introducing Arrays Declaring Arrays Creating Arrays Initializing Arrays Array of Objects Copying Arrays Multidimensional Arrays Command-Line.

Slides:



Advertisements
Similar presentations
1 Arrays, Strings and Collections [1] Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software Engineering.
Advertisements

Arrays.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
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.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
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.
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.
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 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Java Unit 9: Arrays Declaring and Processing Arrays.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
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.
Chapter 7 Arrays and Vectors Introducing Arrays Introducing Arrays Declaring Arrays, Creating Arrays, and Initializing Arrays Declaring Arrays, Creating.
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.
1 Chapter 8 Multi-Dimensional Arrays. 2 1-Dimentional and 2-Dimentional Arrays In the previous chapter we used 1-dimensional arrays to model linear collections.
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.
Liang, Introduction to Java Programming1 Arrays Gang Qian Department of Computer Science University of Central Oklahoma.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
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.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Arrays.
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.
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 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 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.
Chapter 9 Introduction to Arrays Fundamentals of Java.
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.
Arrays Chapter 7.
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.
Java How to Program, Late Objects Version, 10/e
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
Introduction To Programming Information Technology , 1’st Semester
Java Arrays & Strings.
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Arrays Chapter 7.
Chapter 6 Arrays.
Single-Dimensional Arrays chapter6
Single-Dimensional Arrays
Chapter 5 Arrays.
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Presentation transcript:

Arrays and Strings Introducing Arrays Declaring Arrays Creating Arrays Initializing Arrays Array of Objects Copying Arrays Multidimensional Arrays Command-Line Parameters

Introducing Arrays double[] myList = new double[10] In computer science, an 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; Example: int[] myList; datatype arrayname[]; Example: int myList[];

Creating Arrays arrayName = new datatype[arraySize]; Example: myList = new double[10]; An array is considered to be an object. Thus: myList is really a reference to 10 doubles and a field called length that contains the array’s size. For example, myList.length contains 10.

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

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

Enhanced for statement Used to iterate through the elements of an array or collection without using a counter. Form: for (parameter: arrayName) statement; Parameter has two parts – a type and an identifier ArrayName is the array through which to iterate.

Enhanced for Example: total = 0; for (int count=0; count < array.length; count ++) total += array [count]; Is the same as: total = 0; for(int number: array) total += number; number is actually taking on array[0], array[1], etc.

Using Arrays in a Gradebook Objective: Use an array for a grade book used by a professor to store and analyze a set of student grades. Figure 7.14 in Java book (page 317…) and Figure 7.15 on page 321.

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

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];

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

Multidimensional Arrays Multidimensional arrays with two dimensions are often used to represent tables of values consisting of information arranged in rows and columns called a two- dimensional array. Example: int[][] matrix = new int[10][10]; or int matrix[][] = new int[10][10]; for (int i=0; i<matrix.length; i++) for (int j=0; j<matrix[i].length; j++) { matrix[i][j] = (int)(Math.random()*1000); }

Nested array initializers A 2D array can be initialized when it is declared. Example: int b[][] = { {1, 2}, {3, 4}, {5, 6}}; creates the following array containing the values shown

How does Java treat a 2D array? A 2D array is thought of as an array of arrays. For example if b is an array with 3 rows and 2 columns, it is considered to be a one dimensional array with 3 objects. Each object is an array with 2 elements. b[0]  {1, 2} b[1]  {3, 4} b[2]  {5, 6}

2D arrays with rows of varying lengths The way Java represents 2D arrays makes them very flexible. Lengths of rows are not required to be the same: int b[][] = {{1, 2}, {3, 4, 5, 6}}; makes b look like: b[0]  {1, 2} b[1]  {3, 4, 5, 6}

2D Array creation examples int a[][] = new int[4][2]; int a[][]; a = new int [4][2]; int c[][]; c = new int[3][]; //create 3 rows c[0] = new int[3];//create 3 columns for row 0 C[1] = new int[2];//create 2 columns for row 1 C[2] = new int[4];//create 4 columns for row 2

length field for 2D arrays For a 2D array declared as: int B[][]=new int[2][3]; there are several “length” fields B.length contains 2, the number of rows B[0].length contains 3, the number of columns in row 0. B[1].length contains 3, the number of columns in row 1, etc.

Working with 2D arrays int total = 0; for (int row = 0; row < a.length; row++) { for (int column = 0; column < a[row].length; column++) total += a[row][length]; { Or for (int rows[] : a) //loop thru rows of a {//loop thru columns of the current row for (int oneValue : rows) total += oneValue; }

Example: Revisit grade book using 2D arrays Figures 7.18 and 7.19

Command-Line Parameters class TestMain { 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: 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. Java TestCommandParameters Java TestCommandParameters Java TestCommandParameters / 2 3