大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學習攻略 Java學習 快速學習指南:Java中的堆棧

快速學習指南:Java中的堆棧

更新時間:2022-04-02 11:19:59 來源:動力節點 瀏覽2144次

1.概述

在這篇快速文章中,我們將介紹java.util.Stack類并開始研究如何使用它。

堆棧是一種通用數據結構,它表示允許在恒定時間內推送/彈出元素的對象的 LIFO(后進先出)集合。

對于新的實現,我們應該支持Deque接口及其實現。 雙端隊列 定義了一組更完整和一致的 LIFO 操作。但是,我們可能仍然需要處理 Stack類,尤其是在遺留代碼中,所以理解它很重要。

2.創建堆棧

讓我們首先使用默認的無參數構造函數創建一個空的Stack實例:

@Test
public void whenStackIsCreated_thenItHasSizeZero() {
    Stack<Integer> intStack = new Stack<>();    
    assertEquals(0, intStack.size());
}

這將創建一個默認容量為 10的Stack 。如果添加的元素數量超過Stack總大小,它將自動加倍。但是,它的大小在刪除元素后永遠不會縮小。

3.堆棧同步

Stack是Vector的直接子類;這意味著與它的超類類似,它是一個同步的實現。

但是,并不總是需要同步,在這種情況下,建議使用ArrayDeque。

4.加入堆棧

讓我們首先使用push()方法將一個元素添加到Stack的頂部- 該方法還返回添加的元素:

@Test
public void whenElementIsPushed_thenStackSizeIsIncreased() {
    Stack<Integer> intStack = new Stack<>();
    intStack.push(1);    
    assertEquals(1, intStack.size());
}

使用push()方法與使用addElement()的效果相同。唯一的區別是addElement ()返回操作的結果,而不是添加的元素。

我們還可以一次添加多個元素:

@Test
public void whenMultipleElementsArePushed_thenStackSizeIsIncreased() {
    Stack<Integer> intStack = new Stack<>();
    List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);    
    boolean result = intStack.addAll(intList);    
    assertTrue(result);
    assertEquals(7, intList.size());
}

5.從堆棧中檢索

接下來,讓我們看看如何獲??取和刪除Stack中的最后一個元素:

@Test
public void whenElementIsPoppedFromStack_thenElementIsRemovedAndSizeChanges() {
    Stack<Integer> intStack = new Stack<>();
    intStack.push(5);
    Integer element = intStack.pop();    
    assertEquals(Integer.valueOf(5), element);
    assertTrue(intStack.isEmpty());
}

我們也可以在不移除S大頭釘的情況下獲得最后一個元素:

@Test
public void whenElementIsPeeked_thenElementIsNotRemovedAndSizeDoesNotChange() {
    Stack<Integer> intStack = new Stack<>();
    intStack.push(5);
    Integer element = intStack.peek();
    assertEquals(Integer.valueOf(5), element);
    assertEquals(1, intStack.search(5));
    assertEquals(1, intStack.size());
}

6.在堆棧中搜索元素

(1)搜索

Stack允許我們搜索一個元素 并獲取它與頂部的距離:

@Test
public void whenElementIsOnStack_thenSearchReturnsItsDistanceFromTheTop() {
    Stack<Integer> intStack = new Stack<>();
    intStack.push(5);
    intStack.push(8);
    assertEquals(2, intStack.search(5));
}

結果是給定對象的索引。如果存在多個元素,則返回最接近頂部的元素的索引 。位于堆棧頂部的項目被視為位于位置 1。

如果找不到對象,search()將返回 -1。

(2)獲取元素索引

要獲取 S大頭釘上元素的索引,我們還可以使用indexOf()和lastIndexOf()方法:

@Test
public void whenElementIsOnStack_thenIndexOfReturnsItsIndex() {
    Stack<Integer> intStack = new Stack<>();
    intStack.push(5);    
    int indexOf = intStack.indexOf(5);    
    assertEquals(0, indexOf);
}

lastIndexOf()將始終找到最接近堆棧頂部的元素的索引。這與search()的工作方式非常相似——重要的區別是它返回索引,而不是與頂部的距離:

@Test
public void whenMultipleElementsAreOnStack_thenIndexOfReturnsLastElementIndex() {
    Stack<Integer> intStack = new Stack<>();
    intStack.push(5);
    intStack.push(5);
    intStack.push(5);    
    int lastIndexOf = intStack.lastIndexOf(5);    
    assertEquals(2, lastIndexOf);
}

7.從堆棧中刪除元素

除了用于刪除和檢索元素的pop()操作之外,我們還可以使用從Vector類繼承的多個操作來刪除元素。

(1)刪除指定元素

我們可以使用removeElement()方法刪除給定元素的第一次出現:

