Home > Back-end >  Everybody to help me have a look at that code what went wrong?
Everybody to help me have a look at that code what went wrong?

Time:09-23

Have just met a little problem, began to code, everybody to help me have a look at why the results of the program will be such?
In the code below, in my Sort class defines a turn () method, want to finish an array of transposed, let the front of the elements in an array,
But when I call this method, the output is the content of the original array, did not happen before and after, why on earth would lead to such result, please?
The code is as follows:

 
The class Sort
{
Public static void printArray (int [] temp) {//an array for printing method
For (int x: temp) {
System. The out. Print (x + "\ t");
}
System.out.println();
}

Public static void turn (int [] temp) {//an array of transposed method
Int t=new int [] [] (temp. Length);
Int foot=temp. Length - 1;
For (int x=0; X & lt; Temp. Length; X++) {
T [foot -]=temp [x];
}
Temp=t;
}
}
Public class ArrayDemo
{
Public static void main (String args []) {
The int data []=new int [],9,0,2,3,5,10,7,6,1 {8};
Sort. Turn (data);
Sort the printArray (data);//output: 8,9,0,2,3,5,10,7,6,1
}
}

CodePudding user response:

The
refer to the original poster bellyreborn response:
code is just beginning to self-study, met a little problem, everybody to help me have a look at why the results of the program will be such?
In the code below, in my Sort class defines a turn () method, want to finish an array of transposed, let the front of the elements in an array,
But when I call this method, the output is the content of the original array, did not happen before and after, why on earth would lead to such result, please?
The HTML code is as follows:


Pay attention to the variable scope
 
The class Sort
{
Public static void printArray (int [] temp) {//an array for printing method
For (int x: temp) {
System. The out. Print (x + "\ t");
}
System.out.println();
}

Public static int [] turn (int [] temp) {//an array of transposed method
Int t=new int [] [] (temp. Length);
Int foot=temp. Length - 1;
For (int x=0; X & lt; Temp. Length; X++) {
T [foot -]=temp [x];
}
return t;
}
}
Public class ArrayDemo
{
Public static void main (String args []) {
The int data []=new int [],9,0,2,3,5,10,7,6,1 {8};
data=https://bbs.csdn.net/topics/Sort.turn (data);
Sort the printArray (data);
}
}

CodePudding user response:

The
refer to the original poster bellyreborn response:
code is just beginning to self-study, met a little problem, everybody to help me have a look at why the results of the program will be such?
In the code below, in my Sort class defines a turn () method, want to finish an array of transposed, let the front of the elements in an array,
But when I call this method, the output is the content of the original array, did not happen before and after, why on earth would lead to such result, please?
The code is as follows:

 
The class Sort
{
Public static void printArray (int [] temp) {//an array for printing method
For (int x: temp) {
System. The out. Print (x + "\ t");
}
System.out.println();
}

Public static void turn (int [] temp) {//an array of transposed method
Int t=new int [] [] (temp. Length);
Int foot=temp. Length - 1;
For (int x=0; X & lt; Temp. Length; X++) {
T [foot -]=temp [x];
}
Temp=t;
}
}
Public class ArrayDemo
{
Public static void main (String args []) {
The int data []=new int [],9,0,2,3,5,10,7,6,1 {8};
Sort. Turn (data);
Sort the printArray (data);//output: 8,9,0,2,3,5,10,7,6,1
}
}

You just put the value after unilateral assign values to the face value is not you can exchange
 public static void turn (int [] temp) {//the method of array transposed 
Int [] t=new int [temp. Length];
Int tempx;
For (int x=0; X & lt; Temp. Length/2; X++) {
Tempx=temp [x];
\ [x]=temp/temp. Length - x - 1;
Temp/temp. Length - x - 1=tempx;
}
Temp=t;
}

CodePudding user response:

reference 1st floor qq_39936465 response:
Quote: refer to the original poster bellyreborn response:

Have just met a little problem, began to code, everybody to help me have a look at why the results of the program will be such?
In the code below, in my Sort class defines a turn () method, want to finish an array of transposed, let the front of the elements in an array,
But when I call this method, the output is the content of the original array, did not happen before and after, why on earth would lead to such result, please?
The HTML code is as follows:


Pay attention to the variable scope
 
The class Sort
{
Public static void printArray (int [] temp) {//an array for printing method
For (int x: temp) {
System. The out. Print (x + "\ t");
}
System.out.println();
}

Public static int [] turn (int [] temp) {//an array of transposed method
Int t=new int [] [] (temp. Length);
Int foot=temp. Length - 1;
For (int x=0; X & lt; Temp. Length; X++) {
T [foot -]=temp [x];
}
return t;
}
}
Public class ArrayDemo
{
Public static void main (String args []) {
The int data []=new int [],9,0,2,3,5,10,7,6,1 {8};
data=https://bbs.csdn.net/topics/Sort.turn (data);
Sort the printArray (data);
}
}



Thank you very much for the reply, predecessors provide return value method can solve the problem, but also simple and clear,

About "variable scope", however, I still don't understand why wrong, actually I'm such a tangle on this issue, because the array is a reference data type, in my code, pass by reference, temp [] -> data [], then in turn () through a series of operations, the data before and after the [] of elements in the reverse order assigned to the new array t [], then the array t [] after the assignment to assign a value to the data [], why this process involves the problem of variable scope?

I am in my code to increase the numerical test variables, please look at the code below:
 
The class Sort
{
Public static void printArray (int [] temp) {//an array for printing method
For (int x: temp) {
System. The out. Print (x + "\ t");
}
System.out.println();
}

Public static void turn (int [] temp) {//an array of transposed method
Int t=new int [] [] (temp. Length);
Int foot=temp. Length - 1;
For (int x=0; X & lt; Temp. Length; X++) {
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related