Java SE 8 for Programmers, Third Edition

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Arrays and ArrayLists Ananda Gunawardena. Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages.
Chapter 10 Introduction to Arrays
 2005 Pearson Education, Inc. All rights reserved Introduction.
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Chapter 9: Arrays and Strings
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
Chapter 6: Arrays Java Software Solutions Third Edition
More Arrays Length, constants, and arrays of arrays By Greg Butler.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
1.  Collections are data structures that holds data in different ways for flexible operations  C# Collection classes are defined as part of the ◦ System.Collections.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
 2006 Pearson Education, Inc. All rights reserved Arrays.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
ArrayList, Multidimensional Arrays
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
 Pearson Education, Inc. All rights reserved Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 Pearson Education, Inc. All rights reserved Arrays.
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.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
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.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CSE 1201 Object Oriented Programming ArrayList 1.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
CS 116: Object Oriented Programming II. Topics Vectors Multidimensional Arrays ArrayList.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
 2005 Pearson Education, Inc. All rights reserved Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Java Programming Language Lecture27- An Introduction.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Sixth Lecture ArrayList Abstract Class and Interface
Array, Strings and Vectors
Data Structures Array - Code.
Java How to Program, Late Objects Version, 10/e
Object Oriented Programming in java
Data Structures Array - Code.
MSIS 655 Advanced Business Applications Programming
Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7.
Arrays.
Presentation transcript:

Java SE 8 for Programmers, Third Edition Advanced Java Programming Java SE 8 for Programmers, Third Edition

Contents I Multidimensional Arrays Introduction to Collections and Class ArrayList Object-Oriented Programming(cont’d) Interfaces final Methods and Classes Creating and Using Interfaces Interface Enhancements Exception Handling

Contents II Swing GUI Components Simple GUI-Based Input/Output with JOptionPane Overview of Swing Components Displaying Text and Images in a Window Text Fields and an Introduction to Event Handling with Nested Classes Buttons That Maintain State JComboBox; Using an Anonymous Inner Class for Event Handling Mouse Event Handling Adapter Classes JPanel Subclass for Drawing with the Mouse Introduction to Layout Managers

Contents III Graphics and Java 2D Strings, Characters and Regular Expressions Class String Files, Streams and Object Serialization

Two-Dimensional Arrays with Rows of Different Lengths

Arrays of One-Dimensional Arrays int[][] b = {{1, 2}, {3, 4}} Multidimensional arrays are maintained as arrays of one-dimensional arrays. The initial values are grouped by row in braces. 1 and 2 initialize b[0][0] and b[0][1], 3 and 4 initialize b[1][0] and b[1][1], The compiler counts the number of nested array initializers to determine the number of rows in array b. The compiler counts the initializer values in the nested array initializer for a row to determine the number of columns in that row. array b is actually composed of two separate one-dimensional arrays one containing the value first nested initializer list {1, 2} one containing the values in the second nested initializer list {3, 4}. array b itself is anof two elements, each a one-dimensional array of int values.

Two-Dimensional Arrays with Rows of Different Lengths int[][] b = {{1, 2}, {3, 4, 5}}; creates integer array b with two elements Representing the rows of the two-dimensional array. Each element of b is a reference to a one-dimensional array of int variables. The int array for row 0 is a one-dimensional array with two elements (1 and 2), the int array for row 1 is a one-dimensional array with three elements (3, 4 and 5).

Creating Two-Dimensional Arrays with Array-Creation Expressions int[][] b = new int[3][4]; we use the values 3 and 4 to specify the number of rows and number of columns, but this is not required. Programs can use variables to specify array dimensions new creates arrays at execution time—not at compile time. The elements of a multidimensional array are initialized when the array object is created.

Creating Two-Dimensional Arrays with Array-Creation Expressions A multidimensional array in which each row has a different number of columns can be created int[][] b = new int[2][]; // create 2 rows b[0] = new int[5]; // create 5 columns for row 0 b[1] = new int[3]; // create 3 columns for row 1 This statements create a two-dimensional array with two rows. Row 0 has five columns Row 1 has three columns

