A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 芝麻糊 中级黑马   /  2015-11-9 23:19  /  414 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

abc_de_fgh_i
这类字符串, 变为 i_fgh_de_abc

不使用split, 不使用StringBuffer、StringBuilder , 不从头到尾遍历字符串

2 个回复

倒序浏览
这是一道小米面试题。 楼主当时被虐的体无完肤, 现在也没想到怎么做, 求解答
回复 使用道具 举报
本帖最后由 黑夜中那颗星 于 2015-11-10 10:58 编辑

不知道这样符不符合要求
  1. import java.util.*;
  2. public class Test {
  3.         public static void main(String[] args) {
  4.                 String str = "abc_de_fgh_i";
  5.                 ArrayList<String> al = new ArrayList<String>();
  6.                 System.out.println(str);
  7.                 while(true){
  8.                         int index = str.indexOf('_');
  9.                         if(index<0){
  10.                                 al.add(str);
  11.                                 break;
  12.                         }
  13.                         String temp = str.substring(0,index);
  14.                         str = str.substring(index +1);
  15.                         al.add(temp);
  16.                         
  17.                 }
  18.                 for(int x = al.size()-1;x>=0;x--){
  19.                         if(x>0)
  20.                                 System.out.print(al.get(x)+"_");
  21.                         else
  22.                                 System.out.print(al.get(x));
  23.                 }
  24.         }

  25. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马