Java: differences between array and set classes when passing parameters (problem solved, thank you for reminding me)

Source: Internet
Author: User

Recently, when I was working on a project, I encountered a problem of passing List sets as parameters. I remembered the previous summary of parameter passing. parameter passing includes value passing and reference passing. The parameters of the Set class should belong to reference passing, the array suddenly flashed into my mind, and I felt that the array was also a special set and should also be a reference transfer. So I gave a simple example for testing, but the test results fell sharply, the array has not implemented the same effect as the List set. Now we will List the problems. Please advise me and make a summary after the problem is solved. The problem is as follows:
First define a class Test1:
[Java]
Public class Test1 {

// This part is for list operations. First, define an empty list set container.
List <String> list = null;
// Use the list collection container as the parameter Constructor
Public Test1 (List <String> list)
{
Super ();
This. list = list;
}
// Add data to the container
Public void putList (){

List. add ("list1 ");
List. add ("list2 ");
List. add ("list3 ");
}

//------------------------------------------------------------------

// This part is an array operation. First, define an empty array.
String [] str = null;
// Use arrays as the parameter Constructor
Public Test1 (String [] str)
{
This. str = str;
}
// Add data to the array
Public void putString ()
{
Str = new String [] {"string1 "};
}
}
This class defines the processing of the List set and the array Str respectively. For a List set, first define a List set as the set of parameters, and then increase the data of the set by using the putList () method, and perform similar operations on the data, then define an operation class Test2:
 
[Java]
Public class Test2 {
 
// Define an empty list set and an array
Static List <String> list = null;
Static String [] str = null;
// Return data of the list set type
Public static List <String> getList ()
{
List <String> test = new ArrayList <String> ();
Test1 test1 = new Test1 (test );
Test1.putList ();
Return test;
}
// Returns an array of data.
Public static String [] getString ()
{
String [] test = new String [] {"string2 "};
Test1 test1 = new Test1 (test );
Test1.putString ();
Return test;
}
 
Public static void main (String [] args)
{
// Pass the list set obtained in the getList () method to the list and display the content.
List = getList ();
For (Iterator <String> iterator = list. iterator (); iterator. hasNext ();)
{
System. out. println ("List set Result:" + iterator. next (). toString ());
}
// Pass the array obtained in the getString () method to str and display the content.
Str = getString ();
For (int I = 0; I <str. length; I ++)
{
System. out. println ("the array result is:" + str [I]);
}
}
}
This class defines empty List sets and arrays, and obtains methods of their respective types. Then, the values of List sets and arrays are printed in the main method. The results are as follows:
[Java]
The result of the List set is list1.
The result of the List set is list2.
The result of the List set is list3.
The result of the array is string2.
Problem: the List set is passed by address reference when it is passed as a parameter. Therefore, in the getList () method of Test2, after the test of the defined LIst type is passed as the construction method of test1. list = list: Give the test address to the member variable list in Test1, and then call putList () in test1 to add data to the list, because it is an address reference, the value of the passed test is changed, and list1, list2, list3, the return test method is used to return a List set with added data, and finally assign a value to the member variable list in Test2 and output the result. The result is expected, the problem is that the results of the array operation are the same because the two operations are the same. In the getString () method of Test2, an array test is defined and assigned a value, pass it as the constructor of Test1 and use putString () of test1 to modify it. The value of test should be the modified value: string1, but the original value is string2 after the output. Isn't it passed as a value, and the array is not changed?
Solution: first, when the List set and array are used as parameters, address reference must be passed, but an error occurs during programming. After passing the array test in Test2, the array str in Test1 points to this array, but in the putString () method in Test1, the array str points to a new array address, in this way, operations on str will not affect test, so the test in the getString () method has not changed. Well, here, I thought of my poor basic knowledge, continue accumulating. Come on!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.