欢迎来到麦多课文档分享! | 帮助中心 海量文档,免费浏览,给你所需,享你所想!
麦多课文档分享
全部分类
  • 标准规范>
  • 教学课件>
  • 考试资料>
  • 办公文档>
  • 学术论文>
  • 行业资料>
  • 易语言源码>
  • ImageVerifierCode 换一换
    首页 麦多课文档分享 > 资源分类 > PPT文档下载
    分享到微信 分享到微博 分享到QQ空间

    Arrays, Strings and Collections [2].ppt

    • 资源ID:378574       资源大小:117.50KB        全文页数:29页
    • 资源格式: PPT        下载积分:2000积分
    快捷下载 游客一键下载
    账号登录下载
    微信登录下载
    二维码
    微信扫一扫登录
    下载资源需要2000积分(如需开发票,请勿充值!)
    邮箱/手机:
    温馨提示:
    如需开发票,请勿充值!快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如需开发票,请勿充值!如填写123,账号就是123,密码也是123。
    支付方式: 支付宝扫码支付    微信扫码支付   
    验证码:   换一换

    加入VIP,交流精品资源
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    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.,


    注意事项

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




    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

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

    收起
    展开