java API

1         String StringBuffer

1String类的对象表示的是字符串常量,一个字符串常量创建以后就不能够被修改了。所以在创建String类对象时,通常需要向构造函数传递参数来指定创建的字符串的内容。

  如创建一个String对像:String str=”abc”   String str=new String(“abc”).

    StringBuffer类对象表示的是字符串变量,每一个StringBuffer类对象都是可以扩充和修改的字符串变量。如:new StringBuffer("Hello").setCharAt(1,'E') => "HEllo"

String类中的方法:(1) public String concat(String str) 字符串连接函数-–>"Hello".concat(" World!") => "Hello World!“ (2) public int length()-->求字符转函数"Hello".length() => 5  (3) public char charAt(int index)—>求字符串中某一位置的字符" Hello".charAt(1) => 'H' (4) public boolean equalsObject anObject)字符转比较函数—"Hello".equals("Hello") => true 等方法

    public static void main(String[] args){

       String s="abcdefgh";

       int l=s.length();

       System.out.println("字符串s的长度为:"+l);

       for (int i=0;i<l;i++){

              char c=s.charAt(i);

              System.out.println("s中的第"+i+"个字符是:"+c);

       }    

   }

}

 2StringBuffer类提供两组方法用来扩充StringBuffer对象所包含的字符,分别是

1public StringBuffer appendObject  obj 如:new StringBuffer("Hello:").append(2) => "Hello:2"

2public StringBuffer insertint 插入位置,参数对象类型  参数对象名)

StringBuffer类对象的修改通过

   public void setCharAt(int index, char ch)

 该方法将当前StringBuffer对象中的index位置的字符替换为指定的字符ch

       如:new StringBuffer("Hello").setCharAt(1,'E') => "HEllo"

 

  例子:public class StringBufferOp{

    public static void main(String[] args){

        String s = "0123456789";

        System.out.println("原来s="+s);

        int i = s.length();

        StringBuffer buffer = new StringBuffer(i);

        for(int j=i-1;j>=0;j--){

            buffer.append(s.charAt(j));

        }

        System.out.println("反转后s="+buffer);

    }

}

3StringStringBuffer的比较

String 对象的內容是不可以被更改;而 StringBuffer 的对象內容则是可以被更改的。

String 类型的字串其长度是固定的;而 StringBuffer 类型的字串其长度是变动、並非固定的。

举例说明:public class TestString {

      public static void main(String[] args) {

       String s = “Ad123//abc”;

       StringBuffer sb = “Ad123//abc”;

       // 不可将字符串常量直接赋值给 StringBuffer变量

       StringBuffer sb = new StringBuffer(“Ad123//abc”);

       // 将字串中的小写字符改变为大写

       System.out.println(s.toUpperCase());

       // 原始字串中的內容并未改变

       System.out.println(s);

       // 將字串中字符的順序反转

       System.out.println(sb.reverse());

       System.out.println(sb);

       }

 }

4StringStringBuffer的转换

  创建一个StringBuffer对象: StringBuffer sb = new StringBuffer();   sb转换成字符串String sb = sb.toSting();

 

2         基本数据类型---相对应的包装类

包括boolean –Boolean

byte---- Byte

char----Character

short----Short

 int----Integer

 long ----Long

float----Float

     double----Double

String转化成int有三种方法:

1  int s1=new Integer(args[0]).intValue();

       2 int s2=Integer.parseInt(args[0]);

    3 int s3=Integer.valueOf(args[0]).intValue();

 

3        集合

 定义:集合类用于存储一组对象,是一种容器对象,用于按照一定的规则在其中保存一组对象,其中每个对象称之为元素,通常用的有Vector Enumeration ArrayList Collection  Iterator Set List 等集合类和接口

(1)       Vector类和Enumeration接口

    Vector 类可以实现可增长的对象数组。与数组一样,它包含可以使用整数索引进行访问的组件。但是,Vector 的大小可以根据需要增大或缩小,以适应创建 Vector 后进行添加或移除项的操作。

  例题:将键盘上输入的一个数字序列中的每位数字存储在Vector对象中,然后在屏幕上打印出每位数字相加的结果,例如:输入123,打印出5,输入1234,打印出10.

      package com.Text4;

import java.util.Enumeration;

import java.util.Vector;

 

public class TextVector {

 

    /**

     * @param args

     */

    public static void main(String[] args) {

       // TODO Auto-generated method stub

       Vector v=new Vector();

       try{

           while(true){

       int b=System.in.read();

       if(b=='/n'||b=='/r'){

           break;

       }else{    

           int num=b-'0';

               v.addElement(new Integer(num));

           }

       }

      }  

              catch(Exception e){

                  }

       int sum=0;

    Enumeration e=v.elements();

    while(e.hasMoreElements()){

        Integer inte=(Integer)e.nextElement();

        sum+=inte.intValue();

    }

    System.out.println(sum);

       }

}

(2)       Collection接口和Iterator接口

   Collection 表示一组对象,这些对象也称为 collection 元素。一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的。

  同样用上面的例子:package com.Text4;

 

import java.util.Enumeration;

import java.util.ArrayList;

import java.util.Iterator;

 

public class TextCollection {

 

    /**

     * @param args

     */

    public static void main(String[] args) {

       // TODO Auto-generated method stub

       ArrayList v=new ArrayList();

       try{

           while(true){

       int b=System.in.read();

       if(b=='/n'||b=='/r'){

           break;

       }else{    

           int num=b-'0';

               v.add(new Integer(num));

           }

       }

      }  

              catch(Exception e){

                  }

    int sum=0;

    Iterator e=v.iterator();

    while(e.hasNext()){

        Integer inte=(Integer)e.next();

        sum+=inte.intValue();

    }

    System.out.println(sum);

       }

    }

4        Collection  Set  List的区别

-Collection 各元素对象之间没有指定的顺序,允许有重复的元素和多个null元素对象

-Set 各元素对象之间没有指定的顺序,不允许有重复的元素,最多允许有一个null元素对象

-List 各元素对象之间有指定的顺序,允许有一个重复元素和多个null原速度 对象

 

举例说明:package com.Text4;

 

import java.util.ArrayList;

import java.util.Collections;

 

public class TextSort {

 

/**

 * @param args

 */

public static void main(String[] args) {

    // TODO Auto-generated method stub

     

    ArrayList a1=new ArrayList();

    a1.add(1);

    a1.add(3);

    a1.add(2);

    System.out.println(a1.toString());

    Collections.sort(a1);//a1进行自然升序排序

    System.out.println(a1.toString());

}

}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值