More on Arrays Review of Arrays of ints, doubles, chars

Slides:



Advertisements
Similar presentations
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Advertisements

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Java intro Part 3. 2 Arrays in Java Store fixed number of values of a given type Arrays are objects –have attributes –must be constructed Array declaration:
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
ECE122 L13: Arrays of Objects March 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 13 Arrays of Objects.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
1 More on Arrays Arrays of objects Command line arguments The ArrayList class Javadoc Review Lecture 8 notes and L&L 7.1 – 7.2 Reading for this lecture:
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
The Java Programming Language
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Java Syntax and Style JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin,
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition.
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.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
CSE 501N Fall ‘09 08: Arrays 22 September 2009 Nicholas Leidenfrost.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 7 Arrays 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
1 Arrays An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 7: Arrays.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
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.
Lecture 101 CS110 Lecture 10 Thursday, February Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.
© 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Everything is an object (CH-2) Manipulating Objects with References. Manipulating Objects with References. String s; String s = “IS2550” String s = new.
© 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State.
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
1  lecture slides online
Lecture 7 February 24, Javadoc version and author Tags These go in the comments before named classes. –Put your SS# on a separate line from the.
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.
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.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 The if-else Statement An else clause can be added to an if statement to make an if-else statement.
Chapter 7 Arrays…. 7-2 Arrays An array is an ordered list of values An array of size N is indexed from.
Java: Base Types All information has a type or class designation
Variable Scope & Lifetime
CMSC 202 ArrayList Aug 9, 2007.
Lecture 5 array declaration and instantiation array reference
Arrays of Objects October 9, 2006 ComS 207: Programming I (in Java)
Chapter VII: Arrays.
Sixth Lecture ArrayList Abstract Class and Interface
Java: Base Types All information has a type or class designation
Parameter Lists & Command Line Arguments
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Lecture 2: Implementing ArrayIntList reading:
The ArrayList Class An ArrayList is a complex data structure that allows you to add or remove objects from a list and it changes size automatically. The.
Algorithm Efficiency, Big O Notation, and Javadoc
CMSC 202 ArrayList Aug 9, 2007.
Welcome to CSE 143!.
Arrays and Collections
Object Oriented Programming in java
CMSC 202 ArrayList Aug 9, 2007.
Grouped Data Arrays, and Array Lists.
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Classes CS 21a: Introduction to Computing I
Review: libraries and packages
Arrays of Objects October 8, 2007 ComS 207: Programming I (in Java)
Arrays.
Presentation transcript:

More on Arrays Review of Arrays of ints, doubles, chars Arrays of objects Command line arguments The ArrayList class Javadoc Review Lecture 8 notes and L&L 7.1 – 7.2 Reading for this lecture: L&L 7.3 – 7.7, App I

Review of Arrays We can declare an array of int variables with the capability to use an index variable to select one variable int[ ] nums = new int [5]; The above declares 5 variables of type int The valid array index values are 0-4 Note: Values have not been assigned to those 5 variables in the array yet. To set a value, can use, for example nums[0] = 3; Similarly, an array of 10 double variables: double[ ] nums1 = new double[10]; 2

Review: Array Initializer Lists An array can be defined and initialized so that each element contains a specific value: int[] primes = {2,3,5,7,11, 13}; char [] vowels = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’}; Java uses the initializer list to determine how long the array must be and allocates that many elements An initializer list can be used only when the array is first declared, as above Afterward, each individual element of the array can be accessed with an index, for example: int firstPrime = primes[0]; // 2 boolean result = vowels[3] == ‘o’ // true 3

String[] words = new String[5]; Arrays of Objects The elements of an array can be object references The following declaration reserves space to store 5 references to String objects String[] words = new String[5]; It does NOT create the String objects themselves Initially an array of objects holds null references Each object stored in an element of an array must be instantiated separately

System.out.println(words[0].length()); Arrays of Objects The words array when initially declared: words - A reference to words.length is OK (= 5) However, the following reference will throw a NullPointerException: System.out.println(words[0].length());

