16、StringBuffer类与包装类、java数组

来源:互联网 发布:walktour软件下载 编辑:程序博客网 时间:2024/04/29 18:10

字符串String是个常量,其对象一旦创建完毕就无法该改变,使用“+”进行拼接时,是生成了一个新的String对象,而不是向原有的String对象追加内容。字符串与任何其他原生数据类型变量相“+”,其他都先转换为字符串,然后相加。

StringBuffer初始化有16个字符长度,append()方法返回StringBuffer对象本身,toString()方法返回一个字符串。

1、包装类(Wrapper Class)。针对原生数据类型的包装。所有的包装类(8个)都位于java.lang包下。java中8个包装类分别是:Byte类、Short类、Long类、Float类、Double类、Character类和Boolean类。可以实现原生数据类型与包装类的双向转换。

2、数组(Array):相同类型数据的集合就叫做数组。如何定义数组:

type[] 变量名 = new type[数组中元素的个数];   或者   type 变量名[] = new type[]

int[]  a = new int[4];      int a[] = int[4];

数组中的元素索引是从0开始的,对于数组来说,最大的索引== 数组长度-1;

[java] view plaincopyprint?
  1. 数组的三种定义与初始化: 
[java] view plaincopyprint?
  1. public class ArrayTest 
  2.     public staticvoid main(String[] args) 
  3.     { 
  4.         int[] a = new int[4]; 
  5.          
  6.         a[0] = 1
  7.         a[1] = 2
  8.         a[2] = 3
  9.         a[3] = 4
  10.  
  11.         System.out.println(a[3]); 
  12.  
  13.         int b[] = newint[2]; 
  14.  
  15.         b[0] = 1
  16.         b[1] = 2
  17.  
  18.         int[] c = {1,2,3,4}; 
  19.  
  20.         System.out.println(c[2]); 
  21.  
  22.         int[] d = new int[]{1,2,3,4}; 
  23.  
  24.     } 


3、java中每个数组都有一个length属性,表示数组的长度。length属性是public ,final,int的。数组长度一旦确定,就不能改变大小。

[java] view plaincopyprint?
  1. int[] a = newint[4]; 
  2. a.lenght = 6;         //错误,length无法改变 
  3. int[] b = newint[4]; 
  4. System.out.println(a[0]);   //打印0 


没有赋初值,使用默认值。数组元素在内存中是连续存放的

int[] a = new int[10],其中a是一个引用,它指向了生成的数组对象的首地址,数组中每一个元素都是int类型,其中仅存放数据值本身。

4、当是一个引用类型的数组时,假定定义了一个Person类,定义一个Person数组:Person[]  p = new Person[3];

5、二维数组:二维数组是一种平面的二维结构。本质上是数组的数组。二维数组的定义方式:

Type[][] a = new type[2][3];

java允许定义不规整的二维数组。 

int [][] a = new int[3][];

a[0] = new int[2];

a[1] = new int[3];

a[2] = new int[1];

二维数组初始化:

int[][] a = new int[][]{{1,2,3}{4,5}{6,7,8,9}};

6、关于交换

[java] view plaincopyprint?
  1. public class Swap1 
  2.     public staticvoid swap(char[] ch,char c) 
  3.     { 
  4.         ch[0] = 'B'
  5.         c ='D'
  6.     } 
  7.  
  8.     public staticvoid swapInt(int[] i) 
  9.     { 
  10.         int temp = i[0]; 
  11.         i[0] = i[1]; 
  12.         i[1] = temp; 
  13.     } 
  14.     public staticvoid main(String[] args) 
  15.     { 
  16.         char[] ch ={'A','C'}; 
  17.  
  18.         Swap1.swap(ch,ch[1]); 
  19.  
  20.         for(int i =0;i < ch.length;i++) 
  21.         { 
  22.          
  23.         System.out.println(ch[i]); 
  24.         } 
  25.  
  26.         int[] i ={1,2}; 
  27.  
  28.         Swap1.swapInt(i); 
  29.          
  30.         for(int j =0;j < i.length;j++) 
  31.         { 
  32.          
  33.         System.out.println(i[j]); 
  34.         } 
  35.  
  36.     } 


7、如果定义了一个接口  interface I  {}

则I[] i = new I[2];是正确的。

8、java.util.Arrays类提供了大量数组方法共使用,这些方法都是静态的(static),可以直接通过类名加点的形式使用,就是Arrays.methods()方式使用。

9、java.lang.System类提供了很多静态数组方法,如arraycopy()等。

10、三维数组:int[][][] a = new int[2][3][4];

11、冒泡排序

[java] view plaincopyprint?
  1. public class BollList 
  2.     public staticvoid bollList(int[] a) 
  3.     { 
  4.         for(int i =0; i < a.length-1; i++) 
  5.         { 
  6.             boolean change =true
  7.  
  8.             for(int k =0; k < a.length - i -1; k++) 
  9.             { 
  10.                 if(a[k] > a[k+1]) 
  11.                 { 
  12.                     int temp = a[k+1]; 
  13.                     a[k+1] = a[k]; 
  14.                     a[k] = temp; 
  15.  
  16.                     change = false
  17.                 } 
  18.             } 
  19.              
  20.             if(change ==true
  21.             { 
  22.                 break
  23.             } 
  24.             for(int h =0;h < a.length;h++) 
  25.             { 
  26.                 System.out.print(a[h] + "  "); 
  27.             } 
  28.             System.out.println(); 
  29.              
  30.         } 
  31.     } 
  32.      
  33.     public staticvoid main(String[] args) 
  34.     { 
  35.         int[] a = newint[]{2,4,7,9,8}; 
  36.  
  37.         BollList.bollList(a); 
  38.  
  39.         for(int i =0; i < a.length; i++) 
  40.         { 
  41.             System.out.println(a[i]); 
  42.         } 
  43.     } 
0 0