Java合併排序法示例
public class Coalition{ public static void main(String[] args){ /* 定義一個數組和二個整型變量 */ int[] num = {65,87,15,23,46,48,52,79,97,53}; int i,j; /* 合併排序法 */ for (i = 0; i < 9; i++) for (j = i+1; j < 10; j++){ if (num[i] > num[j]){ num[i] = num[i] + num[j]; num[j] = num[i] - num[j]; num[i] = num[i] - num[j]; } } /* 循環輸出排序後的結果 */ for (i = 0; i < 10; i++){ System.out.println(num[i]); } } }
Java冒泡排序法示例
public class BubbleUp{ public static void main(String[] args){ /* 定義一個數組和三個整型變量 */ int[] num = {65,87,15,23,46,48,52,79,97,53}; int i,j,temp; /* 冒泡排序法 */ for (i = 10; i > 0; i--) for (j = i-1; j > 0; j--){ if (num[j] < num[j-1]){ temp = num[j]; num[j] = num[j-1]; num[j-1] = temp; } } /* 循環輸出排序後的結果 */ for (i = 0; i < 10; i++){ System.out.println(num[i]); } } }
Java插入排序法示例
public class Insert{ public static void main(String[] args){ /* 定義一個數組和三個整型變量 */ int[] num = {65,87,15,23,46,48,52,79,97,53}; int i,j,temp; /* 插入排序法 */ for (i = 1; i < 10; i++){ temp = num[i]; for (j = i; j > 0 && temp < num[j - 1]; j--) num[j] = num[j - 1]; num[j] = temp; } /* 循環輸出排序後的結果 */ for (i = 0; i < 10; i++){ System.out.println(num[i]); } } }
JAVA數據類型轉換 將字串 String 轉換成整數 int 有兩個方法:
注: 字串轉成 Double、Float、Long 的方法大同小異1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([String],[int radix]); 2). int i = Integer.valueOf(my_str).intValue();
將整數 int 轉換成字串 String 有叄種方法:
1). String s = String.valueOf(i); 2). String s = Integer.toString(i); 3). String s = "" + i;注: Double、Float、Long 轉成字串的方法大同小異
將布爾 boolean 變量轉換爲字符串 String
boolean bool = true; String s = String.valueOf(bool);這是一個例子,說的是JAVA中數據數型的轉換
package cn.com.lwkj.erts.register; import java.sql.Date; public class TypeChange{ public TypeChange(){ } //change the string type to the int type public static int stringToInt(String intstr){ Integer integer; integer = Integer.valueOf(intstr); return integer.intValue(); } //change int type to the string type public static String intToString(int value){ Integer integer = new Integer(value); return integer.toString(); } //change the string type to the float type public static float stringToFloat(String floatstr){ Float floatee; floatee = Float.valueOf(floatstr); return floatee.floatValue(); } //change the float type to the string type public static String floatToString(float value){ Float floatee = new Float(value); return floatee.toString(); } //change the string type to the sqlDate type public static java.sql.Date stringToDate(String dateStr){ return java.sql.Date.valueOf(dateStr); } //change the sqlDate type to the string type public static String dateToString(java.sql.Date datee){ return datee.toString(); } public static void main(String[] args){ java.sql.Date day ; day = TypeChange.stringToDate("2003-11-3"); String strday = TypeChange.dateToString(day); System.out.println(strday); } }JAVA中常用數據類型轉換函數 雖然都能在JAVA API中找到,整理一下做個備份。
string->byte Byte static byte parseByte(String s) byte->string Byte static String toString(byte b) char->string Character static String to String (char c) string->Short Short static Short parseShort(String s) Short->String Short static String toString(Short s) String->Integer Integer static int parseInt(String s) Integer->String Integer static String tostring(int i) String->Long Long static long parseLong(String s) Long->String Long static String toString(Long i) String->Float Float static float parseFloat(String s) Float->String Float static String toString(float f) String->Double Double static double parseDouble(String s) Double->String Double static String toString(Double)數據類型
基本類型有以下四種:
int | 長度數據類型有:byte(8bits/1字節)、short(16bits/2字節)、int(32bits/4字節)、long(64bits/8字節) |
float | 長度數據類型有:單精度(32bits/4字節 float)、雙精度(64bits/8字節 double) |
boolean | 類型變量的取值有:ture(1bits)、false(1bits) |
char | 數據類型有:unicode字符(16bits/2字節) |
轉換原則
從低精度向高精度轉換
byte、short、int、long、float、double、char
注:兩個char型運算時,自動轉換爲int型;當char與別的類型運算時,也會先自動轉換爲int型的,再做其它類型的自動轉換
基本類型向類類型轉換
正向轉換:通過類包裝器來new出一個新的類類型的變量
Integer a = new Integer(2);反向轉換:通過類包裝器來轉換
int b = a.intValue();類類型向字符串轉換
正向轉換:因爲每個類都是object類的子類,而所有的object類都有一個toString()函數,所以通過toString()函數來轉換即可
反向轉換:通過類包裝器new出一個新的類類型的變量
eg1: int i = Integer.valueOf("123").intValue();
說明:上例是將一個字符串轉化成一個Integer對象,然後再調用這個對象的intValue()方法返回其對應的int數值。
eg2: float f = Float.valueOf("123").floatValue();
說明:上例是將一個字符串轉化成一個Float對象,然後再調用這個對象的floatValue()方法返回其對應的float數值。
eg3: boolean b = Boolean.valueOf("123").booleanValue();
說明:上例是將一個字符串轉化成一個Boolean對象,然後再調用這個對象的booleanValue()方法返回其對應的boolean數值。
eg4: double d = Double.valueOf("123").doubleValue();
說明:上例是將一個字符串轉化成一個Double對象,然後再調用這個對象的doubleValue()方法返回其對應的double數值。
eg5: long l = Long.valueOf("123").longValue();
說明:上例是將一個字符串轉化成一個Long對象,然後再調用這個對象的longValue()方法返回其對應的long數值。
eg6: char = Character.valueOf("123").charValue();
說明:上例是將一個字符串轉化成一個Character對象,然後再調用這個對象的charValue()方法返回其對應的char數值。
基本類型向字符串的轉換
正向轉換:如
int a = 12;反向轉換:通過類包裝器
String b;
b = a + "";
eg1: int i = Integer.parseInt("123")
說明:此方法只能適用於字符串轉化成整型變量
eg2: float f = Float.valueOf("123").floatValue();
說明:上例是將一個字符串轉化成一個Float對象,然後再調用這個對象的floatValue()方法返回其對應的float數值。
eg3: boolean b = Boolean.valueOf("123").booleanValue();
說明:上例是將一個字符串轉化成一個Boolean對象,然後再調用這個對象的booleanValue()方法返回其對應的boolean數值。
eg4: double d = Double.valueOf("123").doubleValue();
說明:上例是將一個字符串轉化成一個Double對象,然後再調用這個對象的doubleValue()方法返回其對應的double數值。
eg5: long l = Long.valueOf("123").longValue();
說明:上例是將一個字符串轉化成一個Long對象,然後再調用這個對象的longValue()方法返回其對應的long數值。
eg6: char = Character.valueOf("123").charValue();
說明:上例是將一個字符串轉化成一個Character對象,然後再調用這個對象的charValue()方法返回其對應的char數值。
ArrayList用法System.Collections.ArrayList類是一個特殊的數組。通過添加和刪除元素,就可以動態改變數組的長度。
一.優點
1、支持自動改變大小的功能
2、可以靈活的插入元素
3、可以靈活的刪除元素
二.侷限性
跟一般的數組比起來,速度上差些
三.添加元素
1、public virtual int Add(objectvalue);
將對象添加到ArrayList的結尾處
ArrayList aList = new ArrayList(); aList.add("a"); aList.add("b"); aList.add("c"); aList.add("d"); aList.add("e");結果爲abcde
2、public virtual void Insert(intindex,objectvalue);
將元素插入ArrayList的指定索引處
ArrayList aList = new ArrayList(); aList.add("a"); aList.add("b"); aList.add("c"); aList.add("d"); aList.add("e"); aList.Insert(0,"aa");結果爲aaabcde
3、public virtual void InsertRange(intindex,ICollectionc);
將集合中的某個元素插入ArrayList的指定索引處
ArrayList aList = new ArrayList(); aList.add("a"); aList.add("b"); aList.add("c"); aList.add("d"); aList.add("e"); ArrayList aList2 = new ArrayList(); aList2.add("tt"); aList2.add("ttt"); aList.InsertRange(2,aList2);結果爲abtttttcde
四.刪除
1、public virtual void Remove(objectobj);
從ArrayList中移除特定對象的第一個匹配項,注意是第一個
ArrayList aList = new ArrayList(); aList.add("a"); aList.add("b"); aList.add("c"); aList.add("d"); aList.add("e"); aList.Remove("a");結果爲bcde
2、public virtual void RemoveAt(intindex);
移除ArrayList的指定索引處的元素
aList.add("a"); aList.add("b"); aList.add("c"); aList.add("d"); aList.add("e"); aList.RemoveAt(0);結果爲bcde
3、public virtual void RemoveRange(intindex,intcount);
從ArrayList中移除一定範圍的元素。Index表示索引,count表示從索引處開始的數目
aList.add("a"); aList.add("b"); aList.add("c"); aList.add("d"); aList.add("e"); aList.RemoveRange(1,3);結果爲ae
4、public virtual void Clear();
從ArrayList中移除所有元素。
五.排序
1、public virtual void Sort();
對ArrayList或它的一部分中的元素進行排序。
ArrayList aList = new ArrayList(); aList.add("e"); aList.add("a"); aList.add("b"); aList.add("c"); aList.add("d"); DropDownList1.DataSource = aList; //DropDownList DropDownList1; DropDownList1.DataBind();結果爲eabcd
ArrayList aList = new ArrayList(); aList.add("a"); aList.add("b"); aList.add("c"); aList.add("d"); aList.add("e"); aList.Sort(); //排序 DropDownList1.DataSource = aList; //DropDownList DropDownList1; DropDownList1.DataBind();結果爲abcde
2、public virtual void Reverse();
將ArrayList或它的一部分中元素的順序反轉。
ArrayList aList = new ArrayList(); aList.add("a"); aList.add("b"); aList.add("c"); aList.add("d"); aList.add("e"); aList.Reverse(); //反轉 DropDownList1.DataSource = aList; //DropDownList DropDownList1; DropDownList1.DataBind();結果爲edcba
六.查找
a) public virtual int IndexOf(object);
b) public virtual int IndexOf(object,int);
c) public virtual int IndexOf(object,int,int);
返回ArrayList或它的一部分中某個值的第一個匹配項的從零開始的索引。沒找到返回-1。
ArrayList aList = new ArrayList(); aList.add("a"); aList.add("b"); aList.add("c"); aList.add("d"); aList.add("e"); int nIndex = aList.IndexOf("a"); //1 nIndex = aList.IndexOf("p"); //沒找到,-1d) public virtual intLastIndexOf(object);
e) public virtual intLastIndexOf(object,int);
f) public virtual intLastIndexOf(object,int,int);
返回ArrayList或它的一部分中某個值的最後一個匹配項的從零開始的索引。
ArrayList aList = new ArrayList(); aList.add("a"); aList.add("b"); aList.add("a"); //同0 aList.add("d"); aList.add("e"); int nIndex = aList.LastIndexOf("a"); //值爲2而不是0g) public virtual bool Contains(objectitem);
確定某個元素是否在ArrayList中。包含返回true,否則返回false
七.其他
1、public virtual int Capacity{get;set;}
獲取或設置ArrayList可包含的元素數。
2、public virtual int Count{get;}
獲取ArrayList中實際包含的元素數。 Capacity是ArrayList可以存儲的元素數。Count是ArrayList中實際包含的元素數。
Capacity總是大於或等於Count。如果在添加元素時,Count超過Capacity,則該列表的容量會通過自動重新分配內部數組加倍。
如果Capacity的值顯式設置,則內部數組也需要重新分配以容納指定的容量。如果Capacity被顯式設置爲0,則公共語言運行庫將其設置爲默認容量。默認容量爲16。
在調用Clear後,Count爲0,而此時Capacity切是默認容量16,而不是0。
3、public virtual void TrimToSize();
將容量設置爲ArrayList中元素的實際數量。
如果不向列表中添加新元素,則此方法可用於最小化列表的內存系統開銷。
若要完全清除列表中的所有元素,請在調用TrimToSize之前調用Clear方法。截去空ArrayList會將ArrayList的容量設置爲默認容量,而不是零。
ArrayList aList = new ArrayList(); aList.add("a"); aList.add("b"); aList.add("c"); aList.add("d"); aList.add("e"); //Count=5,Capacity=16, aList.TrimToSize(); //Count=Capacity=5;
| ||