欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實(shí)現(xiàn)順序結(jié)構(gòu)線性列表的函數(shù)代碼

 更新時間:2013年10月24日 10:09:44   作者:  
java實(shí)現(xiàn)順序結(jié)構(gòu)線性列表的函數(shù)代碼。需要的朋友可以過來參考下,希望對大家有所幫助

廢話不多說,直接上代碼

復(fù)制代碼 代碼如下:

package com.ncu.list;

/**
 *
 * 順序結(jié)構(gòu)線性列表
 * 
 *
 */
public class SquenceList<T> {
    private int size; // 線性表的長度
    private Object[] listArray;
    private int currenSize = 0; // 當(dāng)前線性表中的數(shù)據(jù)

    public SquenceList() {

    }

    public SquenceList(int size) {
        this.size = size;
        listArray = new Object[size];
    }

    public void arrayCopy(int index) {
        Object newArray[] = new Object[size];
        for (int i = 0; i < currenSize; i++) {
            if (i >= index) {
                newArray[i] = listArray[i + 1];
            } else {
                newArray[i] = listArray[i];
            }
        }
        listArray = newArray;
        newArray = null; // 釋放資源
    }

    /**
     * 根據(jù)索引位置移除元素
     *
     * @param index
     */
    public void remove(int index) {
        index = index - 1;
        if (index < 0 || index > currenSize) {
            System.out.println("線性表索引越界");
        }
        if (currenSize == 0) {
            System.out.println("線性表為空");
        } else {
            currenSize--;
            arrayCopy(index);
            if (currenSize == 0) {
                listArray = null;
            }
        }
    }

    /**
     * 根據(jù)元素內(nèi)容移除元素
     *
     * @param element
     */
    public void removeLocate(T element) {
        for (int i = 0; i < currenSize;) {
            if (element.equals(listArray[i])) {
                remove(i + 1);
            } else {
                i++;
            }
        }
    }

    /**
     * 從線性表尾段插入數(shù)據(jù)
     *
     * @param element
     */
    public void add(T element) {
        if (currenSize > size || currenSize < 0) {
            System.out.println("線性表索引越界");
        } else {
            listArray[currenSize] = element;
            currenSize++;
        }
    }

    private void insert(T element, int index) {
        index = index - 1;
        if (currenSize > size || currenSize < 0 || index < 0
                || index >= currenSize) {
            System.out.println("線性表索引越界");
        } else {
            Object newArray[] = new Object[size];
            for (int i = 0; i < currenSize; i++) {
                if (i >= index) {
                    newArray[index] = element;
                    newArray[i + 1] = listArray[i];
                } else {
                    newArray[i] = listArray[i];
                }

            }
            listArray = newArray;
            newArray = null;
            currenSize++;
        }
    }

    /**
     * 在指定索引位置插入數(shù)據(jù)
     *
     * @param element
     * @param index
     */
    public void add(T element, int index) {
        if (index == size) {
            add(element);
        } else {
            insert(element, index);
        }
    }

    /**
     * 刪除線性表最后一個元素
     */
    public void delete() {
        if (isEmpty()) {
            System.out.println("線性表為空,不能刪除");
        } else {
            listArray[currenSize - 1] = null;
            currenSize--;
        }
    }

