• java第六节 字符串/集合


    /*
     *String类和StringBuffer类
     * 位于java.lang包中
     * String类对象中的内容一旦被初始化就不能再改变
     * StringBuffer类中用于封装内容可以改变的字符串
     *   toString()方法转换成String类型
     *   String x= "a" + 4 + "c"; 编译时等效于
     *   Stirng x = new StringBuffer().append("a").append(4).append("C").toString();
     *
     *字符串常量(如"hello")实际上是一种特殊的匿名String对象,比较下面的两种情况差导
     *   String s1 = "hello"; String s2 = "hello";
     *   这种情况这两个匿名函数是相等的,因为会将内容相同的常量指向同一个地址
     *   String s1 = new String("hello");
     *   String s2 = new String("hello");
     *   这种情况两种类型是不相等的,因为内存地址不一样
     *
     * 编程实例:逐行读取键盘输入,直到输入内容为"bye"时 ,结束程序
     *
     *
     *String 类的常州用成员方法
     *  构造方法:
     *    String(byte[] bytes, int offset, int length);
     *
     * equalsIgnoreCase 方法与equal方法一样,只是不区分大小写
     *
     * indexOf(int ch) 方法,用于返回一个字符大某一个字符串中首次出现的位置
     *
     * substring(int beginIndex)方法
     * substring(int beginIndex, int endIndex);
     *
     *
     *
     * 基本数据类型的对象包装类
     *
     * 基本数据类型包装类的作用
     *    基本数据类型       包装类
     *        boolean          Boolean
     *        byte             Byte
     *        char             Character
     *        short            Short
     *        int              Integer
     *        long             Long
     *        float            Float
     *        double           Double
     *
     *
     * 将字符串转换成整数的编程举例
     *   在屏幕上打印出一个星号(*)组成的矩形,矩形的宽度和高度通过
     *启动程序时传递给main方法的参数指定,并比较下面两段代码的运行效率
     *
     *  String sb = new String();
     *  for(int j=0; j<w; j++)
     *  {
     	     sb = sb + '*';
     	}
    
     	StringBuffer sb = new StringBuffer();
     	for(int j=0; j<w; j++)
     	{
     	    sb.append('*');
     	}
     	*
     	*
     	*
     	*
     	* 集合类
     	* 集合类用于存储一组对象,其中的每个对象称之为元素
     	* 经常会用到的有Vector, Enumeration, ArrayList
     	* Collection lterator, Set, List等集合类和接口
     	*
     	* 1 Vector类与Enumeration接口
     	*   编程举例q:将键盘上输入的一个数字序列中的每位数字存储在Vector对象中,然后在屏幕上打印出每位数字相加的结果
     	*   例如:输入32 打印出5
     	*   首先输入的整数也就是ASSIC码转换为Integer对象保存到Vector中
     	*   然后用Enumberation接口方法来循环整个Vector
     	*   Enumberation.hasMoreElements();如果没有元素时返回false,如果还有元素返回true
     	*   Enumberation.nextElements(); 返回当前位标的值
     	*
     	*
     	*
     	*
     	*Collection接口与Iterator接口
     	*   编程例举:用ArrayList和Iterator改写上面的例子程序
     	*
     	*
     	*Collection Set, List 的区别如下
     	*   Collection 是Set List的父类
     	*
     	*   Collection各元素对象之间没有指定的顺序,允许有重复元素和多个null元素对象
     	*   Set各元素对象之间没有指定的顺序,
     	*      不允许有重复元素,最多允许有一个null元素对象
     	*   List各元素对象之间有指定的顺序,允许有重复元素和多个null元素对象
     	*
     	*   ArrayList也是实现了一个List的一个类
     	*
     	*
     	*
     	* hashTable类
     	*  Hashtable类不仅可以象Vector一样动态存储一系列的对象
     	*  而且对存储的每一个对象(称为值)都要安排一个对象(
     	   称为键字)与之相关支付
           Hashtable numbers = new Hashtable();
           numbers.put("one", new Integer(1));
           numbres.put("two", new Integer(2));
           numbers.put("three", new Integer(3));
           Integer n = (Integer)numbres.get("two");
           if(n!=null){
                System.out.println("two="+n);
           }
    
     	*
     	* 用作关键字的类必须覆盖Object.hashCode方法和
     	* Ojbect.equals方法
     	*
     	*
     	*
     	*Properties类
     	*   Properties类是hashTable的子类
     	*   增加了将Hashtable对象中的关键字和值保存到文件和从文件中读取关键字的值到Hashtable对象中的方法
     	*
     	* 如果要用Properties.store方法存储Properties对象中的内容,每个属性的关键字和值都必须是String类型
     	*
     	* 编程举例:使用Properties把程序的启动运行次数记录在某个文件中,每次运行时打印出它的运行次数
     	*
     	*
     	*
     	*System与Runtime类
     	*
     	*System类
     	*   exit方法
     	*   currentTimeMillis方法
     	*   Java虚拟机的系统属性
     	*      设备虚拟机的系统属性
     	*          java -DAAA=b -DBBB=a MyClass
     	*   getProperties和setProperties方法主要是来获取或设置java虚拟机中的系统属性
     	*
     	*
     	*Runtime类
     	*   Runtime.getRuntime静态方法
     	*
     	*编程实例:在java程序中启动一个Windows记事本程序的支行实例
     	*并在该运行实例中打开这个java程序的源文件
     	*启动的记事本程序5秒钟后被关闭
     	*
     	*
     	*与日期和时间有关的类
     	*   最常用的几个类: Date, DateFormat和Calendar
     	*
     	*Calendar类
     	*   Calendar.add方法
     	*   Calendar.get方法
     	*   Calendar.set方法
     	*   Calendar.getInstance静态方法
     	*   GregorianCalendar子类
     	*
     	* 编程实例:
     	*   计算出距当前日期时间315天后的日期时间
     	*   并用"XXXX年xx月xx日xx小时: xx分: xx秒"的格式输出
     	*
     	* Date类
     	* java.text.DataFormat与java.text.SimpleDateFormat子类
     	* 编程实例:
     	*   将"2002-03-15"格式的日期字符串转换成
     	*   "2002年03月15日"的格式
     	*
     	*
     	*Timer与TimerTask类
     	*   schedule方法主要有如下几种重载形式:
     	*      schedule(TimerTask task, long delay)
     	*      schedule(TimerTask task, Date time)
     	*      schedule(TimerTask task, long delay, long period)
     	*      schedule(TimerTask task, Date firstTime, long period)
     	*
     	*TimerTask类实现了Runnable接口,要执行的任务由它里面实现的run方法来完成
     	*
     	* 编程实例,程序启动运行后30秒启动Windows自带的计算器程序
     	*
     	*
     	*Math与Random类
     	*    Math类包含了所有用于几何和三角运算的方法
     	*    Random类是一个伪随机数产生器
     	*
     	* 学习API的方法
     	*    有了某一领域的专业知识,再参看一些范例程序,才能更容易掌握和理解一些新的API类
     	*    不要看什么Java API大全之类的书籍
     	*    结交一些程序员朋友,或上一些技术论坛
     	*    不能纸上谈兵,要敢于动手实践
     	*
     	* 1  简述一下你是如何理解API的
     	*    就是java为开发者提供大量的类,方便开发者调用
     	*
     	* 2  当你要接着以前保存的一个工程继承工和时,应该用JCreator Pror打开工程主目录下的哪里个文件呢?
     	*    .jcp文件
     	*
     	*
     	* 3 查阅JDK文档,通读String和StringBuffer这两个类的所有方法,总结一下这两个类能对字符串进行哪里些处理,了解String类的所有方法后,
     	*   如果有人问:"String类对象中的内容一旦被初始化就不能改变,那么String类中怎么还会有replace和toUpperCase方法呢?这两个方法都要改变字符串中的内容啊?"
     	*   你该如何回答这个问题,
     	*   因为replace以后返回的是一个新的new String对象,
     	*   是否可以这样理解,string里面的都是常量,那么a = string("bac"); 与 b = string("a");中的a的内存地址是一样的?
     	*
     	*   除了可以在JDK文档中仔细阅读这两个方法的帮助外,还可以从JDK安装目录下的src.zip文件件中,查看java.lang.String类的源文代码,了解这两个方法的内部实现
     	*
     	*4 在JDK文档中查看Integer类的帮助,至少例出将字符串转换成整数的三种方式
     	*  查看HomeWork项目
     	*
     	*5 Vector和ArrayList的有什么重要的区别,在什么情况下该使用Vector,在什么情况下该使用ArrayList?
     	*  区别:Vector来能数值为null的值
     	*        但ArrayList可以有多个值为null和键值
     	*   
     	*
     	*6 编写一个能用作Hashtable关键字的类,其中包含String name 和int age这两个成员变量,并编写出验证该关键类是否正确的测试代码
     	*
     	*
     	*7 编写打印当前虚拟机的所有系统属性的程序,并在启动这个程序时,为Java虚拟机增加一个系统属性
     	*
     	*8 为什么时候Runtime类被设计成不能在程序中直接创建它的实例对000000000000000000000000000000000000000000000000000000000000000000象?java设计者又是通过什么样的方式来保证在程序中只能有一个Runtime实例对象的呢?
     	*
     	*9 修改前面讲解的Timer与TimerTask类的例子程序代码,让该程序启动windows自带的计算器程序后立即结束
     	*
     	*
     **/
    package org.it315.sencondproj;
    
    
    public class SencondDemo {
    	public int x = 1;
    	/**
    	 * Method main
    	 *
    	 *
    	 * @param args
    	 *
    	 */
    	public static void main(String[] args) {
    		// TODO: Add your code here
    		if(args.length > 0)
    		{
                System.out.println("the first Param is "+args[0]);
    		}else{
    		    new SencondDemo().callA(new A());
    		}
    	}
    
    	/**
    	 * Method callA
    	 *
    	 *
    	 * @param a
    	 *
    	 */
    	public static void callA(A a) {
    		// TODO: Add your code here
    		a.sayHello();
    	}
    }
    

     Hashtable

    import java.util.*;
    
    public class HashtableTest {
    
    	/**
    	 * Method main
    	 *
    	 *
    	 * @param args
    	 *
    	 */
    	public static void main(String[] args)
    	{
    		// TODO: Add your code here
    
    		Hashtable numbers = new Hashtable();
    		numbers.put(new MyKey("张三",18), new Integer(1));
    		numbers.put(new MyKey("李四",15), new Integer(2));
    		numbers.put(new MyKey("王五",20), new Integer(3));
    
            //取得所有数据
            Enumeration e = numbers.keys();
            while(e.hasMoreElements())
            {
                 MyKey key = (MyKey)e.nextElement();
                 System.out.print(key+"=");
                 System.out.println(numbers.get(key));
            }
    
            //如果没有覆盖hashcode()方法跟equal方法的话,那么这个第三取出来将为空值
            System.out.println(numbers.get(new MyKey("张三",18)));
    	}
    }
    
    
    
    public class MyKey
    {
    
    	private String name = null;
    
    	private int age = 0;
    
    	public MyKey(String name, int age)
    	{
    	    this.name = name;
    	    this.age = age;
    	}
    
    
    	public boolean equals(Object obj)
    	{
    		if(obj instanceof MyKey)
    		{
               MyKey objTemp = (MyKey)obj;
               if(name.equals(objTemp.name) && age==objTemp.age)
               {
                  return true;
               }else{
                  return false;
               }
    		}else{
    		   return false;
    		}
    	}
    
    	public int hashCode()
    	{
    		 //StrinbBuffer类不能用做关键字类
             return name.hashCode() + age;
    	}
    
    	public String toString()
    	{
    	     return "name:"+this.name+", age:"+this.age;
    	     //return name+", "+age;
    	}
    }
    

      Properties

    import java.util.*;
    import java.io.*;
    
    
    public class PropertiesFile {
    
    	/**
    	 * Method main
    	 *
    	 *
    	 * @param args
    	 *
    	 */
    	public static void main(String[] args)
    	{
    		// TODO: Add your code here
    
    		long startTime = System.currentTimeMillis();
    
    
    		Properties settings = new Properties();
    		try{
    			settings.load(new FileInputStream("count.txt"));
    		}catch(Exception e){
    			//e.printStackTrace();
    			settings.setProperty("count",String.valueOf(0));
    		}
    		//settings.get("count");
    		int c = Integer.parseInt(settings.getProperty("count")) +1;
            System.out.println("这是第:"+c+"次运行");
    
            //settings.put("count",new Integer(c).toString());
            settings.setProperty("count",new Integer(c).toString());
            try{
            	settings.store(new FileOutputStream("count.txt"),"Program is used:");
            }catch(Exception e){
                e.printStackTrace();
            }
    
            long endTime = System.currentTimeMillis();
            System.out.print("动行了:"+(endTime - startTime)+"秒");
    
    	}
    }
    

      Calendar

    import java.util.*;
    import java.text.SimpleDateFormat;
    
    public class TestCalendar {
    
    	/**
    	 * Method main
    	 *
    	 *
    	 * @param args
    	 *
    	 */
    	public static void main(String[] args)
    	{
    		// TODO: Add your code here
    		Calendar c1 = Calendar.getInstance();
    		System.out.println(c1.get(Calendar.YEAR)+"年"+c1.get(Calendar.MONTH)+"月"+c1.get(c1.DAY_OF_MONTH)+"日 "+c1.get(c1.HOUR)+":"+c1.get(c1.MINUTE)+":"+c1.get(c1.SATURDAY));
    
    
            c1.add(c1.DAY_OF_YEAR,315);
            System.out.println(c1.get(Calendar.YEAR)+"年"+c1.get(Calendar.MONTH)+"月"+c1.get(c1.DAY_OF_MONTH)+"日 "+c1.get(c1.HOUR)+":"+c1.get(c1.MINUTE)+":"+c1.get(c1.SATURDAY));
    
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd");
    
            try{
    	        Date d = sdf1.parse("2003-03-15");
    	        System.out.println(sdf2.format(d));
            }catch(Exception e){
                e.printStackTrace();
            }
            //System.out.println(d.toString());
    
    
    
            Timer tm =new Timer();
            tm.schedule(new MyTimerTask(tm),3000);
    
            //new Timer(true).schedule(
            //},
            //3000);
    
    	}
    }
    
    
            class MyTimerTask extends TimerTask
            {
            	private Timer tm = null;
    
            	public MyTimerTask(Timer tm)
            	{
            	   this.tm = tm;
            	}
            	public void run()
            	{
            		try{
    	        		Runtime.getRuntime().exec("calc.exe");
            		}catch(Exception e){
            			e.printStackTrace();
            		}
            		//加载结束任务线程
            		//System.exit();
            		this.tm.cancel();
            		//TimerTask.cancel();
            	}
            }
    

      Integer

    public class TestInteger {
    
    	/**
    	 * Method main
    	 *
    	 *
    	 * @param args
    	 *
    	 */
    	public static void main(String[] args)
    	{
    		// TODO: Add your code here
    		int w = new Integer(args[0]).intValue();
    		int h = Integer.parseInt(args[1]);
    		//int h = Integer.valueOf(args[1]).intValue();
    		//将字符串转换为整数的三种方法
    
    		for(int i=0; i<h; i++)
    		{
    			 StringBuffer sb = new StringBuffer();
    		     for(int j=0; j<w; j++){
    		     	sb.append('*');
    		     }
    		     System.out.println(sb.toString());
    		}
    	}
    }
    

      Vector /ArrayList

    //import java.util.Vector;
    //import java.util.Enumeration;
    import java.util.*;
    
    public class TestCollection {
    
    	/**
    	 * Method main
    	 *
    	 *
    	 * @param args
    	 *
    	 */
    	public static void main(String[] args) {
    		// TODO: Add your code here
    		ArrayList v = new ArrayList();
    		System.out.println("请输入一串数值:");
    		while(true)
    		{
    		    int b =0;
    		    try{
    			    b = System.in.read();
    			}catch(Exception e){
    			    e.printStackTrace();
    			}
     			//其实这里的int b是对应的字母的ASSIC码的值,所以不能直接将它保存到Vector中去
    			if(b=='
    ' || b=='
    ')
    			{
    			    break;
    			}else{
    	            int number = b-'0';
    	            v.add(new Integer(number));
    			}
    		}
    
    		int sum = 0;
    		//Enumeration e = v.elements();
    		//(Integer)e.nextElement(); 不是返回下一个对象,而是返回指标器正指向的对象
            //e.hasMoreElements();如果没有对象将返回false,如果还有对象将返回true
            Iterator e = v.iterator();
            //while(e.hasMoreElements())
            while(e.hasNext())
            {
                 //Integer intObj = (Integer)e.nextElement();
                 Integer intObj = (Integer)e.next();
                 sum += intObj.intValue();
            }
            System.out.println("相加总和为:"+sum);
    	}
    }
    
    
    import java.util.*;
    
    public class TestSort {
    
    	/**
    	 * Method main
    	 *
    	 *
    	 * @param args
    	 *
    	 */
    	public static void main(String[] args) {
    		// TODO: Add your code here
    
            ArrayList al = new ArrayList();
            al.add(new Integer(1));
            al.add(new Integer(5));
            al.add(new Integer(4));
            al.add(new Integer(3));
            al.add(new Integer(2));
            al.add(new Integer(9));
            System.out.println("排序前:");
            System.out.println(al.toString());
    
            Collections.sort(al);
            System.out.println("排序后:");
            System.out.println(al.toString());
            //Collections主要操作集合类对象,一般的方法的是静态类
    
    
    
    
    
    	}
    }
    
    
    import java.util.Vector;
    import java.util.Enumeration;
    
    public class TestVector {
    
    	/**
    	 * Method main
    	 *
    	 *
    	 * @param args
    	 *
    	 */
    	public static void main(String[] args) {
    		// TODO: Add your code here
    		Vector v = new Vector();
    		System.out.println("请输入一串数值:");
    		while(true)
    		{
    		    int b =0;
    		    try{
    			    b = System.in.read();
    			}catch(Exception e){
    			    e.printStackTrace();
    			}
     			//其实这里的int b是对应的字母的ASSIC码的值,所以不能直接将它保存到Vector中去
    			if(b=='
    ' || b=='
    ')
    			{
    			    break;
    			}else{
    	            int number = b-'0';
    	            v.addElement(new Integer(number));
    			}
    		}
    
    		int sum = 0;
    		Enumeration e = v.elements();
    		//(Integer)e.nextElement(); 不是返回下一个对象,而是返回指标器正指向的对象
            //e.hasMoreElements();如果没有对象将返回false,如果还有对象将返回true
            while(e.hasMoreElements())
            {
                 Integer intObj = (Integer)e.nextElement();
                 sum += intObj.intValue();
            }
            System.out.println("相加总和为:"+sum);
    	}
    }
    

      

  • 相关阅读:
    JAVA语法之小结
    JAVA之经典Student问题1
    Android之动画1
    Android之屏幕测试
    Android之点击切换图片
    Android之标签选项卡
    Android简单计算器
    Javascript之相册拖动管理
    Javascript之改变盒子颜色
    CSS之照片翻转
  • 原文地址:https://www.cnblogs.com/xiangxiaodong/p/3221986.html
Copyright © 2020-2023  润新知