Arrays, Strings and Collections [2].ppt
《Arrays, Strings and Collections [2].ppt》由会员分享,可在线阅读,更多相关《Arrays, Strings and Collections [2].ppt(29页珍藏版)》请在麦多课文档分享上搜索。
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
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ARRAYS STRINGSANDCOLLECTIONS2PPT