Arrays of Objects To create some String objects and store them in elements of the array: words[0] = new String(“friendship”); words[1] = “loyalty”; words[2] = “honor”; words “friendship” “loyalty” “honor” - -

Arrays of Objects String objects can be created using literals The following declaration creates an array object called verbs with a length of 4 and fills it with references to four String objects created using string literals String[] verbs = {"play", "work", "eat", "sleep"};

Arrays of Objects To use one of the methods of an object element of an array: verbs[2].equals(“eat”); // true To pass one of the object elements of an array as a parameter to a method: “eat”.equals(verbs[2]); // true To return an element of an array: public String methodName(String [] verbs) { return verbs[2]; // “eat” }

Command-Line Arguments Your program’s main method is defined as: public static void main(String [] args) The signature of the main method indicates that it takes an array of String objects as a parameter These values come from command-line arguments that are provided when the interpreter is invoked In Dr Java interactions pane, this invocation of the JVM passes three String objects (or tokens) as arguments to the main method of StateEval: > java StateEval pennsylvania texas arizona Command Line “Tokens”

Command Line Arguments These strings are stored at indexes 0-2 in the array args for the main method The array args will contain: Code in main can print the arguments: for (String arg : args) System.out.println(arg); args “pennsylvania” “texas” “arizona”

The ArrayList Class The ArrayList class is in java.util package Instantiating an empty ArrayList ArrayList<String> myList = new ArrayList<String>( ); Like an array: ArrayList can store a list of object references You can access each one using a numeric index Unlike an array: ArrayList object grows and shrinks as needed You don’t use [ ] syntax with an ArrayList object Cannot store primitive types (Use Wrapper classes)

The ArrayList Class The ArrayList class is available in the java.util package Instantiating an empty ArrayList: ArrayList<String> myList = new ArrayList<String>( ); An ArrayList stores references to the class inside the < > which allows it to store objects of that class only This is a part of Java’s generics capability which you will study further in CS210 12

The ArrayList Class Strings are inserted with a method invocation boolean b = myList.add(string); // to end myList.add(index, string); // at index When an element is inserted at a specific index, the other elements are "moved aside" to make room If index > myList.size(), the method throws an IndexOutOfBounds exception Elements are removed with a method invocation String s = myList.remove(index); When an element is removed, the list "collapses" to close the gap and maintain contiguous indexes

ArrayList Efficiency The ArrayList class is implemented using an underlying array The array is manipulated so that indexes remain contiguous as elements are added or removed If elements are added to and removed from the end of the list, this processing is fairly efficient But as elements are inserted and removed from the front or middle of the list, the remaining elements are shifted

Javadoc Javadoc is a JDK tool that creates HTML user documentation for your classes and their methods In this case, user means a programmer who will be writing Java code using your classes You can access Javadoc via the JDK CLI: javadoc MyClass.java This creates MyClass.html You can access Javadoc via Dr Java menu: Tools > Javadoc >Javadoc All Documents Tools > Javadoc >Preview Javadoc for Current Document

Javadoc The Javadoc tool scans your source file for specialized multi-line style comments: /** * <p>HTML formatted text here</p> */ Your Javadoc text is written in HTML so that it can appear within a standardized web page format We’ll just use plain text: it works fine too * Plain text here

Block Tags for Classes At the class level, you should include these block tags with data (each on a separate line): /** * @author Your Name * @version Version Number or Date */ You should include text describing the use of this class and perhaps give examples

Block Tags for Methods At the method level, you should include these block tags, especially for public methods: /** * @param text for 1st parameter * @param text for 2nd parameter * @return text for return value */ If there are no parameters or return type, you can omit these Javadoc block tags

Example of Javadoc /** * Represents a box * @author Robert Cohen * @version 1.0 */ public class Box { private int id; // id for Box private int height; // height of Box * Constructs a Box * @param id the id of the Box * @param height the height of the Box public Box(int id, int height) { this.id = id; this.height = height; } /** * Returns the height of the box (a getter) * @return the height of the box */ public int getHeight() { return height; } * @return A string representation of the Box public String toString() { return "Box " + id + ": " + height;