陳鍾誠的程式作品集 -- 常用函數 (Java)

程式作品

C 語言

Java

C#

JavaScript

常用函數

文字處理

遊戲程式

衛星定位

系統程式

資料結構

網路程式

自然語言

人工智慧

機率統計

資訊安全

等待完成

訊息

相關網站

參考文獻

最新修改

簡體版

English

package ccc;
import java.util.*;
import java.io.*;
import java.util.regex.*;
 
public class UTIL {
  public static void main(String[] args) throws Exception {
      String mapText = "a=x\nb=y\nc=z";
      TreeMap map = text2map(mapText);
      System.out.println(map2text(map));
  }
 
  public static String array2text(Object[] pArray, String pSpliter) { 
    StringBuffer rzStr = new StringBuffer();
    for (int i=0; i<pArray.length; i++)
        rzStr.append(pArray[i].toString()+pSpliter);
    if (rzStr.length() == 0) return "";
    return rzStr.substring(0, rzStr.length()-pSpliter.length());
  }
 
  public static String map2text(Map map) {
    return array2text(map.entrySet().toArray(), "\n");
  }
 
  public static TreeMap text2map(String pText) {
    TreeMap map = new TreeMap();
    String[] lines = pText.split("\n");
    for (int i=0; i<lines.length; i++) {
        String[] tokens=lines[i].split("=");
        if (tokens.length >= 2)
          map.put(tokens[0].trim(), tokens[1].trim());
    }
    return map;
  }
}
 
/*  public static String map2text(Map map) {
    StringBuffer rzStr = new StringBuffer();
    Object[] keys = map.keySet().toArray();
    Object[] values = map.values().toArray();
    for (int i=0; i<keys.length; i++) 
        rzStr.append(keys[i]+"="+values[i]+"\r\n");
    return rzStr.toString();
  }
*/

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License