English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
في هذا الدرس، سنتعلم عن طريق الأمثلة كيفية استخدام كلاس Stack في Java وما هي طرقه.
يحتوي إطار العمل الخاص بـ Java على كلاس يُدعى Stack، يقدم وظائف بنية البيانات Stack.
تعاقب هذه الكلاس Stack من كلاس Vector.
في الـ Stack، يتم تخزين العناصر بـآخر يدخل أولاً يخرجطريقة التخزين والوصول. بمعنى آخر، يتم إضافة العناصر إلى قمة الـ Stack وإزالتها من قمة الـ Stack.
لإنشاء الـ Stack، يجب علينا أولاً استيراد حزمة java.util.Stack. بعد استيراد الحزمة، يمكننا استخدام Java لإنشاء Stack.
Stack<Type> stacks = new Stack<>();
Here, Type indicates the type of the stack. For example,
//Create a stack of integer type Stack<Integer> stacks = new Stack<>(); //Create a stack of string type Stack<String> stacks = new Stack<>();
Since Stack inherits the Vector class, it inherits all methods of Vector. To learn about the different Vector methods, please visitJava Vector Class.
In addition to these methods, the Stack class includes 5 methods that are distinct from Vector.
To add an element to the top of the stack, we use the push() method. For example,
import java.util.Stack; class Main { public static void main(String[] args) { Stack<String> animals = new Stack<>(); //إضافة العنصر إلى Stack animals.push("Dog"); animals.push("Horse"); animals.push("Cat"); System.out.println("Stack: " + animals); } }
نتيجة الإخراج
Stack: [Dog, Horse, Cat]
To delete an element from the top of the stack, we use the pop() method. For example,
import java.util.Stack; class Main { public static void main(String[] args) { Stack<String> animals = new Stack<>(); //Add element to Stack animals.push("Dog"); animals.push("Horse"); animals.push("Cat"); System.out.println("Initial stack: " + animals); //Delete stack element, following the Last In, First Out (LIFO) principle String element = animals.pop(); System.out.println("Delete element: " + element); } }
نتيجة الإخراج
Initial stack: [Dog, Horse, Cat] Delete element: Cat
The peek() method returns an object from the top of the stack. For example,
import java.util.Stack; class Main { public static void main(String[] args) { Stack<String> animals = new Stack<>(); //إضافة العنصر إلى Stack animals.push("Dog"); animals.push("Horse"); animals.push("Cat"); System.out.println("Stack: " + animals); //Access element from the top, following the Last In, First Out (LIFO) principle String element = animals.peek(); System.out.println("Top element: " + element); } }
نتيجة الإخراج
Stack: [Dog, Horse, Cat] Top element: Cat
To search for an element in the stack, we use the search() method. It returns the position of the element from the top of the stack. For example,
import java.util.Stack; class Main { public static void main(String[] args) { Stack<String> animals = new Stack<>(); //إضافة العنصر إلى Stack animals.push("Dog"); animals.push("Horse"); animals.push("Cat"); System.out.println("Stack: " + animals); //Search for element int position = animals.search("Horse"); System.out.println("Location of element Horse: " + position); } }
نتيجة الإخراج
Stack: [Dog, Horse, Cat] Location of element Horse: 2
للتحقق من whether the stack is empty, we use the empty() method. For example,
import java.util.Stack; class Main { public static void main(String[] args) { Stack<String> animals = new Stack<>(); //إضافة العنصر إلى Stack animals.push("Dog"); animals.push("Horse"); animals.push("Cat"); System.out.println("Stack: " + animals); //تحقق من أن هيكل البيانات دالة فارغ boolean result = animals.empty(); System.out.println("هل هيكل البيانات دالة فارغ؟ " + result); } }
نتيجة الإخراج
Stack: [Dog, Horse, Cat] هل هيكل البيانات دالة فارغ؟ لا
يقدم هذا الكلاس Stack بنية البيانات لتنفيذ هيكل البيانات دالة. ولكن، يُنصح بعدم استخدامها. بل استخدم فئة ArrayDeque (تحقق من واجهة Deque) لتنفيذ هيكل البيانات دالة في Java.
للحصول على معلومات إضافية، يرجى زيارة:ArrayDeque الخاصة بـ Java