Solve the Problem of changing the Data Association caused by the array reference type

Source: Internet
Author: User

In today's JSP development, the following problems occur:
Use string [] STR to retrieve a group of data placed in arraylist <string []>, calculate individual data items in STR, and then use add (index and data to be stored) in the arraylist. Because the string type is the reference data type, when the STR data is changed, the data referenced by the str in the arraylist will also be changed, however, I only want to get the arraylist data as a new object and do not want to change the content of the arraylist. If no processing is performed, the STR data is always the same as the data in the referenced arraylist, and sometimes unexpected errors are generated,
Sample Code
Arraylist <string []> STR = new arraylist <string []> ();
String [] str1 = {"CSZ", "CSZ", "S "};
String [] str2 = {"dfasf", "fesealing", "egsegs "};
String [] str3 = {"htrjh", "htdj", "vdtjhng "};
String [] str4 = {"aaaaaaaa", "aaaaaaaaaa", "aaaaaaaaaaaa "};
Str. Add (str1 );
Str. Add (str2 );
Str. Add (str3 );
Str. Add (str4 );
// Obtain data
String [] str5 = Str. Get (0 );
// Modify data
Str5 [0] = "qqqqq ";
Str5 [1] = "qqqqqqqqqq ";
Str5 [2] = "dsdgdfjhfjygkt6j6r6jsrjrsj6rsj6rwjsfrths ";
// Place the modified data in the specified location of arraylist
Str. Add (4, str5 );
// Print the output
For (INT I = 0; I <5; I ++ ){
For (string S: Str. Get (I ))
{
System. Out. Print (S + "");
}
System. Out. println ();
}

Running result:
Qqqqq qqqqqqqqqqqq dsdgdfjhfjygkt6j6r6jsrjrsj6rsj6rwjsfrths
Dfasf fesealing egsegs
Htrw.htdj vdtjhng
Aaaaaaaa aaaaaaaaaa
Qqqqq qqqqqqqqqqqq dsdgdfjhfjygkt6j6r6jsrjrsj6rsj6rwjsfrths

It can be seen that the inconvenience caused by this association in development often results in errors that developers do not want. The solution is:
Use an array to retrieve a group of data, and then use multiple strings to retrieve each data item in the array, then, when assigning these string variables to each item of A New String [], the data in the arraylist will not be changed when this array is calculated or changed.
Change code:
Arraylist <string []> STR = new arraylist <string []> ();
String [] str1 = {"CSZ", "CSZ", "S "};
String [] str2 = {"dfasf", "fesealing", "egsegs "};
String [] str3 = {"htrjh", "htdj", "vdtjhng "};
String [] str4 = {"aaaaaaaa", "aaaaaaaaaa", "aaaaaaaaaaaa "};
Str. Add (str1 );
Str. Add (str2 );
Str. Add (str3 );
Str. Add (str4 );
// Retrieve a group of data
String [] str6 = Str. Get (0 );
// Use the string variable to store each item of data.
String a1 = str6 [0];
String a2 = str6 [1];
String a3 = str6 [2];
// Define a new array
String [] str5 = new string [3];
// Assign the variables to str5 respectively
Str5 [0] = A1;
Str5 [1] = a2;
Str5 [2] = A3;
// The change to str5 does not change the data in the original arraylist.
Str5 [0] = "qqqqq ";
Str5 [1] = "qqqqqqqqqq ";
Str5 [2] = "dsdgdfjhfjygkt6j6r6jsrjrsj6rsj6rwjsfrths ";
// Add sre5 to arraylist
Str. Add (4, str5 );
// Print the output
For (INT I = 0; I <5; I ++ ){
For (string S: Str. Get (I ))
{
System. Out. Print (S + "");
}
System. Out. println ();
}
Running result:
CSZ s
Dfasf fesealing egsegs
Htrw.htdj vdtjhng
Aaaaaaaa aaaaaaaaaa
Qqqqq qqqqqqqqqqqq dsdgd

Solve the Problem of changing the Data Association caused by the array reference type

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.