實戰(數組操作)
class Arr
{
private int data[];
private int foot;
public Arr(int len){
if (len>0)
{
this.data=new int[len];
}
else{
this.data=new int[1];
}
}
public boolean add(int num){
if (this.foot<this.data.length)
{
this.data[this.foot++]=num;
return true;
}
return false;
}
public int[]getData(){
return this.data;
}
}
class SortArray extends Arr
{
public SortArray(int len){
super(len);
}
public int[]getData(){
java.util.Arrays.sort(super.getData());//排序
return super.getData();
}
}
class Reverse extends Arr
{
public Reverse(int len){
super(len);
}
public int[]getData(){
int center=super.getData().length/2;
int head=0;
int tail=super.getData().length-1;
for (int x=0;x<center ;x++ )
{
int temp=super.getData()[head];
super.getData()[head]=super.getData()[tail];
super.getData()[tail]=temp;
head++;
tail--;
}
return super.getData();
}
}
public class Array
{
public static void main(String args[]){
SortArray ar=new SortArray(3);
System.out.println(ar.add(100));
System.out.println(ar.add(19));
System.out.println(ar.add(30));
System.out.println(ar.add(20));
System.out.println(ar.add(300));
int[] temp=ar.getData();
for (int x=0;x<temp.length ;x++ )
{
System.out.println(temp[x]);
}
Reverse re=new Reverse(3);
System.out.println(re.add(100));
System.out.println(re.add(19));
System.out.println(re.add(30));
System.out.println(re.add(20));
System.out.println(re.add(300));
int[] temp1=re.getData();
for (int x=0;x<temp1.length ;x++ )
{
System.out.println(temp1[x]);
}
}
}
實戰(數組操作)