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:

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection.
For use of IST410 Students only Arrays-1 Arrays. For use of IST410 Students only Arrays-2 Objectives l Declaring arrays l Instantiating arrays l Using.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
For use of Cleveland State's IST410 Students only 1 Vectors and Collections.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
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:
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
Java Unit 9: Arrays Declaring and Processing Arrays.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
JavaDoc1 JavaDoc DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY July 24, 2006 by Emil Vassev & Joey Paquet revision 1.2 –
CSC Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 4.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
ArrayList, Multidimensional Arrays
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Arrays BCIS 3680 Enterprise Programming. Overview 2  Array terminology  Creating arrays  Declaring and instantiating an array  Assigning value to.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Lists and the Collection Interface Review inheritance & collections.
GENERICS. Generics  Classes and methods can have a type parameter (actually, more than one).  The type parameter can be any reference (class) type.
Data structures Abstract data types Java classes for Data structures and ADTs.
Javadoc Comments.  Java API has a documentation tool called javadoc  The javadoc tool is used on the source code embedded with javadoc-style comments.
Documentation Array and Searching. Documentation rules Easy rules: –Naming convention for variables, constants and methods Difficult rules: –Professional.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
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.
Lists and the Collection Interface Chapter 4. 2 The List Interface and ArrayList Class So far, all we have is an array for storing a collection of elements.
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.
CSE 143 Lecture 2 ArrayList reading: 10.1 slides created by Marty Stepp
Everything is an object (CH-2) Manipulating Objects with References. Manipulating Objects with References. String s; String s = “IS2550” String s = new.
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
List Interface and Linked List Mrs. Furman March 25, 2010.
CSC Java Programming, Fall, 2008 Week 3: Objects, Classes, Strings, Text I/O, September 11.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Lecture 9: Lists MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture we will learn…. ArrayList – These are re-sizeable.
CSC Java Programming, Spring, 2010 Week 2: Java Data Types, Control Constructs, and their C++ counterparts.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
CSC Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 9.
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
More on Arrays Review of Arrays of ints, doubles, chars
The need for Programming Languages
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
Can store many of the same kind of data together
Words exercise Write code to read a file and display its words in reverse order. A solution that uses an array: String[] allWords = new String[1000]; int.
Chapter 8 Slides from GaddisText
Arrays versus ArrayList
Welcome to CSE 143!.
Can store many of the same kind of data together
Object Oriented Programming in java
MSIS 655 Advanced Business Applications Programming
Can store many of the same kind of data together
Java Programming Language
CSC Java Programming, Spring, 2012
Arrays.
Presentation transcript:

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: data type [] name = new data type[int expression];

3 Arrays in Java Elements in a new array are automatically set to 0, false, or null (depending on the underlying data type) Size of an array is stored in its length attribute; example: int [] numbers = new int [100]; int size = numbers.length; // stores 100 in size

4 Arrays in Java Access to individual members is achieved via index (subscript), just as it is in C++ Can construct and initialize an array in a single statement, such as: char [] vowels = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’};

5 Anonymous arrays Array reference that is not stored in a variable Can be used in construction of other objects Java’s Polygon class is an example; has constructor: Polygon(int[] xvalues, int[] yvalues, int n) Can construct a triangle using anonymous arrays, as shown below: Polygon triangle = new Polygon(new int[] {0, 10, 5}, new int[] {10, 0, 5}, 3);

6 2-dimensional arrays Declaration & construction: Data type[][] name = new data type [rows][cols]; As in C++, array is indexed in row-major order

7 String[] args The default parameter to every main() method is args, an array of String This array is used to hold command-line arguments –args.length stores the number of arguments –Each argument is stored as a String from args[0] to args[args.length-1]

8 ArrayLists ArrayList class describes object that can hold a collection of objects of any type –objects, not primitive data types –can use wrapper classes (Integer, Double, Character, etc.) if ArrayList of one of these types is desired ArrayList class is part of the java.util package

9 ArrayList methods Constructor: ArrayList myList = new ArrayList(); doesn’t specify data type or size data type is Object, size determined by number of elements placed in List add() –simplest form just adds element to end of List; example: myList.add(“Finish notes before class”); myList.add(“Get haircut”); myList.add(“Lather, rinse, repeat”);

10 ArrayList methods size(): returns number of elements in List get(): returns element at specified position (between 0 and size() - 1) String firstItem = (String)myList.get(0); Since get() returns an Object, need explicit cast set(): replaces element at specified location –first argument: position of element –second argument: object to place myList.set(2, “Buy wig”);

11 ArrayList methods Insertion of elements in the middle of a list is accomplished via a call to add() using two arguments: –first: position of insertion –second: object to insert myList.add(1, “Yet another item”);

12 ArrayList methods remove() method removes the element at the specified position: myList.remove(0); Both add() and remove() move remaining elements up or down as needed

13 LinkList ArrayLists are especially useful for applications in which random access to specific elements is desirable However, they are inefficient for applications in which there are frequent insertions/deletions in the middle of a List The LinkList class provides an alternative for this type of application

14 LinkList Part of Java standard library Like ArrayList, container for collection of Objects Methods include: –constructor: LinkedList aList = new LinkedList(); –add(): places element at end of List –listIterator(): constructs an iterator for traversal of List (described on next slide)

15 ListIterator() Object that can access a position anywhere within a LinkList A LinkList object creates an iterator by calling its listIterator() method: ListIterator cursor = aList.listIterator(); Includes methods for moving through a List and adding and removing an element from the List

16 ListIterator methods hasNext(): returns true if iterator hasn’t reached end of List next(): returns current element; moves iterator to next element while (cursor.hasNext()) { String s = (String)cursor.next(); System.out.println(s); }

17 Adding/removing elements add() called on iterator inserts element in position in front of the iterator: cursor.add(“new element”); remove() called on iterator removes the element in front of the iterator: cursor.remove(); If an iterator reaches the end of a list, can start over by constructing another one

18 Packages Groups of Java classes Named using dot-separated sets of identifiers that correspond to the names of directories in a class’s path Base directory of a package is that directory containing all package directories Compilation of a class in a package must occur in the class’s base directory

19 Placing class in a package As the beginning of the source file, use a statement like the following: package base.sub1.sub2.subN.current; –for example, a class in the MYCLASSES subdirectory on drive a: would contain the statement: package myclasses; –if the class name is CLASS1, its full name would be myclasses.class1 A source file that doesn’t have a package statement is considered to be in the default package

20 Internal documentation & javadoc javadoc is a program included with the SDK (along with javac and java) When code has been commented with specially formatted comments, javadoc produces HTML pages describing your classes

21 Format for javadoc comments Start with /** and end with */ First sentence of each comment will be copied by javadoc to the summary table; should place one such comment strategically before each class and each method at the start of a line to describe a parameter at the start of a line to describe a return value

22 Guidelines for comments in Java code Supply an informative summary comment in the first line of documentation for every class and method Supply a description of every parameter and return value Write the comments first, then write the code: –for class, can use description from CRC card –for methods, can borrow information from responsibilities

23 Running javadoc At command line, type javadoc *.java Produces: –one HTML file per class –index.html containing links to other files When code is updated, update javadoc comments at the same time; run javadoc to update your HTML files

24 Java intro Part 3