回覆列表
  • 1 # 阿七牛雜

      JAVA 中int型別轉String型別的通常方法,有三種:  

    1、String.valueOf(int i)  

    2、Integer.toString(int i)  

    3、i+""; //i 為 int型別  三種方法效率排序為:    Integer.toString(int i) > String.valueOf(int i) > i+"";    程式碼測試:  public static void main(String[] args) {  int[] intArr = new int[1000000];  String[] strArr1 = new String[1000000];//為了公平分別定義三個陣列  String[] strArr2 = new String[1000000];  String[] strArr3 = new String[1000000];  //賦值  Long t1 = System.currentTimeMillis();  for(int i=0;i<1000000;i++){  intArr[i]=i+1;  }  Long t2 = System.currentTimeMillis();  for(int i=0;i<1000000;i++){  strArr1[i] = String.valueOf(intArr[i]);  }  Long t3 = System.currentTimeMillis();  for(int i=0;i<1000000;i++){  strArr2[i] = Integer.toString(intArr[i]);  }  Long t4 = System.currentTimeMillis();  for(int i=0;i<1000000;i++){  strArr3[i] = intArr[i]+"";  }  Long t5 = System.currentTimeMillis();  System.out.println("t1 = "+t1);  System.out.println("t2 = "+t2);  System.out.println("t3 = "+t3);  System.out.println("t4 = "+t4);  System.out.println("t5 = "+t5);  System.out.println("賦值:"+(t2-t1));  System.out.println("String.valueOf(i):"+(t3-t2));  System.out.println("Integer.toString(i):"+(t4-t3));  System.out.println("i+\"\":"+(t5-t4));  }  我的測試結果為:  t1 = 1298798872078  t2 = 1298798872093  t3 = 1298798872656  t4 = 1298798873000  t5 = 1298798873671  賦值:15  String.valueOf(i):563  Integer.toString(i):344  i+"":671

  • 中秋節和大豐收的關聯?
  • 黑色翅膀,藍色身軀的蜻蜓是什麼品種?