在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b 1、在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b。2、写一个方法去掉字符串前后空格。(主要考察代码思路,对字符串为空或NULL是否进行处理。) 都用java实现的代码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 >>主要考察代码思路,不明白怎么考察jdk源码中关于String去掉前后空格的代码(trim()方法)是这样写的. public String trim() { int len = count; int st = 0; int off = offset; /* avoid getfield opcode */ char[] val = value; /* avoid getfield opcode */ while ((st < len) && (val[off + st] <= ' ')) { st++; } while ((st < len) && (val[off + len - 1] <= ' ')) { len--; } return ((st > 0) || (len < count)) ? substring(st, len) : this; }但估计你要是这么写,你领导能骂死你. 难道你比jdk源码里写的还好? public final class test { public static void main(String[] args) { System.out.println(single()); } public static String single(){ StringBuffer sb = new StringBuffer(); Scanner input = new Scanner(System.in); System.out.println("输入一串字母"); if (input.hasNext()) {//这里虽然判断了是否输入即是否为null。但是scanner控制台输入我还真不知道如何触发他为false的情况 String sth = input.next().trim();//这里获取控制台输入的字符串的时候进行了去空格处理。 for (int i = 0; i < sth.length(); i++) { int count = 0; for (int j = 0; j < sth.length(); j++) { if (sth.charAt(i) == sth.charAt(j)) { count++; } } if (count == 1) { sb.append(sth.charAt(i)); } } return sb.toString(); } return "丫什么都没输入我给你判断什么?!"; }}控制台输入及输出结果如下:输入一串字母abcdeefabcdf 第一题public class Test { /** * @Function: * @Since Oct 12, 2011 * @param args */ public static void main(String[] args) { String str = "abcbcefg"; for(int i = 0 ; i < str.length();i++){ if(str.indexOf(str.charAt(i)) == str.lastIndexOf(str.charAt(i))){ System.out.println(str.charAt(i)); break; } } }} 第二题不多说,看下trim()的源码就知道 public String trim() { int len = count; int st = 0; int off = offset; /* avoid getfield opcode */ char[] val = value; /* avoid getfield opcode */ while ((st < len) && (val[off + st] <= ' ')) { st++; } while ((st < len) && (val[off + len - 1] <= ' ')) { len--; } return ((st > 0) || (len < count)) ? substring(st, len) : this; } 蛋疼 java做聊天服务器效率问题 JTable拖拽表头 JAVA字符串比较问题 jsp如何实现像DataGrid 那样的显示,有阅览和编辑功能,数据库是sql 2000 --------------------急用 请问如何在JCreator中连接SQL 2000数据库????????? 各位帮帮手!我的applet不能在ie中显示(解决给分!) 请问如何在一个控件里添加他的属性,并显示出来 超菜问题:) !!!!!!!!!!求教各位高手!此段关于控件的代码为什么不能编译? 快点进来看看吧~~~回答得好我就给你分 新手求助:servlet运行不了,下面是http错误代码。。
不明白怎么考察
jdk源码中关于String去掉前后空格的代码(trim()方法)是这样写的. public String trim() {
int len = count;
int st = 0;
int off = offset; /* avoid getfield opcode */
char[] val = value; /* avoid getfield opcode */ while ((st < len) && (val[off + st] <= ' ')) {
st++;
}
while ((st < len) && (val[off + len - 1] <= ' ')) {
len--;
}
return ((st > 0) || (len < count)) ? substring(st, len) : this;
}但估计你要是这么写,你领导能骂死你. 难道你比jdk源码里写的还好?
public static void main(String[] args) {
System.out.println(single());
}
public static String single(){
StringBuffer sb = new StringBuffer();
Scanner input = new Scanner(System.in);
System.out.println("输入一串字母");
if (input.hasNext()) {//这里虽然判断了是否输入即是否为null。但是scanner控制台输入我还真不知道如何触发他为false的情况
String sth = input.next().trim();//这里获取控制台输入的字符串的时候进行了去空格处理。
for (int i = 0; i < sth.length(); i++) {
int count = 0;
for (int j = 0; j < sth.length(); j++) {
if (sth.charAt(i) == sth.charAt(j)) {
count++;
}
}
if (count == 1) {
sb.append(sth.charAt(i));
}
}
return sb.toString();
}
return "丫什么都没输入我给你判断什么?!";
}
}
控制台输入及输出结果如下:
输入一串字母
abcdeefab
cdf
第一题public class Test { /**
* @Function:
* @Since Oct 12, 2011
* @param args
*/
public static void main(String[] args) {
String str = "abcbcefg";
for(int i = 0 ; i < str.length();i++){
if(str.indexOf(str.charAt(i)) == str.lastIndexOf(str.charAt(i))){
System.out.println(str.charAt(i));
break;
}
} }}
public String trim() {
int len = count;
int st = 0;
int off = offset; /* avoid getfield opcode */
char[] val = value; /* avoid getfield opcode */ while ((st < len) && (val[off + st] <= ' ')) {
st++;
}
while ((st < len) && (val[off + len - 1] <= ' ')) {
len--;
}
return ((st > 0) || (len < count)) ? substring(st, len) : this;
}