    /**
     * 判讀線性表是否為空
     *
     * @return
     */
    public boolean isEmpty() {
        if (currenSize == 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根據(jù)索引找到相應(yīng)的元素
     *
     * @param index
     * @return
     */
    public T get(int index) {
        T obj = null;
        if (isEmpty() || index > currenSize || index < 0) {
            System.out.println("線性表為空,不能刪除");
        } else {
            obj = (T) listArray[index - 1];
        }

        return obj;
    }

    /**
     * 清空線性表
     */
    public void clear() {
        size = 0;
        currenSize = 0;
    }

    /**
     * 得到線性表當(dāng)前的元素的個數(shù)
     *
     * @return
     */
    public int size() {
        return currenSize;
    }

    public void showList() {
        if (currenSize > 0) {
            for (int i = 0; i < currenSize; i++) {
                System.out.println(listArray[i]);

            }
        } else {
            System.out.println("線性表為空");
        }

        System.out.println("------------");
    }

    public static void main(String[] args) {
        SquenceList<Integer> list = new SquenceList<Integer>(10);
    }
}

相關(guān)文章

  • MyBatis學(xué)習(xí)教程(六)-調(diào)用存儲過程

    MyBatis學(xué)習(xí)教程(六)-調(diào)用存儲過程

    這篇文章主要介紹了MyBatis學(xué)習(xí)教程(六)-調(diào)用存儲過程的相關(guān)資料,非常不錯,具有參考借鑒價值,感興趣的朋友一起看下吧
    2016-05-05
  • Java數(shù)據(jù)結(jié)構(gòu)之單鏈表詳解

    Java數(shù)據(jù)結(jié)構(gòu)之單鏈表詳解

    在之前的學(xué)習(xí)中,我們主要了解了很多 Java 的 基本語法,但是在之后的 Java學(xué)習(xí)中,了解基礎(chǔ)數(shù)據(jù)結(jié)構(gòu)的知識非常重要,數(shù)據(jù)結(jié)構(gòu)的思想可以幫助我們更加清晰明白的了解 Java 的解題思路等等.今天我們就來開始學(xué)習(xí)實(shí)現(xiàn)一個Java基礎(chǔ)的單鏈表,需要的朋友可以參考下
    2021-05-05
  • java加密MD5實(shí)現(xiàn)及密碼驗(yàn)證代碼實(shí)例

    java加密MD5實(shí)現(xiàn)及密碼驗(yàn)證代碼實(shí)例

    這篇文章主要介紹了java加密MD5實(shí)現(xiàn)及密碼驗(yàn)證代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • SpringMVC的Body參數(shù)攔截的問題

    SpringMVC的Body參數(shù)攔截的問題

    SpringMVC對出參和入?yún)⒂蟹浅S押玫耐卣怪С?方便你對數(shù)據(jù)的輸入和輸出有更大的執(zhí)行權(quán),我們?nèi)绾瓮ㄟ^SpringMVC定義的結(jié)果做一系列處理呢,需要的朋友可以參考下
    2018-06-06
  • IDEA利用自帶Axis工具和wsdl文件反向生成服務(wù)端客戶端代碼圖文詳解

    IDEA利用自帶Axis工具和wsdl文件反向生成服務(wù)端客戶端代碼圖文詳解

    這篇文章主要介紹了IDEA利用自帶Axis工具和wsdl文件反向生成服務(wù)端客戶端代碼詳細(xì)流程,在這里小編使用的是idea2021.1最新開發(fā)工具,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-05-05
  • ZooKeeper入門教程一簡介與核心概念

    ZooKeeper入門教程一簡介與核心概念

    本文是ZooKeeper入門系列教程,涵蓋ZooKeeper核心內(nèi)容,通過實(shí)例和大量圖表,結(jié)合實(shí)戰(zhàn),幫助學(xué)習(xí)者理解和運(yùn)用,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2022-01-01
  • Quarkus改造Pmml模型項目異常記錄及解決處理

    Quarkus改造Pmml模型項目異常記錄及解決處理

    這篇文章主要為大家介紹了Quarkus改造Pmml模型項目是遇到的異常記錄以及解決方法,有需要的同學(xué)可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-02-02
  • spring-security關(guān)于hasRole的坑及解決

    spring-security關(guān)于hasRole的坑及解決

    這篇文章主要介紹了spring-security關(guān)于hasRole的坑及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • JAVA8之函數(shù)式編程Function接口用法

    JAVA8之函數(shù)式編程Function接口用法

    這篇文章主要介紹了JAVA8之函數(shù)式編程Function接口用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • Java語言實(shí)現(xiàn)簡單FTP軟件 FTP本地文件管理模塊實(shí)現(xiàn)(9)

    Java語言實(shí)現(xiàn)簡單FTP軟件 FTP本地文件管理模塊實(shí)現(xiàn)(9)

    這篇文章主要為大家詳細(xì)介紹了Java語言實(shí)現(xiàn)簡單FTP軟件,F(xiàn)TP本地文件管理模塊的實(shí)現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04

最新評論