Home > Back-end >  New people for help: array transposed problem, why didn't program error, output without transpo
New people for help: array transposed problem, why didn't program error, output without transpo

Time:10-08

Why didn't this array transposed? Code is not an error, it is not transposed,
 public class ShuZuJiCheng {
Public static void main (String args []) {
/*
* validation:
*/
//instantiate the object, and through constructing method is passed the length of the array
ArrayTransposition ar=new ArrayTransposition (8);
//call the assign method for each element of the array assignment
Ar. Assign (66);
Ar. Assign (22);
Ar. Assign (55);
Ar. Assign (33);
Ar. Assign (99);
Ar. Assign (11);
Ar. Assign (88);
Ar. Assign (77);
//to call the add method expanded scope of array
Ar. The add (2);
Ar. Assign (44);
Ar. Assign (100);
//call getArr method after the assignment of array
For (int I=0; iSystem. Out. Print (ar) getArr () [I] + ", ");
}
}
}
//define an Array class, let the object can be obtained through dynamic expansion of an Array
The class Array {
//declare an array
Private int arr [];
//declare an int variable index, said access to the array index, cannot use a for loop, because access to the array index of object method calls will repeat visit
//if set for loops, and each object when this method is called, are cycle, so will only assign objects to the last call when the value of the
Private int index;
//using the constructor gives the length of the array and the value of each element
Public Array (int len) {
//require incoming length cannot be less than 0
If (len<0 {
This. Arr=new int [1].
}
This. Arr=new int [len];
}
/method/set each element of the array, the parameters for each object call assignment number
Public void the assign (int num) {
//requirement index after the + + operation can not be greater than or equal to the array length
If (index & lt; This. Arr. Length) {
This. Arr [index++]=num;
}
}
//if you want to extend the length of the array and the elements, you can define the following method
Public void the add (int) plus {
The int data []=new int [this. Arr. Length + plus];
System. Arraycopy (this. Arr, 0, data, 0, enclosing arr. Length);
This. Arr=data;
}
//set up a method to get each element of the array
Public int [] getArr () {
Return this. Arr;
}
}
//define a subclass of Array class Array has the function of transposed
The class ArrayTransposition extends Array {
//because subclass instantiation objects before will call the superclass constructor instantiate the superclass object, and as the superclass constructor parameters, is must clearly with super () method
//call the superclass constructor
Public ArrayTransposition (int len) {
Super (len);
}
//need a method to transpose arrays, and can be transposed after the value of the array is an array by the values of the parent class getArr method already can do
//so can take override this method to extend this method, because the method override a function is the function of the extension method
Public int [] getArr () {
Int time=super. GetArr (.) length/2;
Int head=0;
Int tail=super. GetArr (.) length - 1.
For (int I=0; iInt middle=super. GetArr () [head];
Super. GetArr () [head]=super. GetArr () (tail);
Super. GetArr () [tail]=middle;
The head + +;
Tail -;
}
Return super. GetArr ();
}
}

The output is:

  • Related