java語言中,數組是一種最簡單的復合數據類型。數組是有序數據的集合,數組中的每個元素具有相同的數據類型,可以用一個統一的數組名和下標來唯一地確定數組中的元素。數組有一維數組和多維數組。
★ 一維數組
1. 一維數組的定義
type arrayName[ ];
類型(type)可以為Java中任意的數據類型,包括簡單類型和復合類型。
例如:
int intArray[ ];
Date dateArray[];
2.一維數組的初始化
◇ 靜態初始化
int intArray[]={1,2,3,4};
String stringArray[]={"abc", "How", "you"};
◇ 動態初始化
1)簡單類型的數組
int intArray[];
intArray = new int[5];
2)復合類型的數組
String stringArray[ ];
String stringArray = new String[3];/*為數組中每個元素開辟引用
空間(32位) */
stringArray[0]= new String("How");//為第一個數組元素開辟空間
stringArray[1]= new String("are");//為第二個數組元素開辟空間
stringArray[2]= new String("you");// 為第三個數組元素開辟空間
3.一維數組元素的引用
數組元素的引用方式為:
arrayName[index]
index為數組下標,它可以為整型常數或表達式,下標從0開始。每個數組都有一個屬性length指明它的長度,例如:intArray.length指明數組intArray的長度。
★多維數組
Java語言中,多維數組被看作數組的數組。
1.二維數組的定義
type arrayName[ ][ ];
type [ ][ ]arrayName;
2.二維數組的初始化
◇ 靜態初始化
int intArray[ ][ ]={{1,2},{2,3},{3,4,5}};
Java語言中,由于把二維數組看作是數組的數組,數組空間不是連續分配的,所以不要求二維數組每一維的大小相同。
◇ 動態初始化
1) 直接為每一維分配空間,格式如下:
arrayName = new type[arrayLength1][arrayLength2];
int a[ ][ ] = new int[2][3];
2) 從最高維開始,分別為每一維分配空間:
arrayName = new type[arrayLength1][ ];
arrayName[0] = new type[arrayLength20];
arrayName[1] = new type[arrayLength21];
…
arrayName[arrayLength1-1] = new type[arrayLength2n];
3) 例:
二維簡單數據類型數組的動態初始化如下,
int a[ ][ ] = new int[2][ ];
a[0] = new int[3];
a[1] = new int[5];
對二維復合數據類型的數組,必須首先為最高維分配引用空間,然后再順次為低維分配空間。
而且,必須為每個數組元素單獨分配空間。
例如:
String s[ ][ ] = new String[2][ ];
s[0]= new String[2];//為最高維分配引用空間
s[1]= new String[2]; //為最高維分配引用空間
s[0][0]= new String("Good");// 為每個數組元素單獨分配空間
s[0][1]= new String("Luck");// 為每個數組元素單獨分配空間
s[1][0]= new String("to");// 為每個數組元素單獨分配空間
s[1][1]= new String("You");// 為每個數組元素單獨分配空間
3.二維數組元素的引用
對二維數組中的每個元素,引用方式為:arrayName[index1][index2]
例如: num[1][0];
4.二維數組舉例:
【例2.2】兩個矩陣相乘
public class MatrixMultiply{
public static void main(String args[]){
int i,j,k;
int a[][]=new int [2][3]; //動態初始化一個二維數組
int b[][]={{1,5,2,8},{5,9,10,-3},{2,7,-5,-18}};//靜態初始化
一個二維數組
int c[][]=new int[2][4]; //動態初始化一個二維數組
for (i=0;i<2;i++)
for (j=0; j<3 ;j++)
a[i][j]=(i+1)*(j+2);
for (i=0;i<2;i++){
for (j=0;j<4;j++){
c[i][j]=0;
for(k=0;k<3;k++)
c[i][j]+=a[i][k]*b[k][j];
}
}
System.out.println("*******Matrix C********");//打印Matrix C標記
for(i=0;i<2;i++){
for (j=0;j<4;j++)
System.out.println(c[i][j]+" ");
System.out.println();
}
}
}
2.5 字符串的處理
★ 字符串的表示
Java語言中,把字符串作為對象來處理,類String和StringBuffer都可以用來表示一個字符串。(類名都是大寫字母打頭)
1.字符串常量
字符串常量是用雙引號括住的一串字符。
"Hello World!"
2.String表示字符串常量
用String表示字符串:
String( char chars[ ] );
String( char chars[ ], int startIndex, int numChars );
String( byte ascii[ ], int hiByte );
String( byte ascii[ ], int hiByte, int startIndex, int numChars );
String使用示例:
String s=new String() ; 生成一個空串
下面用不同方法生成字符串"abc":
char chars1[]={'a','b','c'};
char chars2[]={'a','b','c','d','e'};
String s1=new String(chars1);
String s2=new String(chars2,0,3);
byte ascii1[]={97,98,99};
byte ascii2[]={97,98,99,100,101};
String s3=new String(ascii1,0);
String s4=new String(ascii2,0,0,3);
3.用StringBuffer表示字符串
StringBuffer( ); /*分配16個字符的緩沖區*/
StringBuffer( int len ); /*分配len個字符的緩沖區*/
StringBuffer( String s ); /*除了按照s的大小分配空間外,再分配16個
字符的緩沖區*/
★訪問字符串
1.類String中提供了length( )、charAt( )、indexOf( )、lastIndexOf( )、getChars( )、getBytes( )、toCharArray( )等方法。
◇ public int length() 此方法返回字符串的字符個數
◇ public char charAt(int index) 此方法返回字符串中index位置上的字符,其中index 值的 范圍是0~length-1
◇ public int indexOf(int ch)
public lastIndexOf(in ch)
返回字符ch在字符串中出現的第一個和最后一個的位置
◇ public int indexOf(String str)
public int lastIndexOf(String str)
返回子串str中第一個字符在字符串中出現的第一個和最后一個的位置
◇ public int indexOf(int ch,int fromIndex)
public lastIndexOf(in ch ,int fromIndex)
返回字符ch在字符串中位置fromIndex以后出現的第一個和最后一個的位置
◇ public int indexOf(String str,int fromIndex)
public int lastIndexOf(String str,int fromIndex)
返回子串str中的第一個字符在字符串中位置fromIndex后出現的第一個和最后一個的位置。
◇ public void getchars(int srcbegin,int end ,char buf[],int dstbegin)
srcbegin 為要提取的第一個字符在源串中的位置, end為要提取的最后一個字符在源串中的位置,字符數組buf[]存放目的字符串, dstbegin 為提取的字符串在目的串中的起始位置。
◇public void getBytes(int srcBegin, int srcEnd,byte[] dst, int dstBegin)
參數及用法同上,只是串中的字符均用8位表示。
2.類StringBuffer提供了 length( )、charAt( )、getChars( )、capacity()等方法。
方法capacity()用來得到字符串緩沖區的容量,它與方法length()所返回的值通常是不同的。
★修改字符串
修改字符串的目的是為了得到新的字符串,類String和類StringBuffer都提供了相應的方法。有關各個方法的使用,參考java 2 API。
1.String類提供的方法:
concat( )
replace( )
substring( )
toLowerCase( )
toUpperCase( )
◇ public String contat(String str);
用來將當前字符串對象與給定字符串str連接起來。
◇ public String replace(char oldChar,char newChar);
用來把串中出現的所有特定字符替換成指定字符以生成新串。
◇ public String substring(int beginIndex);
public String substring(int beginIndex,int endIndex);
用來得到字符串中指定范圍內的子串。
◇ public String toLowerCase();
把串中所有的字符變成小寫。
◇ public String toUpperCase();
把串中所有的字符變成大寫。
2.StringBuffer類提供的方法:
append( )
insert( )
setCharAt( )
如果操作后的字符超出已分配的緩沖區,則系統會自動為它分配額外的空間。
◇ public synchronized StringBuffer append(String str);
用來在已有字符串末尾添加一個字符串str。
◇ public synchronized StringBuffer insert(int offset, String str);
用來在字符串的索引offset位置處插入字符串str。
◇ public synchronized void setCharAt(int index,char ch);
用來設置指定索引index位置的字符值。
注意:String中對字符串的操作不是對源操作串對象本身進行的,而是對新生成的一個源操作串對象的拷貝進行的,其操作的結果不影響源串。
相反,StringBuffer中對字符串的連接操作是對源串本身進行的,操作之后源串的值發生了變化,變成連接后的串。
★ 其它操作
1.字符串的比較
String中提供的方法:
equals( )和equalsIgnoreCase( )
它們與運算符'= ='實現的比較是不同的。運算符'= ='比較兩個對象是否引用同一個實例,而equals( )和equalsIgnoreCase( )則比較 兩個字符串中對應的每個字符值是否相同。
2.字符串的轉化
java.lang.Object中提供了方法toString( )把對象轉化為字符串。
3.字符串"+"操作
運算符'+'可用來實現字符串的連接:
String s = "He is "+age+" years old.";
其他類型的數據與字符串進行"+"運算時,將自動轉換成字符串。具體過程如下:
String s=new StringBuffer("he is").append(age).append("years old").toString();
注意:除了對運算符"+"進行了重載外,java不支持其它運算符的重載。