هيكلة البيانات في كومة Java Collection Framework 59- Stack

في هذا الفيديو من جافا JAVA, سنتحدث عن كيفية هيكلة البيانات في كومة stack, و التي ترتكز حول مبدأ LIFO last in first out , اي سنشرح مبدا اخر من يدخل اول من يخرج.


package package3;

import java.util.Stack;

import package1.Rectangle;

public class Apple{

    public static void main(String... args){
        
        /*
         * public E push(E item)
         * public synchronized E pop()
         * public synchronized E peek()
         * public synchronized int search(Object o)
         */
        
        Stack<Rectangle> list = new Stack<>();
        Rectangle rect1= new Rectangle("Yellow", 8, 9);
        Rectangle rect2= new Rectangle("White", 10, 11);

        // Ajouter des éléments à la pile
        list.push(new Rectangle("Red", 4, 5));
      list.push(new Rectangle("Orange", 6, 7));
      list.push(rect1);
      list.push(rect2);
      System.out.println("stack : "+list);

        // Supprimer l'élément de la pile
      System.out.println("Pop : "+list.pop());

        // Accéder à l'élément depuis le haut
        System.out.println("Peek : "+list.peek());  
      
        // Chercher un élément
        System.out.println("stack index : "+list.search(rect1));
        System.out.println("list index : "+list.indexOf(rect1));

    }
}

------------------------------------
stack : [coleur de rectangle est Red et la surface 20.0, coleur de rectangle est Orange et la surface 42.0, coleur de rectangle est Yellow et la surface 72.0, coleur de rectangle est White et la surface 110.0]
Pop : coleur de rectangle est White et la surface 110.0
Peek : coleur de rectangle est Yellow et la surface 72.0
stack index : 1
list index : 2