Insert deletion of linear table (Java implementation)

Source: Internet
Author: User
public interface Listmethod {int size ();
    Determine if the linear table is an empty Boolean isempty ();
    Insert element void Insert (int i, Object obj) throws Exception;
    Deletes the element void Delete (int i) throws Exception;
Gets the element Object get (int i) throws Exception at the specified location; }
public class List implements listmethod{//default maximum length is 10;
	public static final int defoultsize=10;
	Linear table Maximum length int maxSize;
	The current degree of the linear table list int length;
		Object array object[] listarray;//object array public List (int length) {maxsize=length;
		this.length=0;
	Listarray=new Object[length];
		///Determine if the linear table is null public boolean IsEmpty () {if (length==0) {true;
	return false;
	@Override public int size () {return length; @Override public void Delete (int i) throws Exception {//delete elements of the linear Table I (IsEmpty ()) {throw new Exception ("This table is empty and cannot be
	Delete "); } if (i<0| |
	I>length-1) {throw new Exception ("The table deletion is out of bounds, cannot be deleted");
	for (int k=i;k<=length-1;k++) {listarray[k-1]=listarray[k];	
	} length--; Public Object get (int i) throws Exception {if i<0| |
		I>length-1) {throw new Exception ("parameter has error");
	return listarray[i]; @Override public void Insert (int i, Object obj) throws Exception {if (length==maxsize) {throw new Exception ("The location is full
		, don't plug it in "); //If you insert a position smaller than 0, or the position you inserted is greater than the current maximum length if (i<0| |
		i>maxsize-1) {throw new Exception ("parameter has error");
		for (int k=length-1;k>=i;k--) {listarray[k+1]=listarray[k];
		} listarray[i] = obj;	
	length++;
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.