当前位置:
文档之家› Java基础培训(第5章 数组、Vector与字符串)
Java基础培训(第5章 数组、Vector与字符串)
Java基础培训
辽宁桑地公司 朱雷
第5章 数组、Vector与字符串
数组
定义:是一组有序数据的集合
int i[]; char c[]; float f[][];
数组声明: 数组类型 数组名 [] 数组类型 [] 数组名
数组创建: char s[] = new char[10]; //创建一个10个内容的数组 int[] itable = {1, 2, 3, 48, 9}; //创建一个5个内容的数组,并初始化
检索字符串的位置indexOf(),lastIndexOf()
String str = "This is a String"; int index1 = str.indexOf("i"); //index1=2 int index2 = str.indexOf("i",index1+1); //index2=5 int index3 = stIndexOf("i"); //index3=13 int index4 = str.indexOf("String"); //index4=10 int index5 = str.indexOf("p"); //index5=-1
第5章 数组、Vector与字符串
改变Vector的大小
//获取Vector的元素个数 public final int size(); //获取Vector的容量 public final int capacity(); //设置容量 public final synchronized void ensureCapatity(int minimumCapacity); //将容量减少到元素个数 public final synchronized void trimToSize(); //改变元素个数,如果比原来小则元丢失,如果多,则新增加为null public final synchronized void setSize(int newSize); 作业2:创建一个大小为4的Vector,向里面添加" China"," England"," Franch"," Germany",打印出所有数据、Vector的元素个数和容量, ,在索 引2位置添加一个" America",打印所有数据、Vector的元素个数和容量, 删除索引3位数据后再打印所有数据、Vector的元素个数和容量
第5章 数组、Vector与字符串
向量Vector
访问和查找Vector中的数据
//查找最后一个出现的位置,找不到返回-1 public final int lastIndexOf(Object ob); //查找从startIndex开始,第一次出现的位置 public final synchronized Object indexOf(Object ob, int startIndex) throws ArrayIndexOutOfBoundsException; //查找从startIndex开始,最后一次出现的位置 public final synchronized Object lastIndexOf(Object ob, int startIndex) throws ArrayIndexOutOfBoundsException;
二维数组创建: char s = new char[2][3]; //创建一个2*3的二维数组 int a = new int[3][]; a[0] = new int[3]; a[1] = new int[5]; a[2] = new int[7]; int[][] a = {{1,2},{3,4,5,6},{5,6,7}};
检索字符串开始startsWith(string prefix),结束 endsWith(String suffix)
第5章 数组、Vector与字符串
字符串的比较equals();
String s1 = "aaa"; String s2 = "aaa"; String s3= new String("aaa"); String s4 = new String("aaa"); System.out.println("s1==s2:" + (s1==s2)); System.out.println("s3==s4:" + (s3==s4)); System.out.println("s1.equals(s2):" + s1.equals(s2)); System.out.println("s3.equals(s4):" + s3.equals(s4));
第5章 数组、Vector与字符串
字符串
字符串的定义:
String s1 = "This is a String"; String s2 = new ("This is a String");
可变长字符串StringBuffer
public StringBuffer(); //创建一个空的可变长字符串 public StringBuffer(int length); //创建一个一定长度的可变长字符串 public StringBuffer(String str); //创建一个指定初值的可变长字符串
第5章 数组、Vector与字符串
向量VectorLeabharlann 访问和查找Vector中的数据
//访问某个数据 public final synchronized Object elementAt(int index) throws ArrayIndexOutOfBoundsException; //查找第一个元素数据 firstElement(); //查找最后一个数据; lastElement(); //查找是否为空 isEmpty(); //查找是否包含某个元素 public final boolean contains(Object ob); //查找元素第一个出现的位置,找不到返回-1 public final int indexOf(Object ob);
第5章 数组、Vector与字符串
向量Vector(增加和删除数据)
//向向量最后增加一个数据 public final synchronized void addElement(Object newElement); //把新对象插入到index位置 public final synchronized void inssertElementAt(Object newElement,int index) thorws ArrayIndexOutOfBoundsException //改变某一位置的数据 public final synchronized void setElementAt(Object newElement,int index) thorws ArrayIndexOutOfBoundsException //删除所有数据 public final synchronized void removeAllElements(); //删除一个特定的对象,如果有多个,则只移出第一个 public final synchronized boolean removeElement(Object ob) //删除指定位置上的数据 public final synchronized void removeElementAt(int index) thorws ArrayIndexOutOfBoundsException
获取字符串的长度方法length()
String s = "This is a String"; int length = s.length();
第5章 数组、Vector与字符串
字符串
获取StringBuffer的容量capacity()方法
StringBuffer s = new StringBuffer("This is a String"); int cap = s.capacity();
二维数组下标也是从0开始 作业1:创建一个2维数组,并将各个数据打印 出来
第5章 数组、Vector与字符串
向量Vector
与数组类似,可以存储多个对象 采用索引来检索存储的对象 长度会自动增加 提供了额外的增加和删除元素的方法
Vector的三种构造方法: public Vector(); //创建一个空的Vector public Vector(int initialcapacity);//创建一个初始大小为initialcapacity的 //Vector public Vector(int initialcapacity, int capacityIncrement);//创建一个初始大 //小为initialcapacity,每次递增capacityIncrement 的Vector
第5章 数组、Vector与字符串
修改可变长字符串
//添加数据append(); StringBuffer str = new StringBuffer("This is a String"); str.append(" Test"); //str= "This is a String Test" //插入数据 str.innsert(9,"test"); //str="This is atest Sring Test" ,书上有错误 //设置指定位置的字符 str.setCharAt(9,'B') //str = "This is aBest String Test"