一:String类和StringBuffer类
1.位于java.lang包中。
2.String类对象中的内容一旦被初始化就不能再改变。
3.StringBuffer类用于封装内容可以改变的字符串。
4.字符串常量(如"hello")实际上是一种特殊的匿名String对象。比较下面两种情况的差异:
String s1 = "hello";
String s2 = "hello";
String s1 = new String("hello");
String s2 = new String("hello");
s1和s2两个指向的不是同一个对象,故输出的结果为false。
5.String类的常用成员方法
(1). 构造方法:String(byte[] bytes,int offset,int length)
(2). equalslgnoreCase方法:此方法是忽略大小写。
(3). indexOf(int ch)方法
(4). substring(int beginIndex)方法;
substring(int beginIndex,int endIndex);
实例1:
逐行读取键盘输入,直到输入内容为"stop"时,程序结束:
public class ReadLine { public static void main(String[] args){ byte[] buf = new byte[1024]; String str = null; int pos =0; int ch = 0; System.out.println("请输入:"); while(true){ try{ ch = System.in.read(); }catch(Exception e){ e.printStackTrace(); } switch(ch){ case '\r': break; case '\n': str = new String(buf,0,pos); if(str.equalsIgnoreCase("stop")){ return; }else{ System.out.println(str); pos=0; break; } default: buf[pos++] = (byte)ch; } } } }
实例2:
public class Demo { public static void main(String[] args) { System.out.println("hello world".indexOf('o')); } } public class Demo2 { public static void main(String[] args) { System.out.println("hello world".indexOf('o',5)); } }
实例3:
public class Demo3 { public static void main(String[] args) { System.out.println("hello world".substring(6)); } } public class Demo4 { public static void main(String[] args) { System.out.println("hello world".substring(6,8)); } }
二:集合类
集合类用于存储一组对象,其中的每个对象称之为元素,经常会用到的有Vector、Enumeration,ArrayList,Collection,Iterator,Set,List等集合类和接口。
1.Vector类与Enumeration接口
实例1:
将键盘上输入的一个数字序列中的每位数字存储在Vector对象中,然后再屏幕上打印出每位数字相加的结果, 如:输入1234,打印出10。
import java.io.IOException; import java.util.Enumeration; import java.util.Vector; public class Demo { public static void main(String[] args) { int b=0; Vector v = new Vector(); System.out.println("请输入数字:"); while(true){ try { b = System.in.read(); if(b =='\r' || b =='\n'){ break; }else{ int num = b-'0'; v.addElement(new Integer(num)); } } catch (IOException e) { e.printStackTrace(); } } int sum = 0; Enumeration e = v.elements(); while(e.hasMoreElements()){ Integer intObj = (Integer)e.nextElement(); sum +=intObj.intValue(); } System.out.println(sum); } }
2.Collection接口与Iterator接口:
实例2:
改写上一个例子。
import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; public class Demo2 { public static void main(String[] args) { int b=0; ArrayList list = new ArrayList(); System.out.println("请输入数字:"); while(true){ try { b = System.in.read(); if(b =='\r' || b =='\n'){ break; }else{ int num = b-'0'; list.add(new Integer(num)); } } catch (IOException e) { e.printStackTrace(); } } int sum = 0; Iterator e = list.iterator(); while(e.hasNext()){ Integer intObj = (Integer)e.next(); sum +=intObj.intValue(); } System.out.println(sum); } }
3.Collection,Set,List的区别如下:
(1).Collection各元素对象之间没有指定的顺序,允许有重复元素和多个null元素对象。
(2).Set各元素对象之间没有指定的顺序,不允许有重复元素,最多允许有一个null元素对象。
(3).List各元素对象之间有指定顺序,允许有重复元素和多个null元素对象。
实例3:
import java.util.ArrayList; import java.util.Collections; public class Demo { public static void main(String[] args) { ArrayList a1 = new ArrayList(); a1.add(new Integer(1)); a1.add(new Integer(3)); a1.add(new Integer(2)); System.out.println(a1.toString()); Collections.sort(a1); System.out.println(a1.toString()); } }