Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic.

Slides:



Advertisements
Similar presentations
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Advertisements

Strings in Java 1. strings in java are handled by two classes String &
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
OO Programming Objectives for today: Casting Objects Introduction to Vectors The instanceof keyword.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Strings and Arrays The objectives of this chapter are:  To discuss the String class and some of its methods  To discuss the creation and use of Arrays.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
1 Dynamic Arrays  Why Dynamic Arrays?  A Dynamic Array Implementation  The Vector Class  Program Example  Array Versus Vector.
Fundamental Programming Structures in Java: Strings.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
Review Java.
© 2007 Lawrenceville Press Slide 1 Chapter 6 The while Statement  Loop structure that executes a set of statements as long as a condition is true  The.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
1 ArrayList  Array’s are limited because we need to know the size before we use them.  An ArrayList is an extension of an array that grows and shrinks.
AP CS Workshop ArrayList It is very common for applications to require us to store a large amount of data. Array lists store large amounts of data.
ArrayList, Multidimensional Arrays
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.
The java.lang Package chapter 8 Java Certification Study Group February 2, 1998 Seth Ladd.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
String Manipulation Chapter 15 This chapter explains the String facilities. You have already seen some of the main methods of the String class.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
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.
Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.
Chapter 7: Characters, Strings, and the StringBuilder.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
Geoff Holmes Date Math Weighted Distr Strings String methods Tokenizers System Examples Utility Classes (Chapter 17) import java.util.*;
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
Title Slid CSC 444 Java Programming Arrays By Ralph B. Bisland, Jr.
MCQ Which is the correct syntax for placing the string "boat" into an ArrayList name recVehicles in position 3 (index 2) for the first time? a)recVehicles.set(3,
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
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.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
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.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Introduction to Packages Session 18. Java Simplified / Session 18 / 2 of 40 Review Data may get corrupted when two or more threads access the same variable.
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.
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
String and String Buffers
COP 3503 FALL 2012 Shayan Javed Lecture 8
Primitive Types Vs. Reference Types, Strings, Enumerations
Modern Programming Tools And Techniques-I Lecture 11: String Handling
MSIS 655 Advanced Business Applications Programming
Programming in Java Lecture 11: ArrayList
Java Arrays & Strings.
Can store many of the same kind of data together
Lecture 07 String Jaeki Song.
ArrayLists 22-Feb-19.
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Outline Creating Objects The String Class The Random and Math Classes
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
In Java, strings are objects that belong to class java.lang.String .
Unit-2 Objects and Classes
Presentation transcript:

Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

java.lang package The package consists classes and interface which are fundamental to Java programming All the program automaticlly import java.lang package It contains

Wrapper classes

Wrapper classes (cont…)

Common methods

Math class Defining methods for basic numeric operations as well as trigonomethic functions All the methods are static This is final class Methods public static double pow(double x, double y) public static double exp(double x) public static double log(double x)

Methods of Math class Trigonometric methods public static double sin(double x) public static double cos(double x) public static double tan(double x) Arirthmetic methods

Array Stored many values Elements in array have the same datatype Elements in array are accessed throught by subscript Types of array: Single dimension array (one dimension array) Multi demension array

One Dimensional Array Declarations Three ways to declare an array are: datatype identifier [ ]; int a[]; or int [] a; datatype identifier [ ] = new datatype[size]; char ch[] = new char[10]; datatype identifier [ ] = {value1,value2,….valueN}; byte b[] = {12, 3, 5};

Initialize elements in array Init value for each element a[0] = 12; a[1] = 15; Init when creating array int a[] = {12, 15, 17}; Using loop for(int i = 0 ; i < a.length ; i++) a[i] = …;

Example class ArrDemo { public static void main(String [] arg) { double nums[] = {10.1, 11.3, 12.5,13.7, 14.9}; System.out.println(" The value at location 3 is : " + nums[3]); }

Two dimension array Array that has more than one dimention Syntax: int a[][]; int []a[] = new int[2][3]; int [][]a = {{1, 2}, {3, 4}, {-9, 0}}; a[0][0]a[0][1]a[0][2] a[0]135 a[1]9-817 a[1][0]a[1][1]a[1][2]

Initialize 2 dimension array Initialize for each elements a[0][0] = ‘c’; a[0][1] = ‘e’; Initialize when declaring array int a[][] = {{1, 2}, {13, -78}}; Using loop for(int i = 0 ; i < n ; i++) for(int j = 0 ; j < m ; j++) a[i][j] = …;

String class In Java, a string literal is an object of type String class. Hence manipulation of strings will be done through the use of the methods provided by the String class. String class is in java.lang package Every time we need an altered version of the String, a new String object is created with the modifications in it.

Compare two String String length(): This method determines the length of a string. The == operator and equals(), compareTo() method can be used for strings comparison. The == operator checks if the two operands being used are one and the same object. The equals() method checks if the contents of the two operands are the same. compareTo() return value = 0 if s1 = s2, return value >0 if s1 > s2, return value < 0 if s1 < s2

Methods of String trim () substring (int start, int end) equals (Object s) equalsIgnoreCase (String s) charAt(int i) endsWith (String s) startsWith (String s) indexOf (String s) lastIndexOf (String s) toLowerCase () toUpperCase ()

Example class StringTest { public static void main(String[] args) { String name = args[0]; if(name.startsWith("M")) System.out.println("Hey my name also starts with an M! "); int length = name.length(); System.out.println("Your name has "+length+" characters"); String name_in_caps = name.toUpperCase(); System.out.println(name_in_caps); }

Convert String into number String s = “12”; String to int int a = Interger.parseInt(s); String to float float f = Float.parseFloat(s);

Array of Strings String s[]; s = new String[3]; Or String s[] = new String[3]; Or String []s = {“a”, “b”, “c”}; Learn Java By Example 19/21

Immutability of String Strings in Java once created cannot be changed directly. This is known as immutability in Strings. Learn Java By Example 20/21

Example class Testing { public static void main(String[] args) { String str = "Hello"; str.concat("And Goodbye"); System.out.println(str); } Learn Java By Example 21/21

java.util package Contains useful classes providing a broard range of functionality Collection classes are useful for working with groups of objects Contains classes that provides date and time, calendar, dictionary facilities

StringTokenizer Placed at java.util Seperated String into token Methods countTokens() hasMoreElements() hasMoreTokens() nextElement() nextToken() Example: s = “22+3-5/3” Token: Separator: +-/

ArrayList An ArrayList object is a variable length array of object references. Used to create dynamic arrays Extends AbstractList and implements List interface. ArrayLists are created with an initial size. As elements are added, size increases and the array expands.

ArrayList (cont…) Constructors Methods size() add(int index, E element)addE get(int index)get indexOf(Object elem)indexOfObject lastIndexOf(Object elem)lastIndexOfObject remove(int index)remove remove(Object o)removeObject set(int index, E element)setE

Vector class Similar to ArrayList class, allows to implement dynamic array Storing an array of objects that size can increase or decrease At any given point of time, an instance of type Vector has the capacity to hold a certain number of elements. When it becomes full, its capacity is incremented by an amount specific to that Vector object. Diffirence between Vector and ArrayList is that methods of Vector are synchronized and are thread- safe

Vector class (cont…)

Generic Earlier Collection treated elements as a collection of objects. To retrieve an element from a Collection required an explicit cast Thus, there was always a risk of runtime exception, ClassCastException Generic allows the programmer to communicate the type of a collection to the compiler so that it can be checked. The compiler consistently checks for the element type of the collection. And inserts the correct cast on elements being taken out of the collection.

Example

Example of Generic Code required explicit cast No required explicit cast Type of element