 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 Array and Collections.
Advertisements

Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
Introduction to Computers and Programming Lecture 17: Arrays (cont) Professor: Evan Korth New York University.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Building Java Programs Chapter 7.5
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
Java Unit 9: Arrays Declaring and Processing Arrays.
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.
Chapter 7 Arrays and Vectors Introducing Arrays Introducing Arrays Declaring Arrays, Creating Arrays, and Initializing Arrays Declaring Arrays, Creating.
Arrays and Strings Introducing Arrays Declaring Arrays Creating Arrays Initializing Arrays Array of Objects Copying Arrays Multidimensional Arrays Command-Line.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays. Arrays in Java  Arrays in Java are objects.  Like all objects are created with the new keyword.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Searching Arrays Searching.
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.
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.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays Lecture.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
Liang, Introduction to Java Programming1 Arrays Gang Qian Department of Computer Science University of Central Oklahoma.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
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.
ARRAYS Multidimensional realities Image courtesy of
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
CS 201 Tarik Booker California State University, Los Angeles.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 7 Multidimensional 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.
1 Chapter 7 Multidimensional Arrays. 2 Motivations You can use a two-dimensional array to represent a matrix or a table.
Chapter 8 Multidimensional Arrays
Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Motivations Thus far, you have used one-dimensional arrays to model linear collections of elements. You can use a two-dimensional array to represent a.
Chapter 6 Arrays.
Chapter 8 Multidimensional Arrays
Chapter 6 Arrays Solution Opening Problem
Chapter 8 Multidimensional Arrays
Chapter 8 Multi-Dimensional Arrays
Introducing Arrays Array is a data structure that represents a collection of the same types of data.
Chapter 7 Multidimensional Arrays
Chapter 5 Arrays Introducing Arrays
Chapter 8 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Single-Dimensional Arrays chapter6
Chapter 8 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Outline Declaring and Using Arrays Arrays of Objects
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Presentation transcript:

 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting Methods

Array is a data structure that represents a collection of the same types of data. An Array of 10 Elements of type double

Example: double[] myArray; Example: double myArray[];

Example: myArray = new double[10]; myArray[0] references the first element in the array. myArray[9] references the last element in the array.

double[] myArray = new double[10];

 Once an array is created, its size is fixed. It cannot be changed. You can find its size using arrayVariable.length

 Using a loop: for (int i = 0; i < myList.length; i++) myArray[i] = i;  Declaring, creating, initializing in one step: double[] myArray = {1.9, 2.9, 3.4, 3.5}; This shorthand syntax must be in one statement.

double[] myArray = {1.9, 2.9, 3.4, 3.5}; This shorthand notation is equivalent to the following statements: double[] myArray = new double[4]; myArray[0] = 1.9; myArray[1] = 2.9; myArray[2] = 3.4; myArray[3] = 3.5;

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

 Arrays studied so far have one dimension … length  It is also possible to define arrays with more than one dimension  Two dimensional arrays can store values arranged in rows and columns  Three dimensional arrays can store values arranged in rows, columns, and ranks (or pages)

Declaring Variables of Multidimensional Arrays and Creating Multidimensional Arrays int[][] matrix = new int[10][10]; or int matrix[][] = new int[10][10]; matrix[0][0] = 3; for (int i=0; i<matrix.length; i++) for (int j=0; j<matrix[i].length; j++) { matrix[i][j] = (int)(Math.random()*1000); } double[][] x;

You can also use a shorthand notation to declare, create and initialize a two-dimensional array. For example, int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} }; This is equivalent to the following statements: int[][] array = new int[4][3]; array[0][0] = 1; array[0][1] = 2; array[0][2] = 3; array[1][0] = 4; array[1][1] = 5; array[1][2] = 6; array[2][0] = 7; array[2][1] = 8; array[2][2] = 9; array[3][0] = 10; array[3][1] = 11; array[3][2] = 12;

int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} }; array.length array[0].length array[1].length array[2].length

Each row in a two-dimensional array is itself an array. So, the rows can have different lengths. Such an array is known as a ragged array. For example, int[][] matrix = { {1, 2, 3, 4, 5}, {2, 3, 4, 5}, {3, 4, 5}, {4, 5}, {5} };

 Declare a two dimenstional arrays with 5 row and 5 where each row hold 10 integers.

 class ArrayExample2  {  public static void main (String[] args)  {  int[][] table = new int[3][4];  int row, col;   for (row = 0; row < table.length; row++)  {  for (col = 0; col < table[row].length; col++)  {  table[row][col] = row + col;  }

 class ArrayExample  {  public static void main (String[] args)  {  int[ ][ ] table = new int[3][4];  int row, col;  for (row = 0; row < 3; row++)  {  for (col = 0; col < 4; col++)  {  table[row][col] = row + col;  }  for (int row = 0; row < 3; row++)  {  for (int col = 0; col < 4; col++)  {  System.out.print(table[row][col]);  }  System.out.println();  }

 Multi-dimensional arrays  Length of a multidimensional array  Manupilating multidimensional array.