关于2个函数的提问

fengzhizi715 2004-11-14 12:29:06
1.String类的compareTO方法是比较String对象的,如果调用compareTo的字符串小于作为参数的字符串,方法compareTo返回一个负值。
如果调用compareTo的字符串大于作为参数的字符串,方法compareTo返回一个正值。
请问返回的数值是如何求出的?

2.System.gc()是显示调用垃圾收集程序
System.gc()调用不能保证垃圾收集的执行,且垃圾收集也不能保证按特定的顺序回收对象。如果不能保证垃圾收集的执行会出现什么结果?
...全文
99 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jFresH_MaN 2004-11-14
  • 打赏
  • 举报
回复
关于System.gc(),好多资料是说调用jvm的垃圾回收功能
但好象这个方法并不能保证jvm执行垃圾回收,
至于不回收的结果是什么?那就是什么结果也没有,jvm本来该什么时候回收的还是什么时候回收吧
jFresH_MaN 2004-11-14
  • 打赏
  • 举报
回复
我觉得是这样吧
public int compareTo(String anotherString) {

return this.toCharArray().lenth-anotherString.toCharArray().lenth;

}
vgvg 2004-11-14
  • 打赏
  • 举报
回复
1>
public int compareTo(String anotherString) {
int len1 = count;
int len2 = anotherString.count;
int n = Math.min(len1, len2);
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;

if (i == j) {
int k = i;
int lim = n + i;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
} else {
while (n-- != 0) {
char c1 = v1[i++];
char c2 = v2[j++];
if (c1 != c2) {
return c1 - c2;
}
}
}
return len1 - len2;
}
mandm 2004-11-14
  • 打赏
  • 举报
回复
java有自动垃圾回收机制,可以不用自已去管理内存垃圾的。
jFresH_MaN 2004-11-14
  • 打赏
  • 举报
回复
呵呵,对对对,我刚刚晕了
不好意思
classjava 2004-11-14
  • 打赏
  • 举报
回复
this.charAt(k)-anotherString.charAt(k)

If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:


this.length()-anotherString.length()
classjava 2004-11-14
  • 打赏
  • 举报
回复
public class Test
{
public static void main(String[] args)
{
String s1="ab";
System.out.println(s1.compareTo("ab"));//相等0
System.out.println(s1.compareTo("a"));//大于就1,aa也大于
System.out.println(s1.compareTo("b"));//-1按照字典排列来比较的?
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