用于String和Integer参数的Java compareTo [英] Java compareTo for String and Integer arguments

查看:316
本文介绍了用于String和Integer参数的Java compareTo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施冒泡排序算法,我希望它能够同时接受 Integer String 参数。我将所有输入转换为字符串,并使用 compareTo 方法将作为字符串的整数与字符串进行比较。使用 compareTo 来比较计算的整数时,我得到的答案不正确。我做错了什么?

I am implementing the bubble sort algorithm and I want it to be able to accept both Integer and String parameters. I cast all input as Strings and use the compareTo method to compare the integers casted as strings to the strings. I am getting an incorrect answer when using compareTo to compare the casted integers. What am I doing wrong?

推荐答案

Integer.compareTo以数字方式对数字进行排序。这就是你想要的。

Integer.compareTo sorts numbers numerically. This is what you want.

String.compareTo按字典顺序排序字符串;也就是说,按字母顺序排列。

String.compareTo sorts strings lexicographically; that is, in alphabetical order.

我记得在Windows 3.1中我的数码相机中的照片文件夹是这样订购的:PHOTO1,PHOTO10,PHOTO100,PHOTO2,PHOTO20 ,PHOTO3,......等等。 Windows XP对它们的排序更像您期望的:PHOTO1,PHOTO2,PHOTO3,...等。这是因为它对表示数字的字符串有特殊的排序规则。

I remember in Windows 3.1 that the folder of photos from my digital camera was ordered like this: PHOTO1, PHOTO10, PHOTO100, PHOTO2, PHOTO20, PHOTO3, ... and so on. Windows XP sorts them more like you would expect: PHOTO1, PHOTO2, PHOTO3, ... etc. This is because it has special sorting rules for strings that represent numbers.

在字典顺序中,一个字符串A中的每个字符与另一个字符串B中的相应字符进行比较。对于两个字符串中的每个对应字符:

In lexicographical ordering, each character in one string A is compared to the corresponding character in another string B. For each corresponding character in the two strings:


  • 如果A的当前字符按字典顺序小于(在字母表之前)B的字符,那么A出现在B之前。

  • 如果B的字符小于A的字符,则B出现之前A。

  • 如果两个字符相同,那么我们还不知道。检查下一个。

  • 如果其中一个字符串中没有剩余字符,那么较短字符就会出现在较长字符串之前。

  • 如果两个字符串中没有剩下的字符,则它们是相同的字符串。

  • If A's current character is lexicographically less than (comes before in the alphabet) B's character, then A comes before B.
  • If B's character is less than A's character, then B comes before A.
  • If the two characters are the same, then we don't know yet. The next one is checked.
  • If there are no more characters left in one of the strings, then the shorter one comes before the longer one.
  • If there are no more character left in both strings, then they are the same string.

这里的第四点是你为什么得到错误答案,假设Eddie对您的问题的分析是正确的。

The fourth point here is why you are getting incorrect answers, assuming Eddie's analysis of your problem is correct.

考虑字符串10和2。字典顺序将分别查看每个字符的第一个字符1和2。字符'1'在Java使用的字符集中位于'2'之前,因此它在2之前排序10,就像bare在hare之前排序一样,因为'b'在'之前' h'。

Consider the strings "10" and "2". Lexicographical ordering would look at the first characters of each, '1' and '2' respectively. The character '1' comes before '2' in the character set that Java uses, so it sorts "10" before "2", in the same way that "bare" is sorted before "hare" because 'b' comes before 'h'.

我建议你在排序前将字符串转换为整数。使用Integer.parseString执行此操作。

I suggest you cast your strings to integers before sorting. Use Integer.parseString to do this.

这篇关于用于String和Integer参数的Java compareTo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