1。实现字符串“Dot saw I was Tod”的反转
2。找到两个字符串中所有共有的字符
------解决方案--------------------
- Java code
//问题1 public String reverse(String str) { StringBuffer sb = new StringBuffer(str); sb.reverse(); return sb.toString(); }//问题2 public String check(String str1,String str2) { List<Character> list = new ArrayList<Character>(); char[] cs1 = str1.toCharArray(); char[] cs2 = str2.toCharArray(); for(char c1 : cs1) { for(char c2 : cs2) { if(c1 == c2) { list.add(c1); break; } } } //如果你的结果要求不能重复就用set过滤,不要求就返回list.toString(); Set set = new HashSet(list); return set.toString(); }
------解决方案--------------------
请教 char c1 : cs1 这是什么??? 好像以前没有见过啊!!!???
------解决方案--------------------
请教 char c1 : cs1 这是什么??? 好像以前没有见过啊!!!???
--从头遍历整个数组,1.5开始有的
------解决方案--------------------
FOR EACH循环...1.5开始就有的,性能比较好
------解决方案--------------------
1:
String str = "Dot saw I was Tod" ;
StringBuffer str2 =new StringBuffer(str);
str2.reverse();
System.out.println(str2.toString());
2:
String str = "Dot saw I was Tod" ;
Char[] str2 = str.toCharArray();
int leng = str2.length;
Char str3 = new Char[leng];
for(int i = leng ; i >0 ; i --)
{
str3[leng-i]=str2[i-1];
}
String str4 = new String(str3);
System.out.println(str4);
3:
String str = "Dot saw I was Tod" ;
StringBuffer sb = new StringBuffer();
for(int i = str.length()-1; i>=0;i--)
{
sb.append(str.CharAt(i));
}
System.out.println(sb.toString());
------解决方案--------------------
1楼写的好,给分!!
------解决方案--------------------
package test;
import java.util.ArrayList;
import java.util.List;
public class Test
{
String s1 = "Dot saw I was Tod";
String s2 = "Do you know";
List list = new ArrayList();
public void wenTi_1() //问题1
{
System.out.println(new StringBuffer(s1).reverse());
}
public void wenTi_2() //问题2
{
for (int i = 0; i < s1.length(); i++)
{
for (int j = 0; j < s2.length(); j++)
{
if (s1.charAt(i) == s2.charAt(j) && s1.charAt(i) != ' ')
{
list.add(s1.charAt(i));
}
}
}
int temp = 0;
for (int i = 0; i < list.size(); i++)
{
temp = 0;
for (int j = i + 1; j < list.size(); j++)
{
if (list.get(i).equals(list.get(j)))
{
temp = 1;
}
}
if (temp == 0)
{
System.out.print(list.get(i) + " ");
}
}
}
public static void main(String[] args)
{
Test t = new Test();
t.wenTi_1();
t.wenTi_2();
}
}