The operation of the array - Create, Insert, crop, expand, flip, sort

Just started to learn JAVA, notes from the start!

Array operating program 1

purpose

The length of a book, the array [...] an initialized not change; but some array operation (insertion, cutting, expansion, etc.) and requires a change in the length of the array. "The first line of code the JAVA" book 4.3 Inherit sermon: "defect is that the length of the array is fixed, but can be used to solve this problem is solved list ...... Chapter 3" (P220) Thus, in the inheritance before the next study, I use the method of the list to try to solve.

As his notes, please let me know dwellers.

1. The elements of the array class EleOfArr 

/** element of array
 * 链表结构
 * @author Yuan_hang
 *
 */
public class EleOfArr {
	// 属性
	private double num;
	private EleOfArr next;
	
	// 构造方法
	public EleOfArr(){}
	
	public EleOfArr(double num){
		this();
		this.num = num;
	}
	
	public EleOfArr(double num, EleOfArr next){
		this(num);
		this.next = next;
	}
		
	// 方法
	public void setNum(double num){ this.num = num; }
	public double getNum(){ return this.num; }
	
	public void setNext(EleOfArr next){ this.next = next; }
	public EleOfArr getNext(){ return this.next; }
	
	public void addEle(double num){
		this.next = new EleOfArr(num);
	}
	
}

2. Array array class and method

/**
 * Array from EleOfArr
 * 1. 创建一个数列
 * 2. 读取某个位置的数
 * 3. 在数列最末(任意位置)插入一个数
 * 4. 删除某个位置的数
 * 5. 扩展数列,缺数则补齐0
 * 5. 转置操作
 * 6. 排序(正序/逆序)操作
 * @author Yuan_hang
 *
 */
public class Array {
	//属性
	private double[] arr;
	private EleOfArr[] arrTemp;
	private int arrLength;
	
	/**
	 * 构造方法
	 */
	//无参构造
	public Array(){}
	
	//单参数构造
	public Array(double[] arr){
		this.arr = arr;
		
		this.arrLength = arr.length;
		
		this.arrTemp = new EleOfArr[this.arrLength];
		this.arrTemp[this.arrLength-1] = new EleOfArr(arr[this.arrLength-1]);
		for(int x=this.arrLength-2; x>=0; x--){
			this.arrTemp[x] = new EleOfArr(arr[x],arrTemp[x+1]);
		}
	}
	
	//多参数构造
	public Array(double[] arr,int arrLength){
		if(arrLength<0){System.err.println("Array: Length cannot be nagative!");return;}
		
		this.arrLength = (arr.length > this.arrLength)? arr.length: this.arrLength;
		
		this.arr = new double[this.arrLength];
		for(int x=0; x<arr.length; x++){ this.arr[x] = arr[x];}
		
		this.arrTemp = new EleOfArr[this.arrLength];
		this.arrTemp[arr.length-1] = new EleOfArr(arr[arr.length-1]);
		for(int x=arr.length-2; x>=0; x--){
			this.arrTemp[x] = new EleOfArr(arr[x],arrTemp[x+1]);
		}
	}
	
	/**
	 * 方法
	 * @param arr
	 */
	public void setArr(double[] arr){this.arr = arr;}
	public double[] getArr(){return this.arr;}
	
	public void setArrTemp(EleOfArr[] arrTemp){this.arrTemp = arrTemp;}
	public EleOfArr[] getArrTemp(){return this.arrTemp;}
	
	public void setArrLength(int arrLength){this.arrLength = arrLength;}
	public int getArrLength(){return this.arrLength;}
	
	// 打印数列
	public void printArr(){
		for(int x=0; x<this.arr.length; x++){
			System.out.print(this.arr[x] + ", ");
		}
		System.out.print("\n");
	}
	
	// 在数列最末插入一个数
	public void addArrEle(double addNum){
		double[] arrTemp0 = this.arr;
		this.arrLength = arr.length + 1;
		
		this.arr = new double[this.arrLength];
		for(int x=0; x<this.arrLength - 1; x++){
			this.arr[x] = arrTemp0[x];
		}
		this.arr[this.arrLength-1] = addNum;
		
		this.arrTemp = new EleOfArr[this.arrLength];
		this.arrTemp[this.arrLength-1] = new EleOfArr(arr[this.arrLength-1]);
		for(int x=this.arrLength-2; x>=0; x--){
			this.arrTemp[x] = new EleOfArr(this.arr[x],arrTemp[x+1]);
		}
	}
	
