Home > Back-end >  The for loop about Java
The for loop about Java

Time:11-06

 
Map SourceAsMap=ESUnit. GetSourceAsMap (searchHit searchMethod, searchField, searchFieldTwo);
String authorObjectName=sourceAsMap. Get (" AuthorObjectJson. The Name ")==null?" ": sourceAsMap. Get (" AuthorObjectJson. The Name"). The toString ();
List AuthorObjectJson=(List & gt;) AuthorObjectJson sourceAsMap. Get (" ");
For (Map The map: authorObjectJson) {
String a=authorObjectName. ReplaceAll (" & lt; Em> | & lt;/em>" , "");
String String=map. Get (" Name ");
{if (a.e quals (string))
The map. The put (" Name ", authorObjectName);
}
}


Perform such a piece of code, I inside the for loop using the map. The put (" Name ", authorObjectName); Change the map object that current traversal, why authorObjectJson this object also follow change, the most magical is why sourceAsMap value also follow changes inside the object?

CodePudding user response:

You traverse map object is not traverse authorObjectJson object? Traversed you modify the content, of course authorObjectJson will change, and authorObjectJson is sourceAsMap values, sourceAsMap change is yes
Note whether you use sourceAsMap. Get (key) or for loop, you are to get the object reference address, you to modify the address corresponding to the content, before must have been a reference to the revision

CodePudding user response:

You need to understand the first programming language in the "reference" and "value transfer" these two concepts,
In simple terms, reference is the object itself, value transfer is the passed object "copy",

CodePudding user response:

The
reference 1/f, Amin has existed for response:
you traverse map object is not traverse authorObjectJson object? Traversed you modify the content, of course authorObjectJson will change, and authorObjectJson is sourceAsMap values, sourceAsMap change is yes
Note whether you use sourceAsMap. Get (key) or for loop, you are to get the object reference address, you to modify the address corresponding to the content, before must have been a reference to modify


Seems to be the way it is, but I this String authorObjectName=sourceAsMap. Get (" AuthorObjectJson. The Name ")==null?" ": sourceAsMap. Get (" AuthorObjectJson. The Name"). The toString (); Not declare a authroObjectName object, then the assignment? How to address or reference?

CodePudding user response:

reference 4 floor qq_charm response:
Quote: reference 1/f, Amin has existed for response:
you traverse map object is not traverse authorObjectJson object? Traversed you modify the content, of course authorObjectJson will change, and authorObjectJson is sourceAsMap values, sourceAsMap change is yes
Note whether you use sourceAsMap. Get (key) or for loop, you are to get the object reference address, you to modify the address corresponding to the content, before must have been a reference to modify


Seems to be the way it is, but I this String authorObjectName=sourceAsMap. Get (" AuthorObjectJson. The Name ")==null?" ": sourceAsMap. Get (" AuthorObjectJson. The Name"). The toString (); Not declare a authroObjectName object, then the assignment? How to address or reference?


AuthorObjectName is to declare a string, the value from beginning to end have no change, change is authorObjectJson, sourceAsMap

CodePudding user response:

The map. The put (" Name ", authorObjectName);//the map you come from? In the authorObjectJson is come from?
If LZ can read the following examples, I should to know

List AuthorObjectJson=new ArrayList<> (a);
List List=new ArrayList<> (a);
List. The add (" 123 ");
AuthorObjectJson. Add (list);

List List2=authorObjectJson. Get (0);//here list2 variables is equivalent to the LZ map
System. The out. Println (list==list2);//you will see that this is true, so
List2. Add (" 456 ");//the list will also add a "456", the list in authorObjectJson the list changed, authorObjectJson also changed?
List2. Here the add is equivalent to LZ map. The put, than think it has changed is the map, but in fact the map and authorObjectJson the contents are the same (like the list==list2), so change the map, authorObjectJson also change


CodePudding user response:

Go to the next: the difference between a reference type and value type
  • Related