-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Public class TestDemo {
Public static void main (String args []) {
The int data []=new int [] {7,6,5,4,3,2,1};
ArryData. Sort (data);
ArryData. Tell (data);
}
}
The class ArryData {
Public static void the sort (int [] 'arry) {
Int \ []=new int [' arry. Length].
Int foot='arry. Length - 1;//define temp array corner mark
For (int x=0; x<'arry. Length; X++) {
\ [foot]='arry [x];
Foot=foot - 1;
}
'arry=temp;
}
Public static void tell (int [] 'arry) {
For (int x=0; x<'arry. Length; X++) {
System. The out. Println (' arry [x]).
}
}
}
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Public class TestDemo {
Public static void main (String args []) {
The int data []=new int [] {7,6,5,4,3,2,1};
Int \ []=new int [data. Length];
Int foot=data. The length - 1;//define temp array corner mark
For (int x=0; x
Foot=foot - 1;
}
data=https://bbs.csdn.net/topics/temp;
ArryData. Tell (data);
}
}
The class ArryData {
Public static void tell (int [] 'arry) {
For (int x=0; x<'arry. Length; X++) {
System. The out. Println (' arry [x]).
}
}
}
CodePudding user response:
So I don't know you don't understand.Corresponding reference variable data -- - "new int [] {7,6,5,4,3,2,1}"
In ArryData. You call sort (data) method, you incoming aren't actually data this stuff, but the data corresponding to the "reference new int [] 7,6,5,4,3,2,1 {}",
And in this way, can change the "reference new int [] 7,6,5,4,3,2,1 {}", but will not change the variable data and references to new int [] 7,6,5,4,3,2,1} {corresponding relation,
If you can understand this, behind is easy to understand,
First of all, you built an object temp, and the 'arry flashback,2,3,4,5,6,7 {1} success is assigned to the temp, key:
You directly to the 'arry=temp, like this operation explanation is:
'arry, corresponding to the reference by the "new int [] {7,6,5,4,3,2,1}" into a "new int [],2,3,4,5,6,7 {1}"
But you did not change "new int [] 7,6,5,4,3,2,1 {}" the contents of this reference,
So, from this method, as reference "new int [] {7,6,5,4,3,2,1}" did not change, so the corresponding data values or "new int [] 7,6,5,4,3,2,1 {}"
The solution is in ArryData. Sort (data) in this way can be the reference for operation, as shown in the following:
The 'arry=temp; Modified to
For (int x=0; x
}
With respect to ok, don't understand can reply
CodePudding user response:
The class ArryData {Public static void the sort (int [] 'arry) {
Int \ []=new int [' arry. Length].
Int foot='arry. Length - 1;//define temp array corner mark
For (int x=0; x<'arry. Length; X++) {
\ [foot]='arry [x];
Foot=foot - 1;
}
'arry=temp;//do you understand 'arry and what is the main method of data, the data in the main method (i.e., scope in the main method), sort method can't see the data, it see' arry was as same as the data only because when the parameter values' arry=data, but you back to 'arry assignment in the sort an array=temp, namely' arry!=data, just changed the 'arry, did not change the data
}
Change the
The class ArryData {
Public static void the sort (int [] 'arry) {
Int \ []=new int [' arry. Length].
Int foot='arry. Length - 1;//define temp array corner mark
For (int x=0; x<'arry. Length; X++) {
\ [foot]='arry [x];
Foot=foot - 1;
}
//'arry=temp;
System. Arraycopy (temp, 0, 'arry, 0,' arry. Length);//change so that you can, because the 'arry=data did not change, copy temp to' arry is copied to the data
}
CodePudding user response:
Upstairs said is very clear!In addition, there is a way to solve your problems, you of the sort function can be simplified into the following:
Public static void the sort (int [] 'arry) {
For (int I=0; I & lt; 'arry. Length/2; I++) {
Int temp='arry [I];
'arry [I]=' arry [' arry. Length - I - 1);
'arry [' arry. Length - I - 1]=temp;
}
}
CodePudding user response:
The teacher hello! I'm still a little confused, I understand is' arry=temp executed, is to keep the temp stack memory address given 'arry, let' arry also points to the temp to heap memory contents, namely,2,3,4,5,6,7 {1}, so 'arry return should also be,2,3,4,5,6,7 {1}, ArryData. Sort (data) this method also is to let the data point to temp to heap memory contents, teacher to help me with my trouble where there is a problem, because not born by training, is a little hard to understand, thank you teacher! ??CodePudding user response: