Chapter 6 Arrays.

Slides:



Advertisements
Similar presentations
Arrays part 3 Multidimensional arrays, odds & ends.
Advertisements

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.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
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.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
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.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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.
Arrays Chapter 7.
CMSC 202 ArrayList Aug 9, 2007.
Pointers and Dynamic Arrays
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Arrays in Classes and Methods
Arrays 3/4 By Pius Nyaanga.
Static Members and Methods
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Parameter Lists & Command Line Arguments
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Polymorphism 2nd Lecture
Polymorphism 2nd Lecture
Chapter 4 Linked Lists.
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
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.
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.
CMSC 202 Static Methods.
CMSC 202 Composition.
Building Java Programs Chapter 7
Object Oriented Programming COP3330 / CGS5409
CSC 113 Tutorial QUIZ I.
Polymorphism 2nd Lecture
CMSC 202 ArrayList Aug 9, 2007.
Classes and Objects 5th Lecture
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Defining methods and more arrays
Chapter 7 Part 2 Edited by JJ Shepherd
CMSC 202 ArrayList Aug 9, 2007.
Java Classes and Objects 4th Lecture
CMSC 202 Generics.
1D Arrays and Lots of Brackets
Classes and Objects Static Methods
CSS161: Fundamentals of Computing
Classes, Objects and Methods
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Classes and Objects Reusing Classes with Composition
Chapter 4 Constructors Section 4.4
Multidimensional Arrays Section 6.4
Arrays 3/4 June 3, 2019 ICS102: The course.
CMSC 202 Exceptions.
Classes and Objects Object Creation
Programming with Arrays 1
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Chapter 6 Arrays

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 © 2006 Pearson Addison-Wesley. All rights reserved

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 © 2006 Pearson Addison-Wesley. All rights reserved

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! © 2006 Pearson Addison-Wesley. All rights reserved

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; } © 2006 Pearson Addison-Wesley. All rights reserved

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 © 2006 Pearson Addison-Wesley. All rights reserved

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 © 2006 Pearson Addison-Wesley. All rights reserved

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 © 2006 Pearson Addison-Wesley. All rights reserved

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 © 2006 Pearson Addison-Wesley. All rights reserved

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 } © 2006 Pearson Addison-Wesley. All rights reserved

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; } © 2006 Pearson Addison-Wesley. All rights reserved

Example © 2006 Pearson Addison-Wesley. All rights reserved

© 2006 Pearson Addison-Wesley. All rights reserved

© 2006 Pearson Addison-Wesley. All rights reserved

© 2006 Pearson Addison-Wesley. All rights reserved