(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.

Slides:



Advertisements
Similar presentations
Two Dimensional Arrays and ArrayList
Advertisements

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.
Introduction to Programming Lecture 39. Copy Constructor.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture22.
Engineering Problem Solving with C++ An Object Based Approach Chapter 7 Two-Dimensional Arrays and Matrices.
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.
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.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
ECE122 L13: Arrays of Objects March 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 13 Arrays of Objects.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
One Dimensional Arrays. Declaring references to array objects How would you declare a variable somearray that is an array of ints? int[] somearray;
Lecture 3. 2 Introduction Java is a true OO language -the underlying structure of all Java programs is classes. Everything must be encapsulated in a class.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 04.
SEEM3460 Tutorial Arrays in Java. Arrays in Java Initialization array1 = new SomeClass[ARRAY_SIZE]; array1 = {element1, element2, …}; Object as elements.
Arrays- Part 2 Spring 2013Programming and Data Structure1.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
More Methods and Arrays Material from Chapters 5 to 7 that we didn’t cover in 1226.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Arrays Pepper. What is an Array A box that holds many of the exact same type in mini-boxes A number points to the mini-box The number starts at 0 String.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
Week 2. Functions: int max3(int num1, int num2, int num3) {int result; result = max(max(num1,num2),num3); return result; } //max3.
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Section 4 Boxing classes Boxing classes Array initialization Array initialization Lists: recursion and iteration Lists: recursion and iteration.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
POINTERS Introduction to Systems Programming - COMP 1002, 1402.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Variables and memory addresses
Arrays  an array of 5 ints is filled with 3,2,4,1,7 int a[5] = {3,2,4,1,7}; // note squiggly brackets  an array of 3 floats is filled with 3.2,1.4,0;
Lecture 7: Arrays Tami Meredith. Roadmap Variables Arrays Array indices & Using arrays Array size versus length Two dimensional arrays Sorting an array.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
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];
Two-Dimensional Arrays and Matrices ELEC 206 Computer Applications for Electrical Engineers.
Two Dimensional Arrays. Students will be able to: code algorithms to solve two- dimensional array problems. use 2-D arrays in programs. pass two-use 2-D.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Arrays Pepper. What is an Array A box that holds many of the exact same type in mini-boxes A number points to the mini-box The number starts at 0 String.
CS 201 Tarik Booker California State University, Los Angeles.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
1 Multidimensional Arrays Chapter 13 2 The plural of mongoose starts with a "p" Initializing a multidimensional array Processing by.
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 10.
1 Tirgul 11: Recursion & Backtracking. 2 Elements of a recursive solution (Reminder) A base case that is so simple we need no computation to solve it.
Values vs. References Lecture 13.
ECE Application Programming
Start Simple: Class for rectangle: Class for bank account?
C Passing arrays to a Function
Given the code to the left:
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
CS2011 Introduction to Programming I Arrays (I)
Lecture 13: Two-Dimensional Arrays
Lecture 12: 2D Arrays AP Computer Science Principles
What about multi-dimensional arrays?
Multi-Dimensional Arrays
Arrays an array of 5 ints is filled with 3,2,4,1,7
Linked Lists.
Introduction to Pointers
Presentation transcript:

