ImageVerifierCode 换一换
格式:PPT , 页数:29 ,大小:117.50KB ,
资源ID:378574      下载积分:2000 积分
快捷下载
登录下载
邮箱/手机:
温馨提示:
如需开发票,请勿充值!快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝扫码支付 微信扫码支付   
注意:如需开发票,请勿充值!
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【http://www.mydoc123.com/d-378574.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Arrays, Strings and Collections [2].ppt)为本站会员(jobexamine331)主动上传,麦多课文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知麦多课文库(发送邮件至master@mydoc123.com或直接QQ联系客服),我们立即给予删除!

Arrays, Strings and Collections [2].ppt

1、1,Arrays, Strings and Collections 2,Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software Engineering University of Melbourne, Australia http:/,2,toString() Method,toString() method is a special method that can be defined in any class.This me

2、thod should return a String argument.When an object is used in a String concatenation operation or when specified in print statement, this method gets invoked automatically.,3,toString() Method -Example,class Circle double x, y,r;public Circle (double centreX, double centreY, double radius ) x = cen

3、treX ; y = centreY; r = radius; public String toString()String s = “I am a Circle with centre “ + x + “,” + y + “ and radius “+ r + “”; return s;,4,toString() Method -Example,class CircleTest Circle c = new Circle(10,20, 30);System.out.println( c );/ I am a circle with centre 10.0,20.0 and radius 30

4、.0 ,5,StringBufferClass,Unlike the String class, StringBuffer class is mutable (changeable). Use StringBufferClass class in operations where the string has to be modified.,6,StringBuffer class - Constructors,7,StringBuffer class Some operations,8,Inserting a String in Middle of Existing StringBuffer

