How does recursive work for this scenario?

I am using java

public class MapsConfusion {
  public static void main(String[] args) {
    HashMap< Integer, ArrayList<String>> map = new HashMap<>();
    for (int i = 0; i < 15; i++){
      ArrayList<String> lst = new ArrayList<>();
      lst.add("something");
      lst.add("something2");
      map.put(i, lst);
    }
    for(int j = 0; j < 11; j++){
      System.out.println(map.get(j));
    }   
  }  
}

How it works, every time it goes around it, it creates an arrayalist new . Here is my question

When thinking of pointers , when you declare a new Arraylist<>one each time, you create a new one Arraylist at a new address, will I fix it?

Another question . The list does not exist only within scope, that is for loop?. Then how else is available when I do another (last) for the loop?

+4
source share
5 answers

1 question

Yes, you always create a new object and therefore a new address.

. : https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html

2

ArrayList , . , map .

: http://www.dummies.com/programming/java/the-life-cycle-of-a-java-object/

+2

Arraylist < > , Arraylist ?

, . ArrayList .

, for? , () ?

, ArrayList map , for loop.

ArrayList map, - ArrayList for-loop, .

+2

, Arraylist < > , Arraylist ? ?

.

, for? , () ?

, , . map, , map, .

+1

, Arraylist < > , Arraylist ? ?

, , lst .

: , for? , () ?

, lst, looop. , .

, .

0

arrayList, for. :

  • arrayList /, for, .

  • When you started the first for loop, you specified a hash map value entry for each individual list, and the hash map appears to be accessible outside the for loop.

    Converging from the two points above, all lists of arrays are accessible from links made in the HashMap value

0
source

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


All Articles