Two-Dimensional Array Example: Displaying Element Values public class InitArray { // create and output 2D arrays public static void main(String[] args) int[][] array1 = {{1, 2, 3}, {4, 5, 6}}; int[][] array2 = {{1, 2}, {3}, {4, 5, 6}}; System.out.println ("Values in array1 by row are"); outputArray(array1); // displays array1 by row System.out.printf ("%nValues in array2 by row are%n"); outputArray(array2); // displays array2 by row } // output rows and columns of a 2D array public static void outputArray(int[][] array) { // loop through array's rows for (int row = 0; row < array.length; row++) { // loop through columns of current row for (int column = 0; column < array[row].length; column++) System.out.printf("%d ", array[row][column]); System.out.println(); } } // end class InitArray

Values in array1 by row are 1 2 3 4 5 6 Values in array2 by row are 1 2 3

Common Multidimensional-Array Manipulations Performed with for Statements Many common array manipulations use for statements. The following for statement sets all the elements in row 2 of array a to zero: for (int column = 0; column <a[2].length; column++) a[2][column] = 0; We specified row 2 we know that the first index is always 2 (0 is the first row, and 1 is the second row). This for loop varies only the second index (i.e., the column index). If row 2 of array a contains four elements, then the preceding for statement is equivalent to the assignment statements a[2][0] = 0; a[2][1] = 0; a[2][2] = 0; a[2][3] = 0;

Common Multidimensional-Array Manipulations Performed with for Statements The nested for statement totals the values of all the elements in array a: int total = 0; for (int row = 0; row < a.length; row++) { for (int column = 0; column < a[row].length; column++) total += a[row][column]; } These nested for statements total the array elements one row at a time. The outer for statement begins by setting the row index to 0 so that the first row’s elements can be totaled by the inner for statement. The outer for increments row to 1 so that the second row can be totaled. The outer for increments row to 2 so that the third row can be totaled. The variable total can be displayed when the outer for statement terminates.

// an example of two-dimensional array int[][] grades = {{87, 96, 70}, {68, 87, 90}, {94, 100, 90}, {100, 81, 82}, {83, 65, 85}, {78, 87, 65}, {85, 75, 83}, {91, 94, 100}, {76, 72, 84}, {87, 93, 73}};

Introduction to Collections and Class ArrayList The Java API provides several predefined data structures, called collections, used to store groups of related objects in memory. These classes provide efficient methods that organize, store and retrieve your data without requiring knowledge of how the data is being stored. This can reduce application-development time and help you produce higher-quality, better-performing software.

Introduction to Collections and Class ArrayList Arrays are used to store sequences of objects. Arrays do not automatically change their size at execution time to add additional elements. The collection class ArrayList<T> (package java.util) provides a convenient solution to this problem it can dynamically change its size to accommodate more elements. The T (by convention) is a placeholder when declaring a new ArrayList, replace it with the type of elements that you want the ArrayList to hold.

Class ArrayList<T> java.lang.Object java.util.AbstractCollection<T> java.util.AbstractList<T> java.util.ArrayList<T>

Introduction to Collections and Class ArrayList ArrayList<String> list; declares list as an ArrayList collection that can store only Strings. Classes with this kind of placeholder that can be used with any type are called generic classes. Only nonprimitive types can be used to declare variables and create objects of generic classes. Boxing mechanism allows primitive values to be wrapped as objects for use with generic classes.

Boxing Mechanism ArrayList<Integer> integers; declares integers as an ArrayList that can store only Integers. When you place an int value into an ArrayList<Integer>, the int value is boxed (wrapped) as an Integer object, When you get an Integer object from an ArrayList<Integer>, then assign the object to an int variable, the int value inside the object is unboxed (unwrapped).

Some methods and properties of class ArrayList<T>.

an ArrayList<String> Example import java.util.ArrayList; public class ArrayListCollection { public static void main(String[] args) // create a new ArrayList of Strings with an initial capacity of 10 ArrayList<String> items = new ArrayList<String> (); items.add("red"); // append an item to the list items.add(0, "yellow"); // insert "yellow" at index 0 //header System.out.print( "Display list contents with counter- controlled loop:"); for (int i = 0; i < items.size(); i++) System.out.printf(" %s", items.get(i));

an ArrayList<String> Example // display colors using enhanced for in the display method display(items, "%nDisplay list contents with enhanced for statement:"); items.add("green"); // add "green" to the end of the list items.add("yellow"); // add "yellow" to the end of the list display(items, "List with two new elements:"); items.remove("yellow"); // remove the first "yellow" display(items, "Remove first instance of yellow:"); items.remove(1); // remove item at index 1 display(items, "Remove second list element (green):"); // check if a value is in the List System.out.printf("\red \" is %s in the list%n", items.contains("red") ? "": "not "); // display number of elements in the List System.out.printf("Size: %s%n", items.size()); }

an ArrayList<String> Example // display the ArrayList's elements on the console public static void display(ArrayList<String> items, String header) { System.out.printf(header); // display header // display each element in items for (String item : items) System.out.printf(" %s", item); System.out.println(); } } // end class ArrayListCollection

Output Display list contents with counter-controlled loop: yellow red Display list contents with enhanced for statement: yellow red List with two new elements: yellow red green yellow Remove first instance of yellow: red green yellow Remove second list element (green): red yellow "red" is in the list Size: 2