當前位置:首頁 » 編程語言 » java棧

java棧

發布時間: 2022-01-14 01:09:22

1. java中棧的應用

棧中存儲的是方法參數變數和方法體中的局部變數還有整形變數。。具體的應用你可以去看thinking in java。。。

2. java中什麼是棧啊

存放基本類型的變數數據和對象的引用,但對象本身不存放在棧中,而是存放在堆(new 出來的對象)或者常量池中(字元串常量對象存放在常量池中。)。

棧和常量池中的對象可以共享,對於堆中的對象不可以共享。棧中的數據大小和生命周期是可以確定的,當沒有引用指向數據時,這個數據就會消失。堆中的對象的由垃圾回收器負責回收,因此大小和生命周期不需要確定。

局部變數的數據存在於棧內存中。

棧的優勢是,存取速度比堆要快,僅次於寄存器,棧數據可以共享。但缺點是,存在棧中的數據大小與生存期必須是確定的,缺乏靈活性。棧中主要存放一些基本類型的變數數據(int, short, long, byte, float, double, boolean, char)和對象句柄(引用)。

3. java 創建棧問題

import java.util.Stack;

public class Test1 {
public static void main(String[] args){
Stack kk = new Stack();
kk.push(new Integer(11));//只能放入對象,int,double是不行的,只有先作成對象
kk.push(new Integer(12));
kk.push(new Integer(13));
kk.push(new Integer(14));
kk.push(new Integer(15));
System.out.println(kk);
kk.pop();
kk.pop(); //後進先出,不存在優先順序的概念
System.out.println(kk);
kk.push("132343");
System.out.println(kk);
}
}

4. java 中的堆棧是什麼

首先堆棧是計算機為程序分配的內存空間,用來存儲數據的。
在java中因為我們不直接操作內存,所以並不需要考慮指針的問題

在java中堆和棧也是用來存儲數據,其中棧存儲的引用,堆存儲的對象

如:Student s = new Student("張三");
s在棧中 張三在堆

5. java中棧是如何實現的

這是java.util包下的Stack類,你可以看一下它是如何實現的,至於用法,無非就是push,pop,peek等操作等

/*
* @(#)Stack.java 1.30 05/11/17
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

package java.util;

/**
* The <code>Stack</code> class represents a last-in-first-out
* (LIFO) stack of objects. It extends class <tt>Vector</tt> with five
* operations that allow a vector to be treated as a stack. The usual
* <tt>push</tt> and <tt>pop</tt> operations are provided, as well as a
* method to <tt>peek</tt> at the top item on the stack, a method to test
* for whether the stack is <tt>empty</tt>, and a method to <tt>search</tt>
* the stack for an item and discover how far it is from the top.
* <p>
* When a stack is first created, it contains no items.
*
* <p>A more complete and consistent set of LIFO stack operations is
* provided by the {@link Deque} interface and its implementations, which
* should be used in preference to this class. For example:
* <pre> {@code
* Deque<Integer> stack = new ArrayDeque<Integer>();}</pre>
*
* @author Jonathan Payne
* @version 1.30, 11/17/05
* @since JDK1.0
*/
public
class Stack<E> extends Vector<E> {
/**
* Creates an empty Stack.
*/
public Stack() {
}

/**
* Pushes an item onto the top of this stack. This has exactly
* the same effect as:
* <blockquote><pre>
* addElement(item)</pre></blockquote>
*
* @param item the item to be pushed onto this stack.
* @return the <code>item</code> argument.
* @see java.util.Vector#addElement
*/
public E push(E item) {
addElement(item);

return item;
}

/**
* Removes the object at the top of this stack and returns that
* object as the value of this function.
*
* @return The object at the top of this stack (the last item
* of the <tt>Vector</tt> object).
* @exception EmptyStackException if this stack is empty.
*/
public synchronized E pop() {
E obj;
int len = size();

obj = peek();
removeElementAt(len - 1);

return obj;
}

/**
* Looks at the object at the top of this stack without removing it
* from the stack.
*
* @return the object at the top of this stack (the last item
* of the <tt>Vector</tt> object).
* @exception EmptyStackException if this stack is empty.
*/
public synchronized E peek() {
int len = size();

if (len == 0)
throw new EmptyStackException();
return elementAt(len - 1);
}

/**
* Tests if this stack is empty.
*
* @return <code>true</code> if and only if this stack contains
* no items; <code>false</code> otherwise.
*/
public boolean empty() {
return size() == 0;
}

/**
* Returns the 1-based position where an object is on this stack.
* If the object <tt>o</tt> occurs as an item in this stack, this
* method returns the distance from the top of the stack of the
* occurrence nearest the top of the stack; the topmost item on the
* stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt>
* method is used to compare <tt>o</tt> to the
* items in this stack.
*
* @param o the desired object.
* @return the 1-based position from the top of the stack where
* the object is located; the return value <code>-1</code>
* indicates that the object is not on the stack.
*/
public synchronized int search(Object o) {
int i = lastIndexOf(o);

if (i >= 0) {
return size() - i;
}
return -1;
}

/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = 1224463164541339165L;
}

