Блог пользователя shubhch32

Автор shubhch32, история, 7 лет назад, По-английски

I needed to sort elements for which I used Arrays.sort() function. I read somewhere that it sorts in O(nlogn) time only for arrays containing objects as elements (Integer, Double, etc. but not int, double). But when I was comparing elements in the array after sorting, it showed some weird behaviour. Comparing elements upto 127, it compares correctly but after that it gives wrong results. Any reason as to why?

Here is my code where seq[] is an Integer array (and not int seq[]):

if(seq[i]!=seq[i-1]) map.put(seq[i],count++);

  • Проголосовать: нравится
  • -23
  • Проголосовать: не нравится

»
7 лет назад, скрыть # |
 
Проголосовать: нравится +9 Проголосовать: не нравится

Do you know the difference between == and equals()?

Integers in range [-128, 127] are created in advance and are taken from cache, while other Integer objects are created newly every time.

»
7 лет назад, скрыть # |
 
Проголосовать: нравится +7 Проголосовать: не нравится

For Integer comparisons, use the method intValue()