• Java实验项目三——采用面向对象的方式设计线性表


    Program:

    采用面向对象的方法设计一个线性表,目的是为其他需要用线性表的应用提供线性表对象。

    1、list包中,定义线性表类

      1 /*
      2  * Description:定义线性表类
      3  * 
      4  * Written By:Cai
      5  * 
      6  * Date Written:2017-10-09
      7  * 
      8  * */
      9 
     10 package list;
     11 
     12 public class List {
     13 
     14     private Object[] list;        //声明Object类型的数组
     15     private int foot;            //list数组最后一个元素的下标
     16     private static final int MAXSIZE = 10;        //list的最大长度
     17     
     18     public List() {                //定义无参构造方法,并初始化list数组
     19         this.list = new Object[MAXSIZE];
     20         this.foot = -1;
     21     }
     22     
     23     //返回线性表的长度
     24     public int length() { 
     25         
     26         return this.foot + 1;
     27     }
     28     
     29     //判断当前对象数组是否为空
     30     public boolean isEmpty() {
     31         
     32         if( this.foot == -1 ) {        //线性表为空
     33             
     34             return true;
     35         }else {
     36             
     37             return false;
     38         }
     39     }
     40     
     41     //判断当前对象数组是否已满
     42     public boolean isFull() {
     43         
     44         if( this.foot == MAXSIZE - 1 ) {        //线性表已满
     45             
     46             return true;
     47         }else {
     48             
     49             return false;
     50         }
     51     }
     52     
     53     //向线性表添加数据
     54     public boolean addEle(Object element) {
     55         
     56         if( !this.isFull() ) {
     57             
     58             this.foot++;        //当前下标加1
     59             this.list[this.foot] = element;
     60             
     61             return true;        //添加成功
     62         }else {
     63             
     64             return false;        //添加失败
     65         }
     66     }
     67     
     68     //根据元素值对数组进行搜索,返回目标元素的下标,如果不存在,返回-1
     69     public int searchEle(Object element) {
     70         
     71         for( int i = 0; i <= this.foot; i++ ) {
     72             
     73             if( this.list[i] == element ) {
     74                 
     75                 return i;    //存在,返回对应下标
     76             }
     77         }
     78         
     79         return -1;            //不存在,返回-1,
     80     }
     81     
     82     //根据指定元素位置删除线性表中的元素
     83     public boolean deleteEleByIndex(int index) {
     84         
     85         if( !this.isEmpty() ) {            //线性表不为空
     86             
     87             for( int i = index; i < this.foot; i++ ) {        //将沿删除元素的后面元素一次前移
     88                 
     89                 this.list[i] = this.list[i+1];        
     90             }
     91             this.list[this.foot] = null;        //最后一个元素置为null
     92             this.foot--;
     93             return true;
     94         }else {
     95             
     96             return false;
     97         }
     98     }
     99     
    100 
    101     //根据元素值之删除线性表中的目标元素
    102     public boolean deleteEleByValue(Object element) {
    103         
    104         int index = -1;
    105         boolean flag = false;        //用来标记是否删除成功
    106         
    107         if( !this.isEmpty() ) {        //线性表不为空
    108             
    109             index = this.searchEle(element);        //根据元素值搜索元素下标
    110             if( index != -1 ) {                        //元素存在
    111                 
    112                 flag =  this.deleteEleByIndex(index);    //根据元素下标删除元素,并返回相应的结果
    113             }else {
    114                 
    115                 flag = false;
    116             }
    117             
    118         }
    119         
    120         return flag;        //返回删除结果
    121     }
    122         
    123     //打印线性表元素
    124     public void displayList() {
    125         
    126         for( int i = 0; i <= this.foot; i++ ) {
    127             
    128             System.out.println( this.list[i] );
    129         }
    130     }
    131     
    132     
    133     
    134     
    135 }

    2、在main包中,定义TestDemo类,测试线性表

     1 /*
     2  * Description:定义测试类,测试线性表
     3  * 
     4  * Written By:Cai
     5  * 
     6  * Date Written:2017-10-09
     7  * 
     8  * */
     9 
    10 package main;
    11 
    12 
    13 import list.List;
    14 
    15 public class TestDemo {
    16     
    17     public static void main(String args[]) {
    18         
    19         List list = new List();        //实例化List类对象
    20         
    21         list.addEle("Hello");        //向线性表中添加元素
    22         list.addEle("world");
    23         list.addEle("Good");
    24         list.addEle("morning");
    25         
    26         //打印线性表的长度
    27         System.out.println( "list的长度为:" + list.length() );
    28         
    29         //打印线性表的内容
    30         System.out.println( "
    list的内容为:" );
    31         list.displayList();
    32         
    33         //搜索world是否存在
    34         System.out.println( "
    world元素是否存在,不存在返回打印-1,存在打印下标:" + list.searchEle("world") );
    35         System.out.println( "
    Apple元素是否存在,不存在返回打印-1,存在打印下标:" + list.searchEle("Apple") );
    36         
    37         //删除world元素,并重新打印线性表
    38         System.out.println( "
    删除world元素,删除成功返回true,失败返回false:" + list.deleteEleByValue("world") );
    39         System.out.println( "
    删除"world"后list的内容为:" );
    40         list.displayList();
    41         
    42         //删除下标为1的元素,此处应该删除的Good
    43         System.out.println( "
    删除下标为1,删除成功返回true,失败返回false:" + list.deleteEleByIndex(1) );
    44         System.out.println( "
    删除下标为1的元素后list的内容为:" );
    45         list.displayList();
    46         
    47     }
    48     
    49 }
  • 相关阅读:
    【Linux基础】linux下修改ls显示的时间格式
    【Teradata】gtwglobal查看
    【Teradata】tdlocaledef修改默认日期配置
    【Linux基础】文件处理实例
    【Linux基础】awk命令
    【teradata】强制解锁
    第1节:保存文档
    Centos7安装MySQL数据库
    MyBatis框架之异常处理
    spring事务源码分析
  • 原文地址:https://www.cnblogs.com/caizhen/p/7674563.html
Copyright © 2020-2023  润新知