0

'm having issues understanding what exactly this piece of code does:

1.   HashMap<Integer, Integer> totals = new HashMap<>();
2.       
3.          for (int i = -NBR_STEPS; i <= NBR_STEPS; i++) {
4.              totals.put(i, 0);
5.          }
6.          System.out.println("");
7.          for (int i = 0; i < NBR_WALKS; i++) {
8.              int total_value = 0;
9.              for (int j = 0; j < NBR_STEPS; j++) {
10.                 int L = (int) (Math.random() * 2);
11.                 total_value += (L == 0) ? -1 : 1;
12.             }
13.             totals.put(total_value, totals.get(total_value) + 1);
14.         }
15. }

What I don't understand:

  1. What does totals.put(i,0) do?
  2. What does total_value += (L == 0) ? -1 : 1; do exactly?
  3. What does totals.put(total_value, totals.get(total_value)+1); do?

I'm sorry that I'm asking this question, but I simply don't understand. Thank you:)

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Paludan
  • 622
  • 1
  • 5
  • 21

4 Answers4

3

What does totals.put(i,0) do?

Stores the value 0 at key i in the hashmap totals.

What does total_value += (L == 0) ? -1 : 1; do exactly?

total_value += 1; means the same as total_value = total_value + 1

? is the ternary operator. The value of this statement (L == 0) ? -1 : 1; is -1 when (L == 0) is true and 1 when (L == 0) is false

total_value += (L == 0) ? -1 : 1; is the same as:

if (L == 0)
    total_value = total_value - 1; // subtract 1 from total_value 
else
    total_value = total_value + 1; // add 1 to total_value 

What does totals.put(total_value, totals.get(total_value)+1); do?

Copies the value from hashmap location total_value+1 into hashmap location total_value. It essentially copies a value from the next location to the current location.

Community
  • 1
  • 1
dansalmo
  • 11,506
  • 5
  • 58
  • 53
1

EDIT: A Hashmap saves key-value pairs. So the first argument is the key, the second is the value.

a += 1; means the same as a = a + 1

a = b == 1 ? 1 : 2; means the same like:

if(b == 1)
  a = 1;
else
  a = 2;
PKeidel
  • 2,559
  • 1
  • 21
  • 29
  • The first argument for put is the key, the second argument is the value. – dansalmo Dec 11 '13 at 18:37
  • Alright super thank you:) I think I understand my question now with the comments and this answer. Unfortunately someone thumbed me down:( – Paludan Dec 11 '13 at 18:39
  • 1
    "The second argument for put is the optional index where the element should be placed in the hashmap" ... couldn't be more wrong! There is no order of elements in a Hashmap, hence no index. As dansalmo said: put(key, value) – Kai Huppmann Dec 11 '13 at 18:42
1

What does totals.put(i,0) do? It adds a key, value pair to the map. The variable 'i' is the key and '0' is the value associated with the key.

What does total_value += (L == 0) ? -1 : 1; do exactly? That is an If-else statement. The simplified version is as such.

if(L==0)
total_value += -1
else{
total_value += 1
}

What does totals.put(total_value, totals.get(total_value)+1); do?

It adds a key value pair to the hashmap. total_value is the key and totals.get(total_value)+1 is the value associated for the key.

Hope that helps.

Nilsaw
  • 103
  • 7
1

About the question n.2
A traditional if-else construct in C, Java and JavaScript is written:

if (a > b) {
    result = x;
} else {
    result = y;
}

This can be rewritten as the following ternary statement:

result = a > b ? x : y;

in your case the condition is (L==0) if this is true:

total_value=total_value+-1

and so...

total_value=total_value-1

if the condition is not true:

total_value=total_value+1
Giovanni Far
  • 1,623
  • 6
  • 23
  • 37