最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(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);
        }
    }

    /**
     * 刪除線性表最后一個(gè)元素
     */
    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)前的元素的個(gè)數(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)文章

  • 關(guān)于Arrays.sort()使用的注意事項(xiàng)

    關(guān)于Arrays.sort()使用的注意事項(xiàng)

    這篇文章主要介紹了關(guān)于Arrays.sort()使用的注意事項(xiàng),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • springboot反爬蟲組件kk-anti-reptile的使用方法

    springboot反爬蟲組件kk-anti-reptile的使用方法

    這篇文章主要介紹了springboot反爬蟲組件kk-anti-reptile的使用方法,幫助大家更好的利用spring boot反爬蟲,保護(hù)網(wǎng)站安全,感興趣的朋友可以了解下
    2021-01-01
  • Guava - 并行編程Futures詳解

    Guava - 并行編程Futures詳解

    這篇文章主要介紹了Guava - 并行編程Futures詳解方法的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • 一文帶你了解Java創(chuàng)建型設(shè)計(jì)模式之原型模式

    一文帶你了解Java創(chuàng)建型設(shè)計(jì)模式之原型模式

    原型模式其實(shí)就是從一個(gè)對象在創(chuàng)建另外一個(gè)可定制的對象,不需要知道任何創(chuàng)建的細(xì)節(jié)。本文就來通過示例為大家詳細(xì)聊聊原型模式,需要的可以參考一下
    2022-09-09
  • Java實(shí)現(xiàn)反轉(zhuǎn)一個(gè)鏈表的示例代碼

    Java實(shí)現(xiàn)反轉(zhuǎn)一個(gè)鏈表的示例代碼

    本文主要介紹了Java實(shí)現(xiàn)反轉(zhuǎn)一個(gè)鏈表的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • java控制臺實(shí)現(xiàn)學(xué)生管理系統(tǒng)

    java控制臺實(shí)現(xiàn)學(xué)生管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了java控制臺實(shí)現(xiàn)簡單的學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • jdbc結(jié)合dpcp連接池的封裝實(shí)例

    jdbc結(jié)合dpcp連接池的封裝實(shí)例

    下面小編就為大家?guī)硪黄猨dbc結(jié)合dpcp連接池的封裝實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10
  • springboot使用EMQX(MQTT協(xié)議)的實(shí)現(xiàn)

    springboot使用EMQX(MQTT協(xié)議)的實(shí)現(xiàn)

    最近由于iot越來越火, 物聯(lián)網(wǎng)的需求越來越多, 那么理所當(dāng)然的使用mqtt的場景也就越來越多,本文主要介紹了springboot使用EMQX(MQTT協(xié)議)的實(shí)現(xiàn),感興趣的可以了解一下
    2023-10-10
  • Java實(shí)戰(zhàn)之基于I/O流設(shè)計(jì)的圖書管理系統(tǒng)

    Java實(shí)戰(zhàn)之基于I/O流設(shè)計(jì)的圖書管理系統(tǒng)

    這篇文章主要介紹了Java實(shí)戰(zhàn)之基于I/O流設(shè)計(jì)的圖書館管理系統(tǒng),文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • Sentinel熱門詞匯限流的實(shí)現(xiàn)詳解

    Sentinel熱門詞匯限流的實(shí)現(xiàn)詳解

    這篇文章主要介紹了使用Sentinel對熱門詞匯進(jìn)行限流的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07

最新評論

贡山| 施甸县| 许昌县| 河西区| 墨竹工卡县| 泸西县| 盐源县| 巴东县| 买车| 清丰县| 保亭| 城口县| 阳泉市| 陇川县| 鸡西市| 遵义市| 侯马市| 望奎县| 彝良县| 永济市| 鱼台县| 本溪| 金门县| 阿图什市| 山东省| 宾阳县| 新宁县| 栖霞市| 孟津县| 青冈县| 绥阳县| 阿尔山市| 永嘉县| 利辛县| 峨山| 湖北省| 增城市| 涟水县| 社会| 洪洞县| 青岛市|