Chapter 8 Arrays and the ArrayList Class Arrays of Objects.

Slides:



Advertisements
Similar presentations
1 Fall 2008ACS-1903 Chapter 8: Arrays 8.1 – 8.8 Introduction to Arrays Processing Array Contents Passing Arrays as Arguments to Methods Some Useful Array.
Advertisements

Java: How to Program Arrays and ArrayLists Summary Yingcai Xiao.
ECE122 L13: Arrays of Objects March 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 13 Arrays of Objects.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
Introduction to Arrays. Useful Array Operations  Finding the Highest Value int [] numbers = new int[50]; int highest = numbers[0]; for (int i = 1; i.
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.
Chapter 7: Arrays and the ArrayList Class
© 2012 Pearson Education, Inc. All rights reserved. Lecture 2 (Chapter 8): Arrays and the ArrayList Class Starting Out with Java: From Control Structures.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: From Control Structures through Objects Third Edition.
Chapter 7: Arrays and the ArrayList Class
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Arrays and the ArrayList Class Starting Out with Java From Control.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
JAVA Array 8-1 Outline  Extra material  Array of Objects  enhanced-for Loop  Class Array  Passing Arrays as Arguments to Methods  Returning Arrays.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 8: Arrays and the ArrayList Class Starting Out with Java: From.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Introduction to Collections Arrays. Collections Collections allow us to treat a group of values as one collective entity. The array is a collection of.
Peyman Dodangeh Sharif University of Technology Fall 2013.
ECE122 Feb. 22, Any question on Vehicle sample code?
8-1 Chapter-7 Part1 Introduction to Arrays –Array Types –Creating/Declaring Arrays –Initializing Arrays –Processing Array Contents –Passing Arrays as Arguments.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
Classes - Intermediate
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Chapter 7: Arrays and the ArrayList Class Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
CMSC 150 ARRAYS CS 150: Fri 10 Feb Motivation  Consider a list of your contact addresses: String Zero = String.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.
Chapter 9 A Second Look at Classes and Objects - 2.
Chapter 8 Arrays and the ArrayList Class Introduction to Arrays.
A Second Look at Classes and Objects - 1 Static Class Members
Chapter VII: Arrays.
Test 2 Review Outline.
Array Review CS 139 – November 28, 2007.
3 Introduction to Classes and Objects.
Chapter Topics Chapter 7 discusses the following main topics:
Yanal Alahmad Java Workshop Yanal Alahmad
Arrays, Searching and Sorting
A Second Look at Classes and Objects - 2
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
Advanced Programming Behnam Hatami Fall 2017.
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Starting Out with Java: From Control Structures through Objects
Pass by Reference, const, readonly, struct
Pemrograman Dasar Methods PTIIK - UB.
Chapter 8: Advanced Arrays and the ArrayList Class
An Introduction to Java – Part I, language basics
Classes & Objects: Examples
Chapter 8 Slides from GaddisText
Introducing Arrays Array is a data structure that represents a collection of the same types of data.
Array Review CS 139 – November 28, 2007.
CS2011 Introduction to Programming I Arrays (II)
CS2011 Introduction to Programming I Arrays (I)
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
CS 180 Assignment 6 Arrays.
Arrays and Array Lists CS 21a.
Single-Dimensional Arrays chapter6
Object Oriented Programming
Array Review CS 139 – November 28, 2007.
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
Arrays in Java.
CSS161: Fundamentals of Computing
CS250 Introduction to Computer Science II
Dr. Sampath Jayarathna Cal Poly Pomona
Java: Variables, Input and Arrays
Introduction to java Part I By Shenglan Zhang.
Presentation transcript:

Chapter 8 Arrays and the ArrayList Class Arrays of Objects

2 Contents I. String Arrays II. Arrays of Objects III. Command-Line Arguments and Variable- Length Argument Lists

3 I. String Arrays 1. Creating String Arrays 2. Calling String Methods from an Array Element

4 I.1. Creating String Arrays In memory, an array of String objects is arranged differently than an array of a primitive data type. An array of String objects is really an array of references to String objects. Creating an array of String objects initialized with values: String[] names = {“Bill”, “Susan”, “Steven” };

5 I.1. Creating String Arrays address “Bill”“Susan”“Steven” The names variable holds the address of a String array The String array is an array of references to String objects

6 I.1. Creating String Arrays Creating an initialized array of String objects: String[] names = new String[3]; null address null The names variable holds the address of a String array The String array is an array of references to String objects

7 I.1. Creating String Arrays When we create an initialized array of String objects, we must assign a value to each element in the array that we intend to use. String[] names = new String[3]; names[0] = “Bill”; names[1] = “Susan”; names[2] = “Steven”;

8 I.2. Calling String Methods from an Array Element Each element of a String array is a String object, we can use an element to call a String method. System.out.println(names[0].toUpperCase()); //Declare a char variable named letter char letter; //Assign the first character in names[3] to letter. Letter = names[3].charAt(0); An element of the names array

9 I.2. Calling String Methods from an Array Element The following loop displays the length of each string held in names : for(int i = 0; i < names.length; i++) System.out.println(names[i].length());

10 Checkpoint 8.15 a. Write a statement that declares a String array initialized with the following strings: “Mercury”, “Venus”, “Earth”, and “Mars”. b. Write a loop that displays the contents of each element in the array you declared in a. c. Write a loop that displays the first character of the strings stored in each element of the array you declared in a.

11 II. Arrays of Objects Like any other data type, we can create arrays of class objects. BankAccount[] accounts = new BankAccount[5]; The variable that references the array is named accounts. As with String arrays, each element in this array is a reference variable. Each element of the array is initialized with the value null.

12 II. Arrays of Objects We must individually create the objects that each element will reference. for(int index = 0; index < accounts.length; index++) accounts[index] = new BankAccount();

13 Checkpoint 8.16 Write a Rectangle class. Write code that declares a Rectangle array with five elements. Instantiate each element with a Rectangle object. Use constructor to initialize each object with values for the length and width fields.

14 III. Command-Line Arguments and Variable-Length Argument Lists 1. Command-Line Arguments 2. Variable-Length Argument Lists

15 III.1. Command-Line Arguments Consider the main method's header: public static void main(String[] args) The parameter args is an reference to an arry of String. The array that is passed into the args parameter comes from the operating system command-line.

16 III.1. Command-Line Arguments /** This program displays the arguments passed to it from the operating system command line. */ public class CommandLine { public static void main(String[] args) { for(int index; index < args.length; index++) System.out.println(args[index]); } } Compile and the execute with the following command: java CommandLine Good morning Good morning

17 III.2. Variable-Length Argument Lists Java provides a mechanism known as variable- length argument lists. We can write a method that takes a variable number of arguments. The method that accepts any number of arguments when it is called. The parameter is known as a vararg parameter. int sum(int... numbers) The ellipsis indicates that numbers is a vararg parameter. The vararg parameter can take a variable number of arguments. In fact, vararg parameters are actually arrays.

18