2010-07-27 52 views
2

JavaME在功能上相當稀疏。請列出您最喜歡的實用程序函數,使其更像使用適當的Java,每個答案一個。嘗試使您的答案專用於Java ME。Java ME實用功能

+0

參見:這個問題(http://stackoverflow.com/questions/3340954/java-me-utility-functions/3524999# 3524999) – Casebash 2010-08-20 00:03:54

回答

0

將字符串分割

static public String[] split(String str, char c) 
{ 
    int l=str.length(); 
    int count = 0; 
    for(int i = 0;i < l;i++) 
    { 
     if (str.charAt(i) == c) 
     { 
     count ++;  
     } 
    } 
    int first = 0; 
    int last = 0; 
    int segment=0; 
    String[] array = new String[count + 1]; 
    for(int i=0;i<l;i++) 
    { 
     if (str.charAt(i) == c) 
     { 
      last = i; 
      array[segment++] = str.substring(first,last); 
      first = last; 

     } 
     if(i==l-1){ 
      array[segment++] = str.substring(first,l); 
     } 
    } 
    return array; 
} 
0

閱讀從一個讀者的線。另見this question

public class LineReader{ 
private Reader in; 
private int bucket=-1; 
public LineReader(Reader in){ 
    this.in=in; 
} 

    public boolean hasLine() throws IOException{ 
    if(bucket!=-1)return true; 
    bucket=in.read(); 
    return bucket!=-1; 
    } 

//Read a line, removing any /r and /n. Buffers the string 
public String readLine() throws IOException{ 
    int tmp; 
    StringBuffer out=new StringBuffer(); 
    //Read in data 
    while(true){ 
    //Check the bucket first. If empty read from the input stream 
    if(bucket!=-1){ 
    tmp=bucket; 
    bucket=-1; 
    }else{ 
    tmp=in.read(); 
    if(tmp==-1)break; 
    } 
    //If new line, then discard it. If we get a \r, we need to look ahead so can use bucket 
    if(tmp=='\r'){ 
    int nextChar=in.read(); 
    if(tmp!='\n')bucket=nextChar;//Ignores \r\n, but not \r\r 
    break; 
    }else if(tmp=='\n'){ 
    break; 
    }else{ 
    //Otherwise just append the character 
    out.append((char) tmp); 
    } 
    } 
    return out.toString(); 
} 
} 
1

小日誌框架

微測井 http://microlog.sourceforge.net/site/

+1

這將是更好的答案:[這個問題](http://stackoverflow.com/questions/2422555/java-me-libraries)而不是 – Casebash 2010-08-20 00:03:07

+0

噢!我沒有正確閱讀這個問題! – Azlam 2010-08-20 19:03:10