顺序表java实现代码

package com.List;

public class SqList {
	private static final int MaxLength = 200;   //可能出现的最大值
	public Integer length = 0;   //存放的实际长度
	public Book[] arr ;    //存放方式
	
	public boolean InitList(SqList L){   //初始化
		L.arr = new Book[MaxLength];
		if((L.arr)==null){
			L.length = 0;
		}
		return true;
	}
	public Book getBook(SqList L ,int index ){    //得到参数
		if(index<1||index>L.length){
			return null;
		}
		Book book = L.arr[index-1] ;
		return book;
	}
	
	public Integer LocateBookIndex(SqList L ,Book book){    //得到位置
		for(int i=0;i<L.length;i++){
			if(book.equals(L.arr[i])){
				return i+1;
			}
		}
		return null;
	}
	public boolean insertList(SqList L , int index ,Book book){    //插入
		if(index <1 ||index >L.length+1){
			return false ;
		}
		if(L.length==MaxLength){
			return false;
		}
		for(int j = L.length-1 ; j>index-1 ;j--){
			L.arr[j+1] = L.arr[j];
		}
		L.arr[index] = book;
		L.arr[index -1 ] = book;
		++L.length;
		return true; 
	}
	
	public static void main(String[] args) {
		SqList list = new SqList();
		list.InitList(list);
		list.insertList(list, 1, new Book("小米", "11", 11));
		Integer  index  =list.LocateBookIndex(list, new Book("小米", "11", 11));
		System.out.println(index);
		Book book = list.getBook(list, 1);
		
	}
}
class Book{
	public String name;
	public String number ;
	public double price ;
	public Book(String name, String number, double price) {
		this.name = name;
		this.number = number;
		this.price = price;
	}
	
	public boolean equals(Book book) {
		if(this == book){
			return true;
		}else{
			if(this.name.equals(book.name) && this.number.equals(book.number) && this.price==book.price){
				return true ;
			}
		}
		return false;
	}

	@Override
	public String toString() {
		return "Book [name=" + name + ", number=" + number + ", price=" + price
				+ "]";
	}
	
}

 

全部评论

相关推荐

投递阿里巴巴控股集团等公司7个岗位 >
点赞 评论 收藏
转发
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务