JAVA学习(小白向)—顺序表2—2021.5.31

本文介绍了Java顺序表的基本操作,包括构造空表、使用数组初始化、打印列表、定位元素、插入和删除值。通过实例演示了如何实现列表的创建、查找、插入和删除功能,并展示了运行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JAVA学习(小白向)—顺序表2—2021.5.31
学习顺序表的基础操作
代码:

package a11;

/**
 * 
 * @author hengyuzuo
 *
 */
public class datastructure {

	/**
	 * ******************* 
	 * The maximal length of the list. It's a constant.
	 * 
	 * *******************
	 */
	public static final int MAX_LENGTH = 10;
	/**
	 * *******************
	 * The actual length not exceeding MAX_LENTH.Attention:lenth is not only
	 * the member variable of Sequential list, but also the member variable of
	 * Array. In fact, a name can be the member variable of different classes.
	 * *******************
	 */
	int length;
	/**
	 * *******************
	 * The data stored in an array.
	 * *******************
	 */
	int[] data;
	/**
	 * *******************
	 * Construct an empty sequential list.
	 * *******************
	 */
	public datastructure() {
		length = 0;
		data = new int[MAX_LENGTH];
	}// Of the first constructor
	
	/**
	 * *******************
	 * Construct a sequential list using an array.
	 * @param paraArray
	 * The given array. Its' length should not exceed MAX_LENGTH. 
	 * For simplicity now we do not check it.
	 * *******************
	 */
	public datastructure(int[] paraArray) {
		data = new int[MAX_LENGTH];
		length = paraArray.length;	
		
		// Copy data
		for (int i = 0; i < paraArray.length; i++) {
			data[i] = paraArray[i];
		}// Of for i 
	}// Of the second constructor
	
	/**
	 * ********************
	 * Overrides the method claimed in object, the superclass of any class.
	 * ********************
	 */
	public String toString() {
		String resultString = "";
		if (length == 0) {
			return "empty";
		}// Of if
		
		for (int i = 0; i < length -1;i++) {
			resultString += data[i] + ",";		
		}// Of for i
		
		resultString += data[length - 1];
		return resultString;
	}// Of toString
	
	/**
	 * ********************
	 * Reset to empty.
	 * ********************
	 */
	public void reset() {
		length = 0;
	}// Of reset
	
	/**
	 * ********************
	 * The entrance of the program.
	 * @param args not used now.
	 * ********************
	 */
	public static void main1(String args[]) {
		int[] tempArray = {1, 4, 5, 9};
		datastructure tempFirstList = new datastructure(tempArray);
		System.out.println("Initialized, the list is: " + tempFirstList.toString());
		System.out.println("Again, the list is: " + tempFirstList);
		
		tempFirstList.reset();
		System.out.println("After reset, the list is: " + tempFirstList);
	}
		
   /**
    ***********************
    * Locate the given value. If it appears in multiple positions, simply
    * return the first one.
    * @param paraValue
    * @return The position. -1 for not found.
    ***********************
    */
   public int locate(int paraValue) {
   	int tempPosition = -1;

   	for (int i = 0; i < length; i++) {
   		if (data[i] == paraValue) {
   			tempPosition = i;
   			break;
   		} // Of if
   	} // Of for i

   	return tempPosition;
   }// Of locate

   /**
    ***********************
    * Insert a value to a position. If the list is already full, do nothing.
    * @param paraPosition
    * @param paraValue
    * @return Success or not.
    ***********************
    */
   public boolean insert(int paraPosition, int paraValue) {
   	if (length == MAX_LENGTH) {
   		System.out.println("List full.");
   		return false;
   	}//Of if
   	
   	if ((paraPosition < 0) || (paraPosition > length)) {
   		System.out.println("The position " + paraPosition + " is out of bounds.");
   		return false;
   	} // Of if

   	// From tail to head.
   	for (int i = length; i > paraPosition; i--) {
   		data[i] = data[i - 1];
   	} // Of for

   	data[paraPosition] = paraValue;
   	length++;

   	return true;
   }// Of insert

   /**
    ***********************
    * Delete a value at a position.
    * @param paraPosition
    * @return Success or not.
    ***********************
    */
   public boolean delete(int paraPosition) {
   	if ((paraPosition < 0) || (paraPosition >= length)) {
   		System.out.println("The position " + paraPosition + " is out of bounds.");
   		return false;
   	} // Of if

   	// From head to tail.
   	for (int i = paraPosition; i < length - 1; i++) {
   		data[i] = data[i + 1];
   	} // Of for

   	length--;

   	return true;
   }// Of insert

   /**
    ************************
    * The entrance of the program.
    * @param args not used now.
    ************************
    */
   public static void main(String args[]) {
   	int[] tempArray = { 1, 4, 6, 9 };
   	datastructure tempFirstList = new datastructure(tempArray);
   	System.out.println("Initialized, the list is: " + tempFirstList.toString());
   	System.out.println("Again, the list is: " + tempFirstList);

   	int tempValue = 4;
   	int tempPosition = tempFirstList.locate(tempValue);
   	System.out.println("The position of " + tempValue + " is " + tempPosition);

   	tempValue = 5;
   	tempPosition = tempFirstList.locate(tempValue);
   	System.out.println("The position of " + tempValue + " is " + tempPosition);

   	tempPosition = 2;
   	tempValue = 5;
   	tempFirstList.insert(tempPosition, tempValue);
   	System.out.println("After inserting " + tempValue + " to position " + tempPosition + ", the list is: " + tempFirstList);

   	tempPosition = 8;
   	tempValue = 10;
   	tempFirstList.insert(tempPosition, tempValue);
   	System.out.println("After inserting " + tempValue + " to position " + tempPosition + ", the list is: " + tempFirstList);

   	tempPosition = 3;
   	tempFirstList.delete(tempPosition);
   	System.out.println("After deleting data at position " + tempPosition + ", the list is: " + tempFirstList);

   	for(int i = 0; i < 8; i ++) {
   		tempFirstList.insert(i, i);
   		System.out.println("After inserting " + i + " to position " + i + ", the list is: " + tempFirstList);
   	}//Of for i
   	
   	tempFirstList.reset();
   	System.out.println("After reset, the list is: " + tempFirstList);
   }// Of main
}// Of class datastructure

运行结果:

Initialized, the list is: 1,4,6,9
Again, the list is: 1,4,6,9
The position of 4 is 1
The position of 5 is -1
After inserting 5 to position 2, the list is: 1,4,5,6,9
The position 8 is out of bounds.
After inserting 10 to position 8, the list is: 1,4,5,6,9
After deleting data at position 3, the list is: 1,4,5,9
After inserting 0 to position 0, the list is: 0,1,4,5,9
After inserting 1 to position 1, the list is: 0,1,1,4,5,9
After inserting 2 to position 2, the list is: 0,1,2,1,4,5,9
After inserting 3 to position 3, the list is: 0,1,2,3,1,4,5,9
After inserting 4 to position 4, the list is: 0,1,2,3,4,1,4,5,9
After inserting 5 to position 5, the list is: 0,1,2,3,4,5,1,4,5,9
List full.
After inserting 6 to position 6, the list is: 0,1,2,3,4,5,1,4,5,9
List full.
After inserting 7 to position 7, the list is: 0,1,2,3,4,5,1,4,5,9
After reset, the list is: empty
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值