One Dimensional Arrays. Declaring references to array objects How would you declare a variable somearray that is an array of ints? int[] somearray;

Slides:



Advertisements
Similar presentations
Ch 7 Arrays – The 1 st Data Structure : How Store Data ARRAYS: ARRAYS: Table of same type elements (Objects or Primitives). Table of same type elements.
Advertisements

Two-Dimensional Arrays Chapter What is a two-dimensional array? A two-dimensional array has “rows” and “columns,” and can be thought of as a series.
Warm up Determine the value stored in each of the following: – gradeTable[ 0 ][ 0 ] – gradeTable[ 1 ][ 1 ] – gradeTable[ 3 ][ 4 ] – gradeTable[ 5 ][ 2.
Arrays and Matrices CSE, POSTECH. 2 2 Introduction Data is often available in tabular form Tabular data is often represented in arrays Matrix is an example.
Matrices or 2-Dimensional Arrays or Arrays of Arrays.
Arrays.
Arrays. 1D Array Representation In C 1-dimensional array x = [a, b, c, d] map into contiguous memory locations Memory abcd start location(x[i]) = start.
Engineering Problem Solving with C++ An Object Based Approach Chapter 7 Two-Dimensional Arrays and Matrices.
Lecture 05 - Arrays. Introduction useful and powerful aggregate data structure Arrays allow us to store arbitrary sized sequences of primitive values.
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.
CMPUT 101 Lab #6 October 29, :00 – 17:00. Array in C/C++ Array is a structure type variable. One dimension of array int: int num[3]; There are.
Building Java Programs Chapter 7.5
CS305j Introduction to Computing Two Dimensional Arrays 1 Topic 22 Two Dimensional Arrays "Computer Science is a science of abstraction -creating the right.
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.
Determinants. Determinant - a square array of numbers or variables enclosed between parallel vertical bars. **To find a determinant you must have a SQUARE.
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Section 3.6 – Solving Systems Using Matrices
2 dimensional arrays Steven Wood ©2005. Arrays dimensions Java allows arrays with many subscripts 2-D examples Chess board Excel spreadsheet.
Chapter 8 Arrays and Strings
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
Chapter 6- Arrays. Overview n What are arrays? n Declaring/initializing arrays. n Using arrays. n Arrays details. n Using arrays with classes/ in classes.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Topic 26 Two Dimensional Arrays "Computer Science is a science of abstraction -creating the right model for a problem and devising the appropriate mechanizable.
(Quickly) More on Null references: A null reference is a pointer to nothing. Pointers = space for address in memory Think of RAM, with each space in memory.
ITI 1120 Lab #9 Slides by: Diana Inkpen and Alan Williams.
Arrays and Strings. Why? Consider a class of 30 students, each has a score for hw1  Do we want to have 30 variables with different names?  Would it.
3.6 Solving Systems Using Matrices You can use a matrix to represent and solve a system of equations without writing the variables. A matrix is a rectangular.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int.
Array.  ARRAYS ALLOW US TO STORE ELEMENTS OF SINGLE DATA TYPE CONTAGUISLY IN MEMORY  EACH ELEMENT IS ASSOCIATED WITH AN INDEX OR LOCATION  WE CAN ACCESS.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
C# E1 CSC 298 Arrays in C#. C# E2 1D arrays  A collection of objects of the same type  array of integers of size 10 int[] a = new int[10];  The size.
1. Define an array 1 Create reference arrays of objects in Java program 2 Initialize elements of arrays 3 Pass array to methods 4 Return array to methods.
Arrays.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Multidimensional Arrays Vectors of Vectors When One Is Not Enough.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
Table of Contents Matrices - Definition and Notation A matrix is a rectangular array of numbers. Consider the following matrix: Matrix B has 3 rows and.
ARRAYS Multidimensional realities Image courtesy of
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
CS 201 Tarik Booker California State University, Los Angeles.
Chapter 4 Section 1 Organizing Data into Matrices.
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
C++ Arrays SarMag Trimester 31 C++ Arrays. C++ Arrays SarMag Trimester 32 C++ Arrays An array is a consecutive group of memory locations. Each group is.
Lecture 18: Nested Loops and Two-Dimensional Arrays
Java for Beginners Level 6 University Greenwich Computing At School
Two-Dimensional Arrays
1.5 Matricies.
Topic Dimensional Arrays
Two Dimensional Arrays
Two Dimensional Array Mr. Jacobs.
ECE Application Programming
Nested Loop Review and Two-Dimensional Arrays
Topic 26 Two Dimensional Arrays
Lecture 6 2d Arrays Richard Gesick.
The Matrix A b a d e a a a a a a a A b a d e a a a a a a a
Lecture 13: Two-Dimensional Arrays
Lecture 4 2d Arrays CSE /26/2018.
Lecture 12: 2D Arrays AP Computer Science Principles
Multi-Dimensional Arrays
Arrays.
Matrix A matrix is a rectangular arrangement of numbers in rows and columns Each number in a matrix is called an Element. The dimensions of a matrix are.
Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7.
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Presentation transcript:

One Dimensional Arrays

Declaring references to array objects How would you declare a variable somearray that is an array of ints? int[] somearray;

Creating array objects How do you create an array of 12 BankAccount objects and refer to it with the variable theBank? BankAccount[] theBank = new BankAccount[12] ;

Creating 1-D array using initializer list This shortcut only works for local variables NOT fields. It is very usual to fill an array with data without having to loop. int[] nums = {1,3,6,4,9,11}; String arr = {“apple”, ”orange”, “pear”};

Looping through 1-D arrays f or (int i = 0; I < theArray.length; i++) System.out.println(theArray[i]); or for( int n : theArray) System.out.println(n); NOTE: for each loops CANNOT be used to modify the contents of arrays for( int n : theArray) n = 0;//NOT ALLOWED

Two Dimensional Arrays

2-D arrays are matrices

Declaring references to 2-D array objects int[][] somearray; String[][] crossword; Actor[][] grid;

Creating 2-D array objects double[][] matrix = new double[7][10]; String[][] table = new String[10][10];

Creating a 2-D array using an initializer list This shortcut only works for local variables NOT fields. Very useful in test classes to get data into an array without looping int[][] nums ={ {1,3,6}, {4,9,11} }; Creates a 2D array as follows

Loop through a 2-D array Assume a 2-D array of ints called mat has already been created and filled with values – for (int r = 0; r < mat.length; r++) for(int c = 0; c < mat[0].length; c++) System.out.print(mat[r][c]); This moves left to right by row and goes down each column from top to bottom. Row major order – like reading a book. This is the preferred way to loop through a 2-D array in java. Use mat[r] for an array with rows of different lengths (not rectangular array)

2D array is an Array of Array Objects Consider looping through a 2D array using for each loops: Assume a 2-D array of ints called mat has already been created and filled with values – for(int arr[] : mat) for(int value : arr) System.out.println(value);

Some practice in a BlueJ test class 1.Create a 5 by 7 2-D Arrays of ints and loop through it and fill with the number 3. 2.Create an 3 by 4 2-D array of String objects and fill it using an initializer list. Loop through and print them in row major order (as you read a book). 3.Using the same 2-D array of String objects from #2, loop through and print them in column major order (top to bottom THEN left to right) 4.Create a 10 by 10 2-D array of anything and and fill it using an initializer list. Write 2 separate loops to visit each element of the diagonals and print them.

Most important info. for 2 Arrays Creating a 2D array: int[][] array = new int[10][20]; Row major order means [rows] then [cols] Remember RC cola! Rows and cols numbered starting at 0! To get num rows: arrays.length To get num cols: arrays[0].length (rectangular 2D array) To loop through in row major order for (int r = 0; r < mat.length; r++) for(int c = 0; c < mat[r].length; c++) System.out.print(mat[r][c]);