代码之家  ›  专栏  ›  技术社区  ›  kervin

Java ME实用程序函数

  •  2
  • kervin  · 技术社区  · 7 年前

    JavaME在特性上非常稀少。请列出您最喜欢的实用程序函数,使使用它更像使用正确的Java,每个答案一个。试着让你的答案针对javame。

    3 回复  |  直到 16 年前
        1
  •  1
  •   Thomi    12 年前

    小型日志框架

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

        2
  •  0
  •   Casebash    16 年前

    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;
    }
    
        3
  •  0
  •   kervin    9 年前

    从读者那里读一行。另见 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();
     }
    }