zoukankan      html  css  js  c++  java
  • 【IT笔试面试题整理】字符串转数组+数组转字符串

    【试题描述】定义一个函数,字符串转数组数组转字符串

    【参考代码】

     1     public static int strToInt(String str)
     2     {
     3         int i = 0, num = 0;
     4         char[] strTemp = str.toCharArray();
     5         boolean isNeg = false;
     6         int len = str.length();
     7 
     8         if (strTemp[0] == '-')
     9         {
    10             isNeg = true;
    11             i = 1;
    12         }
    13         while (i < len)
    14         {
    15             num *= 10;
    16             num += (strTemp[i++] - '0');
    17         }
    18         if (isNeg)
    19             num *= -1;
    20         return num;
    21     }
     1     public static String intTOStr(int num)
     2     {
     3         char[] tempChar = new char[11];
     4         int i = 0;
     5         boolean isNeg = false;
     6         if (num < 0)
     7         {
     8             num *= -1;
     9             isNeg = true;
    10         }
    11         do{
    12             tempChar[i++] = (char) (num % 10 + '0');
    13             num /=10;
    14         }while (num != 0);
    15         
    16         if(isNeg)
    17             tempChar[i] = '-';
    18         System.out.println(tempChar);
    19         StringBuilder b = new StringBuilder();
    20         while(i>=0)
    21         {
    22             b.append(tempChar[i--]);
    23         }
    24     
    25         return b.toString();
    26     }
  • 相关阅读:
    8.25 欢乐emmm赛
    树专练
    字符串知识点大集合
    8.12 小组解题
    暑假大联欢 happynk 2019.8.11
    游记-多省联考 2019
    图论-匈牙利算法模板
    数论-哈哈哈好快乐
    数论-线性基
    其他-私人♂收藏(比赛记录 Mar, 2019)
  • 原文地址:https://www.cnblogs.com/WayneZeng/p/9290759.html
Copyright © 2011-2022 走看看