An array in a fancy Java program

I came across this Java program and its behavior is unpredictable. The following program calculates the differences between pairs of elements in an int array.

import java.util.*; public class SetTest { public static void main(String[] args) { int vals[] = {786,678,567,456, 345,234,123,012}; Set<Integer> diffs = new HashSet<Integer>(); for(int i=0; i < vals.length ; i++) for(int j = i; j < vals.length; j++) diffs.add(vals[i] - vals[j]); System.out.print(diffs.size()); } } 

If we analyze, it seems that the size of the set should be 8, which is the size of the array. But if you run this program, it will print 14. What is happening? Any ideas?

Thanks in advance.

Answer: This strange behavior happens because 012 in the array becomes octal, if we change it to 12, then it prints 8 as expected.

Lesson: never put an integer literal with zeros.

+4
source share
6 answers

Did you notice that 012 (octal) is 10 (decimal)?

+11
source

If we analyze, it seems that the given size should be 8, which is the size of the array. But if you run this program, it prints 14. What is happening? Any idea?

Just keep track of information.

 1 : 786 - 786 = 0 (new one 1 ) 2 : 786 - 678 = 108 (new one 2 ) 3 : 786 - 567 = 219 (new one 3 ) 4 : 786 - 456 = 330 (new one 4 ) 5 : 786 - 345 = 441 (new one 5 ) 6 : 786 - 234 = 552 (new one 6 ) 7 : 786 - 123 = 663 (new one 7 ) 8 : 786 - 10 = 776 (new one 8 ) 9 : 678 - 678 = 0 (already contained) 10 : 678 - 567 = 111 (new one 9 ) 11 : 678 - 456 = 222 (new one 10 ) 12 : 678 - 345 = 333 (new one 11 ) 13 : 678 - 234 = 444 (new one 12 ) 14 : 678 - 123 = 555 (new one 13 ) 15 : 678 - 10 = 668 (new one 14 ) 16 : 567 - 567 = 0 (already contained) 17 : 567 - 456 = 111 (already contained) 18 : 567 - 345 = 222 (already contained) 19 : 567 - 234 = 333 (already contained) 20 : 567 - 123 = 444 (already contained) 21 : 567 - 10 = 557 (new one 15 ) 22 : 456 - 456 = 0 (already contained) 23 : 456 - 345 = 111 (already contained) 24 : 456 - 234 = 222 (already contained) 25 : 456 - 123 = 333 (already contained) 26 : 456 - 10 = 446 (new one 16 ) 27 : 345 - 345 = 0 (already contained) 28 : 345 - 234 = 111 (already contained) 29 : 345 - 123 = 222 (already contained) 30 : 345 - 10 = 335 (new one 17 ) 31 : 234 - 234 = 0 (already contained) 32 : 234 - 123 = 111 (already contained) 33 : 234 - 10 = 224 (new one 18 ) 34 : 123 - 123 = 0 (already contained) 35 : 123 - 10 = 113 (new one 19 ) 36 : 10 - 10 = 0 (already contained) 

In addition, the debugger is a great thing in cases where you can not analyze the behavior of the program;)

+3
source

Since you are running two nested loops, the add() method is called several times. Since the set cannot contain duplicate objects, the number of values ​​in the set will be the number of unique values. The add() function returns true if the set already has no element and false if the set already had an element.

Change the line

 diffs.add(vals[i] - vals[j]); 

to

 System.out.println((diffs.add(vals[i] - vals[j]))); 

to understand what I mean.

+3
source

For a set of 8 elements, there are 36 (8 + 7 + ... + 1) different pairs (if you ignore the order of the elements in pairs). Thus, diffs can contain up to 36 elements. But, apparently, some differences occur several times, for example. 456-345 = 111 and 345-234 = 111. Since the set can contain only each value no more than once, only 14 different differences remain.

-1
source

Your HashSet stores non-duplicate values. If you want to duplicate, you need to use a different type of set. The size of the differences should be 36 with duplicates; I get size 19 when I run the as-is code (19 unique different values).

-1
source

In fact, he displays 19 ...

You have 36 calculations, of which 17 have the same result. HashSet stores unique values, so we do not have size 36, and size 19 ...

-1
source

Source: https://habr.com/ru/post/1342635/


All Articles