Java-day12

day12;
顺序表(二)
顺序表的查找、插入、删除
总结:
1、在插入的时候,需要进行对表满值判断,若已满或者位置越界,就返回false。
2、在删除的时候需要判断位置是否合法。

代码:

package day12;

public class SequentialList {

	public static final int MAX_LENGTH =10;
	int length;
	int []data;
	
	//无参方法需要保持方法名和类名的一致性;
	public SequentialList() 
	{
		length =0;
		data= new int [MAX_LENGTH];
	}
	//方法重载,方法名称与类名称保持一致;
	public SequentialList (int [] paraArray)
	{
		data = new int [MAX_LENGTH];
		length =paraArray.length;
		
		for (int i=0;i<paraArray.length;i++)
		{
			data[i]=paraArray[i];
		}
	}
	
	//由data[]的数字数组变成 resultString[]的字符串;
	public String toString() 
	{
		String resultString ="";
		
		if(length == 0 )
		{
			return "empty";
		}
		
		for (int i=0;i<length - 1;i++)
		{
			resultString += data[i]+",";
		}
		resultString += data [length - 1];
		
		return resultString;
	}
	//重置为空
	public void reset() {
		length = 0;
	}
	//查找
	public int indexOf(int paraValue)
	{
		int tempPosition = -1;
		
		for (int i=0;i<length;i++)
		{
			if(data[i] == paraValue)
			{
				tempPosition = i;
				break;
			}
		}
		
		return tempPosition;
	}
	
	//插入
	public boolean insert(int paraPosition, int paraValue)
	{
		if(length == MAX_LENGTH)
		{
			System.out.println("List full.");
			return false;
		}
		
		if((paraPosition < 0)||(paraPosition > length))
		{
			System.out.println("The position "+paraPosition +"is out of bounds.");
			return false;
		}
		
		for (int i =length;i>paraPosition;i--)
		{
			data[i] =data[i -1];
		}
		
		data[paraPosition] = paraValue;
		length++;
		
		return true;
	}
	//删除
	public boolean delete (int paraPosition)
	{
		if((paraPosition <0 )||(paraPosition >= length))
		{
			System.out.println("The position" + paraPosition + "is out of bounds.");
			return false;
		}
		
		for (int i=paraPosition;i<length-1;i++)
		{
			data[i]=data[i+1];
		}
		
		length--;
		return true;
	}

	
	public static void main (String args[])
	{
		int []tempArray = {1,4,6,9};
		SequentialList tempFirstList = new SequentialList (tempArray);
		
		System.out.println("Initialized, the list is: " + tempFirstList.toString());
		System.out.println("Again, the list is: " + tempFirstList);
		
		int tempValue = 4;
	   	int tempPosition = tempFirstList.indexOf(tempValue);
	   	System.out.println("The position of " + tempValue + " is " + tempPosition);
	   	
        tempValue = 5;
        tempPosition = tempFirstList.indexOf(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);

	}

}

结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值