5、,StringBuffer str = new StringBuffer(“Object Language”); String aString = new String(str.toString(); Int pos = aString.indexOf(“ Language”); str.insert(pos, “ Oriented “); what will out put of at this point: System.out.println(“Modified String:”+str); What will be string after executing (modifying c

6、haracter): str.setChar(6,-);,9,StringTokenizer,Breaks string into parts, using delimiters. The sequence of broken parts are the tokens of the string. More than one delimiter can be specified. The tokens can be extracted with or without the delimiters.,10,StringTokenizer - Functionality,Consider the

7、following String CREATE_USER:1234567;John;SmithSeparate the tokens CREATE_USER 1234567 John Smith,11,StringTokenizer - Constructors,12,StringTokenizer - Operations,13,StringTokenizer - example,Output of the program CREATE_USER 123456 John Smith,import java.util.StringTokenizer; class TokenizerExampl

8、e public static void main(string args)String str = “CREATE_USER:123456;John;Smith”;StringTokenizer tokens = new StringTokenizer(str, “:;”);while ( tokens.hasMoreTokens() )System.out.println(tokens.nextToken(); ,14,Collections,Arrays are used to hold groups of specific type of itemsCollections (conta

9、iner) designed to hold generic (any) type of objectsCollections let you store, organize and access objects in an efficient manner.,15,Legacy Collection Types,Vector Stack Dictionary HashTable Properties Enumeration,16,Vector,The Vector class implements a growable array of objects. Like an array, it

10、contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created. In Java this is supported by Vector class contained in java.util package. The Vector class can be us

11、ed to create generic dynamic arrays that hold objects of any type or any number. The objects do not have to be homogeneous. Like arrays, Vectors are created as follows: Vector list = new Vector(); / declaring without size Vector list = new Vector(3); / declaring with size,17,Vector properties,Vector

12、s posses a number of advantages over arrays: It is convenient to use vectors to store objects. A vector can be used to store list of objects that may vary in size. We can add and delete objects from the list as an when required. But vectors cannot be used to store basic data types (int, float, etc.)

13、; we can only store objects. To store basic data type items, we need convert them to objects using “wrapper classes” (discussed later).,18,Important Methods in Vector class,addElement(Object item) insertElementAt(Object item, int index) elementAt(int index) get element at index removeElementAt(int i

14、ndex) size() clone() - Returns a clone of this vector. clear() - Removes all of the elements from this Vector. get(int index) - Returns the element at the specified position in this Vector. copyInto(array) copy all items from vector to array.,19,Vector Example 1,import java.util.*;public class Vecto

15、rOnepublic static void main(String args) Vector circleVector = new Vector();System.out.println(“Vector Length + “, circleVector.size(); / 0for ( int i=0; i 5; i+) circleVector.addElement( new Circle(i) ); / radius of the Circles 0,1,2,3,4 System.out.println(“Vector Length = “ + circleVector.size();/

16、 5 ,20,Vector Example 2,import java.util.*; public class VectorTwopublic static void main(String args) code from VectorOne goes herecircleVector.insertElementAt( new Circle(20), 3);System.out.println(“Vector Length =“ + circleVector.size(); / 6for ( int i = 0; i 6; i+)System.out.println(“Radius of e

17、lement “ + i + “ = “+ ( (Circle) circleVector.elementAt(i).getRadius();/ radius of the Circles are 0,1,2,20,3,4 ,21,Hash Table (Hashtable clalss),Allows associating values with keys. Allows efficient look ups for the value associated with the key This class implements a hashtable, which maps keys to

18、 values. Any non-null object can be used as a key or as a value. Useful Operations: put(Object key, Object value); remove(Object key); get(Object key);,22,HashTable put()/get() operations,The following example creates a hashtable of numbers. It uses the names of the numbers as keys: Hashtable number

19、s = new Hashtable(); numbers.put(“one“, new Integer(1); numbers.put(“two“, new Integer(2); numbers.put(“three“, new Integer(3); To retrieve a number, use the following code: Integer n = (Integer)numbers.get(“two“); if (n != null) System.out.println(“two = “ + n); ,23,HashTable -Example,import java.u

20、til.*; public class HashtableDemo public static void main(String args) Hashtable tbl = new Hashtable();Student s, sRet;s = new Student(“121212“, “John“);tbl.put (s.getId(), s);s = new Student(“100000“, “James“);tbl.put (s.getId(), s);sRet= (Student) tbl.get(“121212“);System.out.println(“Student name

21、 is = “ + sRet.getName();/ Student name is = John ,24,Enumeration,Used to enumerate or iterate through a set of values in a collection. Useful for iterating Hashtables no index. Useful Operations: hasMoreElements(); nextElement();,25,Enumeration - Example,import java.util.*; public class Enumeration

22、Demopublic static void main(String args) Hashtable tbl = new Hashtable();Student s, sRet;s = new Student(“121212“, “John“);tbl.put(s.getId(), s);s = new Student(“100000“, “James“);tbl.put(s.getId(), s);Enumeration e = tbl.elements();while ( e.hasMoreElements() sRet = (Student) e.nextElement();System

23、.out.println(“Student name is = “ + sRet.getName();/ Student name is = James/ Student name is = John ,26,Wrapper Classes,As pointed out earlier, collections cannot handle basic data types such as int, float. They can converted into object types by using the wrapper classes supported by java.lang pac

24、kage.,27,Methods in Wrapper Classes,Constructors: Integer intVal = new Integer(i); Float floatVal = new Float(f); Converting objects to basic values int i = intVal.intValue(); float f = floatValue.floatValue(); Converting Numbers to Strings str = Integer.toString(i) str = Float.toStrin(f);,28,Method

25、s in Wrapper Classes,String Objects to Numeric Objectrs Integer intVal = Integer.ValueOf(str); Float floatVal = Float.ValueOf(str); Numeric Strings to Basic Types int i = Integer.parseInt(str); long l = Long.parseFloat(str) These methods throw exception (NumberFormatException) if the value of the st

26、r does represent an integer. Exception are a OO way of reporting errors. More on it later.,29,Summary,A special method, toString(), can be defined in any Java class, which gets invoked when one tries to concatenation operation with Strings. Collections are like arrays, but can hold any objects, dynamically expandable, and supports their easy manipulation. Java has strong support for Collections, which are very useful when developing large-scale software development. Wrapper classes helps in manipulating basic data types as Objects.,

copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1