1.打开文件,读出一行内容
2.输入一个字符串   sadfsa 逆序输出  asfdas不要使用 reverse函数.
3.计算字符串中某一子串出现的 次数  不要使用 count函数
4.写个堆栈的进栈和出栈
5.深度遍历树的算法

解决方案 »

  1.   

    2. 输入一个字符串  sadfsa 逆序输出  asfdas不要使用 reverse函数.
      char[] array = "sadfsa".toCharArray();
      int length = array.length();
      int halfLength = length / 2
      for(int i = 0; i < halfLength; i ++) {
        int preIndex = i;
        int sufIndex = length - 1 - i;
        array[preIndex] = array[preIndex] ^ array[sufIndex];
        array[sufIndex] = array[preIndex] ^ array[sufIndex];
        array[preIndex] = array[preIndex] ^ array[sufIndex];
      }
      

  2.   

    我没有测试过, 没环境, 将字符串转换成字符数组, 然后将该数组折半, 交换对应的字符, 最后再拼成字符串就行了..那个交换利用了异或操作,  主要是char可以向int转型...所以可以使用.
      

  3.   

    5.深度遍历树的算法 
    Node root = null;
    //initial the tree.public static void recursion(Node parent) {
      Node[] children = parent.children();
      for(Node child: children) {
        recursion(child);
      }
      System.out.println("This is node: " + parent.msg);
    }
      

  4.   

    4.写个堆栈的进栈和出栈 考虑效率问题, 可以根据实际情况把基础数据结构datas的长度保留或做其他处理public class Stack {
      private List<Object> datas;
      public Stack() {
        this.datas = new ArrayList<Object>();
      }
      public void push(Object data) {
        this.datas.add(data);
      }
      public void pop() {
        try {
          return this.datas[this.datas.size()];
        } finally {
          this.datas.remove(this.datas.size());
        }
      }
    }
      

  5.   

    楼主,作业题,还是自己独立思考,做出来的好。
    或者,楼主可以把自己的想法,代码贴出来,大家互相讨论一下也可以啊。1.打开文件,读出一行内容
    BufferedReader br = new BufferedReader(new FileReader("d:\\test.txt"));
    String line = br.readLine();
    System.out.println(line);