6. java 棧 什麼意思

棧是一種常用的數據結構,棧只允許訪問棧頂的元素,棧就像一個杯子,每次都只能取杯子頂上的東西,而對於棧就只能每次訪問它的棧頂元素,從而可以達到保護棧頂元素以下的其他元素.」先進後出」或」後進先出」就是棧的一大特點,先進棧的元素總是要等到後進棧的元素出棧以後才能出棧.遞歸就是利用到了系統棧,暫時保存臨時結果,對臨時結果進行保護.
對於棧的學習,建議你看一看<數據結構與演算法>這本書.

7. Java中什麼叫堆棧

和別的一樣啊,就是後進去的先出來!

8. Java中棧的使用

和C++裡面一樣,有入棧,彈棧,查找函數
import java.util.*;(引入包含棧類的頭文件)
相關函數介紹
boolean empty()
測試堆棧是否為空。
E peek()
查看堆棧頂部的對象,但不從堆棧中移除它。
E pop()
移除堆棧頂部的對象,並作為此函數的值返回該對象。
E push(E item)
把項壓入堆棧頂部。
int search(Object o)
返回對象在堆棧中的位置,以 1 為基數。

9. JAVA堆棧是什麼意思

堆棧是一種存儲方法,就像隊列.
不過不同的是
隊列是先進先出,堆棧是後進先出
例如依次存入A1到A9這10個變數,隊列的取出順序是A1,A2,A3....A9
堆棧則是A9,A8,A7....A1
各有各的用處,看具體情況

10. java 棧、方法棧的區別

棧與堆都是Java用來在Ram中存放數據的地方
String
a="a";這樣的創建形式,在棧中主要存放一些基本類型的和對象的句柄,棧有一個很重要的特殊性,就是存在棧中的數據可以共享
String
b
=
new
String("b");堆中主要存放java對象,同時可以在堆棧中創建一個對String類的對象引用變數,也就是說:Java中所有對象的存儲空間都是在堆中分配的,但是這個對象的引用卻是在堆棧中分配,也
就是說在建立一個對象時從兩個地方都分配內存,在堆中分配的內存實際建立這個對象,而在堆棧中分配的內存只是一個指向這個堆對象的指針(引用)而已。
其中的區別包括:申請空間大小、效率、存儲內容上的差異

熱點內容
好醫生連鎖店密碼多少 發布:2024-09-20 05:09:38 瀏覽:15
魔獸腳本代理 發布:2024-09-20 05:09:35 瀏覽:98
python登陸網頁 發布:2024-09-20 05:08:39 瀏覽:757
安卓qq飛車如何轉蘋果 發布:2024-09-20 04:54:30 瀏覽:178
存儲過程中in什麼意思 發布:2024-09-20 04:24:20 瀏覽:315
php顯示數據 發布:2024-09-20 03:48:38 瀏覽:501
源碼安裝軟體 發布:2024-09-20 03:44:31 瀏覽:354
入門編程游戲的書 發布:2024-09-20 03:31:26 瀏覽:236
e盒的演算法 發布:2024-09-20 03:30:52 瀏覽:144
win10登錄密碼如何修改登錄密碼 發布:2024-09-20 03:09:43 瀏覽:71