	// 在数列任意位置插入一个数
	public void addArrEle(double addNum, int index){
		if(index<0){System.err.println("addArrEle: Index cannot be nagative!");return;}
		
		this.arrLength = (index + 1 > this.arrLength)? index + 1 : this.arrLength + 1;
		
		double[] arrTemp0 = this.arr;
		this.arr = new double[this.arrLength];
		int indexTemp = (index > arrTemp0.length-1)? arrTemp0.length : index +1;
		for(int x=0; x<indexTemp; x++){
			this.arr[x] = arrTemp0[x];
		}
		this.arr[index] = addNum;
		if(indexTemp == index + 1){
			for(int x=indexTemp; x<this.arrLength; x++){
				this.arr[x] = arrTemp0[x-1];
			}
		}
		
		this.arrTemp = new EleOfArr[this.arrLength];
		this.arrTemp[this.arrLength-1] = new EleOfArr(arr[this.arrLength-1]);
		for(int x=this.arrLength-2; x>=0; x--){
			this.arrTemp[x] = new EleOfArr(this.arr[x],arrTemp[x+1]);
		}
	}
	
	// 删除数列的一段
	public void delectArr(int beginIndex, int endIndex){
		if(beginIndex<0){System.err.println("extendArr: Begin index cannot be nagative!");return;}
		if(beginIndex>endIndex){System.err.println("extendArr: Begin index cannot be larger than end index!");return;}
		if(endIndex>this.arrLength-1){System.err.println("extendArr: End index should not beyond the array!");return;}
		
		double[] arrTemp0 = this.arr;
		int delLength = endIndex - beginIndex + 1;
		this.arrLength = this.arrLength - delLength;
		
		this.arr = new double[this.arrLength];
		for(int x=0; x<this.arrLength; x++){
			if(x<beginIndex){this.arr[x] = arrTemp0[x];}
			else{this.arr[x] = arrTemp0[x+delLength];}
			
		}
		
		this.arrTemp = new EleOfArr[this.arrLength];
		this.arrTemp[this.arrLength-1] = new EleOfArr(arr[this.arrLength-1]);
		for(int x=this.arrLength-2; x>=0; x--){
			this.arrTemp[x] = new EleOfArr(this.arr[x],arrTemp[x+1]);
		}
		
	}
	
	// 扩展或裁剪数列,缺数则补齐0
	public void extendArr(int beginIndex, int endIndex){
		if(beginIndex<0){System.err.println("extendArr: Begin index cannot be nagative!");return;}
		if(beginIndex>endIndex){System.err.println("extendArr: Begin index cannot be larger than end index!");return;}
		
		this.arrLength = endIndex - beginIndex + 1;
		double[] arrTemp0 = this.arr;
		this.arr = new double[this.arrLength];
		
		if(beginIndex<arrTemp0.length){
			if(endIndex<arrTemp0.length){
				for(int x=beginIndex; x<beginIndex+this.arrLength; x++){
					this.arr[x-beginIndex] = arrTemp0[x];
				}
			}
			else{
				for(int x=beginIndex; x<arrTemp0.length;x++){
					this.arr[x-beginIndex] = arrTemp0[x];
				}
			}
			
		}
		
		this.arrTemp = new EleOfArr[this.arrLength];
		this.arrTemp[this.arrLength-1] = new EleOfArr(arr[this.arrLength-1]);
		for(int x=this.arrLength-2; x>=0; x--){
			this.arrTemp[x] = new EleOfArr(this.arr[x],arrTemp[x+1]);
		}
	}
	
	// 数列转置
	public void transpArr(){
		double[] arrTemp0 = this.arr;
		this.arr = new double[this.arrLength];
		int y = this.arrLength;
		for(int x=0; x<this.arrLength; x++){
			y--; this.arr[x] = arrTemp0[y];
		}
		
		this.arrTemp = new EleOfArr[this.arrLength];
		this.arrTemp[this.arrLength-1] = new EleOfArr(arr[this.arrLength-1]);
		for(int x=this.arrLength-2; x>=0; x--){
			this.arrTemp[x] = new EleOfArr(this.arr[x],arrTemp[x+1]);
		}
	}
	

