導航:首頁 > 編程語言 > java的棧

java的棧

發布時間:2022-01-19 06:51:36

A. java中什麼是棧啊

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

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

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

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

B. 我要用java實現一個棧,基本操作就是出棧入棧。請問如何實現效率比較高。

java的list的子類裡面有個叫vector,這個下面有個叫做stack,可以實現!你去看 看看

C. 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;
}

D. 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);
}
}

E. java中的調用棧具體指什麼,請幫忙解釋一下,可以的話舉例說明

調用棧可能就是方法棧的意思,方法棧是JVM為對象的每一次方法調用所分配的一塊獨立的內存空間,在對方法棧的理解上需要注意這樣幾個地方:
1.方法棧不是對象唯一的,對同一個對象的方法調用2次,這2次的方法棧是不一樣的。

2.方法棧是不能互相通信的,也就是說當一個方法還沒有返回任何值的時候,方法外部是不能獲得該方法內部參數的狀態的。

學習方法棧可以和線程的概念結合起來理解,因為方法棧是綁定在當前線程之上的。

粗淺的個人理解,希望沒有對你產生誤導。。。Good Luck!

F. Java中什麼叫堆棧

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

G. java 中的堆棧是什麼

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

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

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

H. java 棧、方法棧的區別

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

I. java 棧 什麼意思

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

J. java,中的堆和棧

參考我給別人同樣問題的回答吧

http://..com/question/16857.html

閱讀全文

與java的棧相關的資料

熱點內容
明日之後為什麼有些伺服器是四個字 瀏覽:100
安卓系統l1是什麼意思 瀏覽:21
伺服器一直崩應該用什麼指令 瀏覽:916
cm202貼片機編程 瀏覽:724
php構造函數帶參數 瀏覽:175
解壓電波歌曲大全 瀏覽:336
為啥文件夾移到桌面成word了 瀏覽:858
命令符的安全模式是哪個鍵 瀏覽:758
編程中學 瀏覽:956
單片機求助 瀏覽:993
ug加工側面排銑毛坯怎麼編程 瀏覽:271
程序員有關的介紹 瀏覽:736
支付寶使用的什麼伺服器 瀏覽:210
安卓看本地書用什麼軟體好 瀏覽:921
經傳軟體滾動凈利潤指標源碼 瀏覽:522
螢石雲視頻已加密怎麼解除 瀏覽:574
一命令四要求五建議 瀏覽:30
qq文件夾遷移不了 瀏覽:19
液體粘滯系數測定不確定度演算法 瀏覽:332
輕棧源碼 瀏覽:426