(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 having its own unique address. (different spaces in memory do different things) Circle x; x is now a “null pointer” Meaning x can hold an address for a space in memory that is where a Circle will be, but currently it’s just empty x = new Circle(); Makes a space for a circle object in memory memory space holds radius, circumference, area fields Holds getRadius() method, SetValues() method,etc. x now holds the address of this new circle in memory. It “points to” the circle in memory

Matrices: Making Arrays of Arrays 1. double[][] mat = new double[5][]; What have I just made? - an array of 5 addresses (that will eventually point to arrays of doubles). If I can’t do this: 2.double[][] mat = new double [][]? Why can I do #1?

To create an array of arrays: You can do: double[][] mat = {{3.2,4.1,2.5},{7.1,8.2,9.3}}; Or double[][] mat = new double[3][]; mat[1] = new double[] {3.1,2.4}; Or double[][] mat = new double[3][]; double[] arr = {7.2,3.1,2.4}; mat[2] = arr; Or double[][] mat; mat = new double[][] {{3.2,4.1,2.5},{7.1,8.2,9.3}};

int[] arrfield = {3,7,2,4,1,5}; int[][] matfield= {{3,7,2,4},{1,5,8,5},{3,2,4,1}}; 1.Create a class with two fields: arrfield and matfield. Create a constructor that initializes them as above: 2.Write a method in the class that prints out each value in the arrfield (using a loop); 3.Write a method that generates a random number between 0 and 20, then creates an array that long filled with random numbers between 0 and 100. It sets the arrfield to this new array: 4.Write a method that prints out every value inside each array in the matfield field: 5.Write a method that generates 2 random numbers, x and y. It then creates a matrix of x arrays, each y elements long. It then fills each array in the matrix with random numbers. It sets the matfield to this new matrix. 6.Write a method that generates a random number x. It creates a matrix of x arrays of ints. Then, for each array, it generates a new random number and creates a new array of ints of that length. It fills that array with random numbers, and places it in the original matrix. (In other words, we’re now creating a randomly sized array of randomly sized arrays of ints, with each array filled with random numbers) It then sets the matfield to this new matrix Extra: Sudoku class? Solve the board? What methods in class? Generate random numbers: At the top: import java.util.Random; Then inside the method create a new Random object: Random randvar = new Random(); Then access the randvar object’s nextInt() method, which generates a random number up to (but not including) 100 (or the number you put in as a parameter: int x = randvar.nextInt(100); Also: nextBoolean() nextDouble() nextFloat() Only nextInt takes a value

Problem 1: Create a class with two fields: arrfield and matfield. Create a constructor that initializes them: // one likely solution public class ArrayClass { private int[] arrfield; private int[][] matfield; public ArrayClass() { //could also pass array and matrix in as parameters arrfield = new int[] {3,7,2,4,1,5}; matfield = new int[][] {{3,7,2,4},{1,5,8,5},{3,2,4,1}}; }

Problem 2: Write a method that prints out each value in the arrfield (using a loop) … public void printarr(){ for (int i = 0; i < arrfield.length; i++) { System.out.println(arrfield[i]+ " "); } // Or for (int x:arrfield) { System.out.println(x + " "); }

Problem 3: Write a method that generates a random number between 0 and 20, then creates an array that long filled with random numbers between 0 and 100. It sets the arrfield to this new array: import java.util.Random; public class ArrayClass { private int[] arrfield; private int[][] matfield; public ArrayClass() { arrfield = new int[] {3,7,2,4,1,5}; matfield = new int[][] {{3,7,2,4},{1,5,8,5},{3,2,4,1}}; } public void makearr(){ Random randvar = new Random(); int len = randvar.nextInt(20); int[] arr = new int[len]; for (int i = 0; i < arr.len; i++) { arr[i] = randvar.nextInt(100); } arrfield = arr; // what happens to {3,7,2,4,1,5} (the old array?) }

Problem 4: Write a method that prints out every value inside each array in the matfield field: … public void printmat(){ for (int i = 0; i < matfield.length; i++) { //what does matfield.length give you the length of (specifically)? //what does this loop (above) take you through (specifically)? for (int j = 0; j < matfield[i].length; j++ ) { System.out.print(matfield[i][j]+ " "); } System.out.println(); } // Or for (int[] arr:matfield) { for (int ele: arr) { System.out.print (ele + " "); } System.out.println(); }

Problem 5: Write a method that generates 2 random numbers, x and y. It then creates a matrix of x arrays, each y elements long. Now fill the array with random numbers. import java.util.Random; public class ArrayClass { private int[] arrfield; private int[][] matfield; public ArrayClass() { arrfield = new int[] {3,7,2,4,1,5}; matfield = new int[][] {{3,7,2,4},{1,5,8,5},{3,2,4,1}}; } public void makematrix(){ Random randvar = new Random(); int rows = randvar.nextInt(20); int cols = randvar.nextInt(20); int[][] mat = new int[rows][cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++ ) { mat[i][j] = randvar.nextInt(100); } matfield = mat; // so exactly what does this do? }

Problem 6: Write a method that generates a random number x. It then creates a matrix of x arrays. Then, for each array, it generates a new random number and creates a new array that length. It fills that array with random numbers, then places that in the matrix import java.util.Random; … public void makematrix(){ // how is this method different from the last one? // Why is it different in this way? Random randvar = new Random(); int rows = randvar.nextInt(20); int[][] mat = new int[rows][]; for (int i = 0; i < rows; i++) { int cols = randvar.nextInt(20); int[] arr = new int[cols]; for (int j = 0; j < cols; j++ ) { arr[j] = randvar.nextInt(100); } mat[i] = arr; } matfield = mat; }