当前位置: 代码迷 >> Java相关 >> 不明白,求解
  详细解决方案

不明白,求解

热度:470   发布时间:2012-06-04 10:23:19.0
不明白,求解
程序代码:
public class Test {
public static void main(String[] args) {
/*String[] redButton = {"/", "*", "-", "+", "="};
java.util.Arrays.sort(redButton);
for(int i=0; i<redButton.length; i++) {
System.out.println(redButton[i]);
}
*/
String str = "abc";
System.out.println(str.indexOf("a"));
System.out.println(str.indexOf("b"));
System.out.println(str.indexOf("c"));
System.out.println(str.indexOf("ab"));
System.out.println(str.indexOf("ac"));
System.out.println(str.indexOf("bc"));
System.out.println(str.indexOf("abc"));
System.out.println(str.indexOf(" "));
}
}
搜索更多相关的解决方案: abc  

----------------解决方案--------------------------------------------------------
public class Test {

public static void main(String[] args) {

/*String[] redButton = {"/", "*", "-", "+", "="};

java.util.Arrays.sort(redButton);

for(int i=0; i<redButton.length; i++) {

System.out.println(redButton[i]);

}*/

String str = "abc";

System.out.println(str.indexOf("a"));

System.out.println(str.indexOf("b"));

System.out.println(str.indexOf("c"));

System.out.println(str.indexOf("ab"));

System.out.println(str.indexOf("ac"));

System.out.println(str.indexOf("bc"));

System.out.println(str.indexOf("abc"));

System.out.println(str.indexOf(" "));

}

}
复制代码


这是我在API文档查到方法的说明:
/**public static void sort(Object[] a)根据元素的自然顺序对指定对象数组按升序进行排序。
* 数组中的所有元素都必须实现 Comparable 接口。
* 此外,数组中的所有元素都必须是可相互比较的(也就是说,对于数组中的任何 e1 和 e2 元素而言,e1.compareTo(e2) 不得抛出 ClassCastException)。
*保证此排序是稳定的:不会因调用 sort 方法而对相等的元素进行重新排序。
*该排序算法是一个经过修改的合并排序算法(其中,如果低子列表中的最高元素小于高子列表中的最低元素,则忽略合并)。此算法提供可保证的 n*log(n) 性能。

*参数:
*a - 要排序的数组
*抛出:
*ClassCastException - 如果数组包含不可相互比较的 的元素(例如,字符串和整数)。
**/
/**public int indexOf(String str)返回指定子字符串在此字符串中第一次出现处的索引。返回的整数是
*this.startsWith(str, k)
*为 true 的最小 k 值。
*参数:
*str - 任意字符串。
*返回:
*如果字符串参数作为一个子字符串在此对象中出现,则返回第一个这种子字符串的第一个字符的索引;如果它不作为一个子字符串出现,则返回 -1。
**/

所以我有疑问:
/**
* 提问1:{"/", "*", "-", "+", "="},这里面的东西也可以排序?
* 提问2:" ", "a", "b", "c", "ab", "ac", "bc", "abc",这些字符串中,哪些是"abc"的子字符串?
**/

----------------解决方案--------------------------------------------------------
第一个问题:
提问1:{"/", "*", "-", "+", "="},这里面的东西也可以排序?
这里
{"/", "*", "-", "+", "="}
可以排序,且运行结果为:


第二个问题:
* 提问2:" ", "a", "b", "c", "ab", "ac", "bc", "abc",这些字符串中,哪些是"abc"的子字符串?

这些字符串中 " ", "ac", 不是其子字符串,
剩下的都是。
其运行结果为:

因为:如果字符串参数作为一个子字符串在此对象中出现,则返回第一个这种子字符串的第一个字符的索引;如果它不作为一个子字符串出现,则返回 -1
与预期结果一致。
----------------解决方案--------------------------------------------------------
追问:为什么" ", "ac", 不是其子字符串??
----------------解决方案--------------------------------------------------------
首先因为"abc"中没有空格,所以" "不是,再者因为"abc"中a c之间有b.a能与a对应c不能与b对应,所以不是。自己多多体会吧
----------------解决方案--------------------------------------------------------
恩,你这样一说,我似乎理解了,谢谢
----------------解决方案--------------------------------------------------------
  相关解决方案