@Test
public void whenRemoveElementIsInvoked_thenElementIsRemoved() {
    Stack<Integer> intStack = new Stack<>();
    intStack.push(5);
    intStack.push(5);
    intStack.removeElement(5);    
    assertEquals(1, intStack.size());
}

我們還可以使用removeElementAt()來刪除Stack中指定索引下的元素:

    @Test
    public void whenRemoveElementAtIsInvoked_thenElementIsRemoved() {
        Stack<Integer> intStack = new Stack<>();
        intStack.push(5);
        intStack.push(7);        
        intStack.removeElementAt(1);        
        assertEquals(-1, intStack.search(7));
    }

(2)刪除多個元素

讓我們快速看一下如何使用removeAll() API 從Stack中刪除多個元素——它將Collection作為參數并從Stack中刪除所有匹配的元素:

@Test
public void givenElementsOnStack_whenRemoveAllIsInvoked_thenAllElementsFromCollectionAreRemoved() {
    Stack<Integer> intStack = new Stack<>();
    List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
    intStack.addAll(intList);
    intStack.add(500);
    intStack.removeAll(intList);
    assertEquals(1, intStack.size());
    assertEquals(1, intStack.search(500));
}

也可以使用clear()或removeAllElements()方法從堆棧中刪除所有元素;這兩種方法的工作原理相同:

@Test
public void whenRemoveAllElementsIsInvoked_thenAllElementsAreRemoved() {
    Stack<Integer> intStack = new Stack<>();
    intStack.push(5);
    intStack.push(7);
    intStack.removeAllElements();
    assertTrue(intStack.isEmpty());
}

(3)使用過濾器刪除元素

我們還可以使用條件從堆棧中刪除元素。讓我們看看如何使用removeIf ()執行此操作,并使用過濾器表達式作為參數:

@Test
public void whenRemoveIfIsInvoked_thenAllElementsSatysfyingConditionAreRemoved() {
    Stack<Integer> intStack = new Stack<>();
    List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
    intStack.addAll(intList);    
    intStack.removeIf(element -> element < 6);    
    assertEquals(2, intStack.size());
}

8.迭代堆棧

Stack允許我們同時使用Iterator和ListIterator。主要區別在于第一個允許我們在一個方向上遍歷Stack,第二個允許我們在兩個方向上執行此操作:

@Test
public void whenAnotherStackCreatedWhileTraversingStack_thenStacksAreEqual() {
    Stack<Integer> intStack = new Stack<>();
    List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
    intStack.addAll(intList);    
    ListIterator<Integer> it = intStack.listIterator();    
    Stack<Integer> result = new Stack<>();
    while(it.hasNext()) {
        result.push(it.next());
    }
    assertThat(result, equalTo(intStack));
}

Stack返回的所有迭代器都是快速失敗的。

9.Java 堆棧的 Stream API

Stack是一個集合,這意味著我們可以將它與 Java 8 Streams API 一起使用。將Stream與Stack一起使用類似于將其與任何其他Collection集合一起使用:

@Test
public void whenStackIsFiltered_allElementsNotSatisfyingFilterConditionAreDiscarded() {
    Stack<Integer> intStack = new Stack<>();
    List<Integer> inputIntList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 9, 10);
    intStack.addAll(inputIntList);
    List<Integer> filtered = intStack
      .stream()
      .filter(element -> element <= 3)
      .collect(Collectors.toList());
    assertEquals(3, filtered.size());
}

 

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 久久激情综合网 | 97se亚洲综合在线 | 国产特黄一级毛片特黄 | 国产羞羞事1000部在线观看 | 国产综合亚洲欧美日韩一区二区 | 欧美成人一区二区三区不卡视频 | 国内精品免费 | 国产精品国产欧美综合一区 | 久久久亚洲欧洲日产国码二区 | 大杳蕉伊人狼人久久一本线 | 亚洲欧洲国产经精品香蕉网 | 久久伊人草 | 国产精品一久久香蕉产线看 | 老司机成人午夜精品福利视频 | 成人小视频免费观看 | 欧美 日产 国产精选 | 国产一区二区三区在线免费观看 | 亚洲美女在线观看播放 | 中文字幕在线观看 | 97久久免费视频 | 五月婷在线 | 一区二区视频在线观看免费的 | 91在线精品视频 | 国产桃花视频 | 午夜欧美激情 | 免费一级片在线 | 色八戒国产一区二区三区四区 | 台湾一级毛片免费播放 | 国产欧美日韩精品专区 | 亚洲精品国产高清不卡在线 | 极品吹潮视频大喷潮tv | 午夜一级毛片免费视频 | 天天干天天操天天射 | 精品国产日韩亚洲一区91 | 亚欧洲精品在线视频免费观看 | 欧美精品久久久久久久久大尺度 | 青青青国产观看免费视频 | 亚洲精品在线免费 | 国产亚洲欧美在在线人成 | 久久午夜影院 | 日韩三级一区二区 |