下面就列舉個API 中string類中的10個方法的小應用:
1. contains:當且僅當此字符串包含指定的char值序列時,返回true









2.copyValueOf:通過這個方法把給定數組中的相應的部分copy到字符串中輸出來









3.Intern:返回字符串對象的規范化表示形式,遵循以下規則:對于任意兩個字符串s,t,當且僅當s.equals(t) 為true時,s.intern() == t.intern 才為true














































如果把其實索引改為負數或結束索引大于字符串長度,便會出現indexoutBounsException的異常
8.toCharArray:將此字符串轉化為一個新的字符數組































我知道這只是一個開始,但我會努力,我相信我會愛上java

具體程序為:
package com.dr.stock;
public class Stock {
private String name;
private int price;
private int id;
private int flag;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the price
*/
public int getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(int price) {
this.price = price;
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the flag
*/
public int getFlag() {
return flag;
}
/**
* @param flag the flag to set
*/
public void setFlag(int flag) {
this.flag = flag;
}
}
這個類列出了股票的幾個屬性,下面是股票server和client端的類代碼,在client端調用服務器端的方法,從而得到一些信息。
package com.dr.stock;
import java.util.Random;
public class StockServer {
public static Stock[] getStock(){
Stock[] stock = new Stock[3];
Random rd = new Random();
//##############################
Stock sk1 = new Stock();
sk1.setName("ibm");
sk1.setPrice(rd.nextInt(100));
sk1.setId(1);
sk1.setFlag(1);
stock[0] = sk1;
//###############################
Stock sk2 = new Stock();
sk2.setName("sun");
sk2.setPrice(rd.nextInt(200));
sk2.setId(2);
sk2.setFlag(2);
stock[1] = sk2;
//##################################
Stock sk3 = new Stock();
sk3.setName("sico");
sk3.setPrice(rd.nextInt(300));
sk3.setId(3);
sk3.setFlag(3);
stock[2] = sk3;
return stock;
}
}
package com.dr.stock;
import java.util.Random;
public class StockClient {
public static void main(String[] args) {
while(true){
Stock[] sk = StockServer.getStock();
for(int i = 0;i<sk.length;i++){
if(sk[i] != null)
System.out.println("股票名字:"+sk[i].getName()+" 股票價格:"+sk[i].getPrice()+" 股票ID:"+sk[i].getId()+" 股票類型"+sk[i].getFlag());
}
try{
Thread.sleep(2000);
}catch(InterruptedException e){
e.printStackTrace();
break;
}
}
}
}
| |||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
---|---|---|---|---|---|---|---|---|---|
25 | 26 | 27 | 28 | 29 | 30 | 31 | |||
1 | 2 | 3 | 4 | 5 | 6 | 7 | |||
8 | 9 | 10 | 11 | 12 | 13 | 14 | |||
15 | 16 | 17 | 18 | 19 | 20 | 21 | |||
22 | 23 | 24 | 25 | 26 | 27 | 28 | |||
29 | 30 | 1 | 2 | 3 | 4 | 5 |
常用鏈接
留言簿(13)
隨筆檔案
文章檔案
搜索
最新評論

- 1.?re: java中構造方法和方法全面解析
- 為什么非要調用父類的構造方法
- --zjy
- 2.?re: java-醫院病人排隊掛號醫生叫號簡潔小系統
- 代碼不全 QueueServer 沒有
- --sss
- 3.?re: Java中thread類與Runnable接口的區別
- 受教了
- --李大明
- 4.?re: java中消費者與生產者的問題實例解析
- 樓主有沒有搞錯啊 你的最后那個截圖后面不還是亂的嗎 不是一個一個交替輸出的啊
- --aben
- 5.?re: java堆棧存取實例小講解
-
@閆佳
上面有插入代碼--呵呵--- - --迷人笑笑