Arrays 3/4 June 3, 2019 ICS102: The course.

Slides:



Advertisements
Similar presentations
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Advertisements

Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Arrays part 3 Multidimensional arrays, odds & ends.
CS102--Object Oriented Programming Lecture 5: – Arrays – Sorting: Selection Sort Copyright © 2008 Xiaoyan Li.
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Chapter Eight: Arrays 1.Terms and what they mean 2.Types of arrays -One Dimensional arrays -Two Dimensional arrays -ragged arrays -Parallel arrays 3. Methods.
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
Java Unit 9: Arrays Declaring and Processing Arrays.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CSC Programming I Lecture 8 September 9, 2002.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
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.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CSI 3125, Preliminaries, page 1 Compiling the Program.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
CSE 1201 Object Oriented Programming ArrayList 1.
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
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];
Classes - Intermediate
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
Arrays Chapter 7.
CMSC 202 ArrayList Aug 9, 2007.
Topic: Classes and Objects
Test 2 Review Outline.
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Static data members Constructors and Destructors
Examples of Classes & Objects
Arrays in Classes and Methods
Arrays 3/4 By Pius Nyaanga.
Some Eclipse shortcuts
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Arrays 2/4 By Pius Nyaanga.
Foundations of Programming: Arrays
Java Arrays. Array Object An array :a container object holds a fixed number of values of a single type. The length of an array is established when the.
Constructors CMSC 202.
Java Review: Reference Types
Pointers and References
Objects First with Java CITS1001
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Peer Instruction 6 Java Arrays.
Object Oriented Programming COP3330 / CGS5409
CSC 113 Tutorial QUIZ I.
An Introduction to Java – Part I, language basics
Classes & Objects: Examples
CMSC 202 ArrayList Aug 9, 2007.
Building Java Programs
Defining methods and more arrays
Chapter 7 Part 2 Edited by JJ Shepherd
CMSC 202 ArrayList Aug 9, 2007.
int [] scores = new int [10];
OBJECT ORIENTED PROGRAMMING II LECTURE 13_2 GEORGE KOUTSOGIANNAKIS
1D Arrays and Lots of Brackets
CSS161: Fundamentals of Computing
Classes, Objects and Methods
Building Java Programs
Chapter 6 Arrays.
Classes and Objects Object Creation
Programming with Arrays 1
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Arrays 3/4 June 3, 2019 ICS102: The course

Outline Example Arguments for the Method main Methods That Return an Array Partially Filled Arrays Accessor Methods Need Not Simply Return Instance Variables Privacy Leaks with Array Instance Variables Example June 3, 2019 ICS102: The course

- Arguments for the Method main … The heading for the main method of a program has a parameter for an array of String It is usually called args by convention public static void main(String[] args) Note that since args is a parameter, it could be replaced by any other non-keyword identifier If a Java program is run without giving an argument to main, then a default empty array of strings is automatically provided June 3, 2019 ICS102: The course

… - Arguments for the Method main … Here is a program that expects three string arguments: public class SomeProgram { public static void main(String[] args) System.out.println(args[0] + " " + args[2] + args[1]); } Note that if it needed numbers, it would have to convert them from strings first June 3, 2019 ICS102: The course

… - Arguments for the Method main If a program requires that the main method be provided an array of strings argument, each element must be provided from the command line when the program is run java SomeProgram Hi ! there This will set args[0] to "Hi", args[1] to "!", and args[2] to "there“ It will also set args.length to 3 When SomeProgram is run as shown, its output will be: Hi there! June 3, 2019 ICS102: The course

- Methods That Return an Array In Java, a method may also return an array The return type is specified in the same way that an array parameter is specified public static int[] incrementArray(int[] a, int increment) { int[] temp = new int[a.length]; int i; for (i = 0; i < a.length; i++) temp[i] = a[i] + increment; return temp; } June 3, 2019 ICS102: The course

- Partially Filled Arrays … The exact size needed for an array is not always known when a program is written, or it may vary from one run of the program to another A common way to handle this is to declare the array to be of the largest size that the program could possibly need Care must then be taken to keep track of how much of the array is actually used An indexed variable that has not been given a meaningful value must never be referenced June 3, 2019 ICS102: The course

… - Partially Filled Arrays A variable can be used to keep track of how many elements are currently stored in an array For example, given the variable count, the elements of the array someArray will range from positions someArray[0] through someArray[count – 1] Note that the variable count will be used to process the partially filled array instead of someArray.length Note also that this variable (count) must be an argument to any method that manipulates the partially filled array June 3, 2019 ICS102: The course

- Accessor Methods Need Not Simply Return Instance Variables When an instance variable names an array, it is not always necessary to provide an accessor method that returns the contents of the entire array Instead, other accessor methods that return a variety of information about the array and its elements may be sufficient June 3, 2019 ICS102: The course

- Privacy Leaks with Array Instance Variables … If an accessor method does return the contents of an array, special care must be taken Just as when an accessor returns a reference to any private object public double[] getArray() { return anArray;//BAD! } The example above will result in a privacy leak June 3, 2019 ICS102: The course

… - Privacy Leaks with Array Instance Variables… The previous accessor method would simply return a reference to the array anArray itself Instead, an accessor method should return a reference to a deep copy of the private array object Below, both a and count are instance variables of the class containing the getArray method public double[] getArray() { double[] temp = new double[count]; for (int i = 0; i < count; i++) temp[i] = a[i]; return temp } June 3, 2019 ICS102: The course

… - Privacy Leaks with Array Instance Variables If a private instance variable is an array that has a class as its base type, then copies must be made of each class object in the array when the array is copied: public ClassType[] getArray() { ClassType[] temp = new ClassType[count]; for (int i = 0; i < count; i++) temp[i] = new ClassType(someArray[i]); return temp; } June 3, 2019 ICS102: The course

- Example … June 3, 2019 ICS102: The course

… - Example … June 3, 2019 ICS102: The course

… - Example … June 3, 2019 ICS102: The course

… - Example June 3, 2019 ICS102: The course

THE END June 3, 2019 ICS102: The course