走过路过过来看看 1.打开文件,读出一行内容2.输入一个字符串 sadfsa 逆序输出 asfdas不要使用 reverse函数.3.计算字符串中某一子串出现的 次数 不要使用 count函数4.写个堆栈的进栈和出栈5.深度遍历树的算法 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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]; } 我没有测试过, 没环境, 将字符串转换成字符数组, 然后将该数组折半, 交换对应的字符, 最后再拼成字符串就行了..那个交换利用了异或操作, 主要是char可以向int转型...所以可以使用. 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.写个堆栈的进栈和出栈 考虑效率问题, 可以根据实际情况把基础数据结构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()); } }} 楼主,作业题,还是自己独立思考,做出来的好。或者,楼主可以把自己的想法,代码贴出来,大家互相讨论一下也可以啊。1.打开文件,读出一行内容BufferedReader br = new BufferedReader(new FileReader("d:\\test.txt"));String line = br.readLine();System.out.println(line); 请教奇怪的实例化对像的问题!!! 一个char类型的变量能否存放一个繁体中文的汉字 新手代码问题求助 java堆栈问题,望大家给我详细讲解下原理 来个稍微高点的高手--牛B最好(服务端和多个客户端问题) 请问哪里可以找到Sun公司/jdk1.6.0/demo/jfc/Java2D/Java2Demo.html这个的源代码? 正则表达式的问题 jdbc自动提交的问题 怎么将一个日期得变量用 yyyy-mm-dd hh:mm:ss来格式化一下?急 请教:关于多维数组初始化的小问题 内存分配空间大小 帮忙解决一个算法(多少分都可以)
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];
}
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);
}
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());
}
}
}
或者,楼主可以把自己的想法,代码贴出来,大家互相讨论一下也可以啊。1.打开文件,读出一行内容
BufferedReader br = new BufferedReader(new FileReader("d:\\test.txt"));
String line = br.readLine();
System.out.println(line);