	// 数列排序,sortType>0 为正序,sortType<0 为逆序
	public void sortArr(int sortType){
		if(sortType > 0){
			for(int x = 0; x < this.arrLength - 1; x++){
				for(int y = 0; y < this.arrLength - 1;y++){
					if(this.arr[y]>this.arr[y+1]){
						double temp = this.arr[y+1];
						this.arr[y+1] = this.arr[y]; 
						this.arr[y] = temp;
					}
				}
			}
		}
		
		else if(sortType < 0){
			for(int x = 0; x < this.arrLength - 1; x++){
				for(int y = 0; y < this.arrLength - 1;y++){
					if(this.arr[y]<this.arr[y+1]){
						double temp = this.arr[y+1];
						this.arr[y+1] = this.arr[y]; 
						this.arr[y] = temp;
					}
				}
			}
		}
		
		this.arrTemp = new EleOfArr[this.arrLength];
		this.arrTemp[this.arrLength-1] = new EleOfArr(arr[this.arrLength-1]);
		for(int x=this.arrLength-2; x>=0; x--){
			this.arrTemp[x] = new EleOfArr(this.arr[x],arrTemp[x+1]);
		}
	}
	
}

3. debugging results

/**
 * 测试
 * @author Yuan_hang
 *
 */
public class Test {
	public static void main(String[] args){
		//创建Array成员
		Array a = new Array(new double[]{10,6,2,4,3,5,1,9,7,8});
		System.out.println("原始数组: ");
		a.printArr();
		
		System.out.println("\n** 长度: " + a.getArrLength());
		System.out.println("** 取出第四个元素: " + a.getArrTemp()[4].getNum());
		System.out.println("** 下一个元素: " + a.getArrTemp()[4].getNext().getNum());
		
		// 测试
		a.addArrEle(66,5);
		System.out.println("\n中间插入新元素: ");
		a.printArr();
		
		a.addArrEle(88);
		System.out.println("\n末尾插入新元素: ");
		a.printArr();
		
		a.delectArr(3,6);
		System.out.println("\n删除中间的部分: ");
		a.printArr();
		
		a.extendArr(2,6);
		System.out.println("\n截取中间的部分: ");
		a.printArr();
		
		a.extendArr(0,6);
		System.out.println("\n扩展数组: ");
		a.printArr();
		
		a.transpArr();
		System.out.println("\n数组转置: ");
		a.printArr();
		
		a.sortArr(1);
		System.out.println("\n数组正序排列: ");
		a.printArr();
		
		a.sortArr(-1);
		System.out.println("\n数组逆序排列: ");
		a.printArr();
	}
}

4. Verify the output

原始数组: 
10.0, 6.0, 2.0, 4.0, 3.0, 5.0, 1.0, 9.0, 7.0, 8.0, 

** 长度: 10
** 取出第四个元素: 3.0
** 下一个元素: 5.0

中间插入新元素: 
10.0, 6.0, 2.0, 4.0, 3.0, 66.0, 5.0, 1.0, 9.0, 7.0, 8.0, 

末尾插入新元素: 
10.0, 6.0, 2.0, 4.0, 3.0, 66.0, 5.0, 1.0, 9.0, 7.0, 8.0, 88.0, 

删除中间的部分: 
10.0, 6.0, 2.0, 1.0, 9.0, 7.0, 8.0, 88.0, 

截取中间的部分: 
2.0, 1.0, 9.0, 7.0, 8.0, 

扩展数组: 
2.0, 1.0, 9.0, 7.0, 8.0, 0.0, 0.0, 

数组转置: 
0.0, 0.0, 8.0, 7.0, 9.0, 1.0, 2.0, 

数组正序排列: 
0.0, 0.0, 1.0, 2.0, 7.0, 8.0, 9.0, 

数组逆序排列: 
9.0, 8.0, 7.0, 2.0, 1.0, 0.0, 0.0, 

 

Published an original article · won praise 0 · Views 55

Guess you like

Origin blog.csdn.net/qq_38136906/article/details/104401435