java棧方法
1. 我要用java實現一個棧,基本操作就是出棧入棧。請問如何實現效率比較高。
//這是JDK提供的棧
import java.util.Stack;
public class UsingStack {
public static void main(String[] args) {
//構造棧對象,使用類型限制,只能存儲Integer數據
Stack<Integer> s = new Stack<Integer>();
//1、2、3依次入棧
s.push(1);
s.push(2);
s.push(3);
//3、2、1依次出棧
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());
}
}
//這是我寫的順序結構的棧
import java.util.EmptyStackException;
import java.util.Vector;
public class UsingStack{
public static void main(String[] args){
//構造棧對象,使用類型限制,只能存儲Integer數據
MyStack<Integer> s = new MyStack<Integer>();
//1、2、3依次入棧
s.push(1);
s.push(2);
s.push(3);
//3、2、1依次出棧
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());
}
}
/**
* 棧類
* @author developer_05
* @param <T>
*/
class MyStack<T> extends Vector<T>{
/**
* 構造方法
*/
public MyStack(){
}
/**
* 入棧方法
* @param item 待入棧的元素
* @return 返回入棧的元素
*/
public T push(T item) {
addElement(item);
return item;
}
/**
* 出棧方法(同步處理)
* @return 返回出棧元素
*/
public synchronized T pop() {
T obj;
int len = size();
if (len == 0)
throw new EmptyStackException();
obj = elementAt(len - 1);
removeElementAt(len - 1);
return obj;
}
/**
* 判斷棧是否為空的方法
* @return 返回true(棧空)或false(棧非空)
*/
public boolean empty() {
return size() == 0;
}
private static final long serialVersionUID = 1L;
}
2. Java內存中的棧,堆和方法區的用法有什麼不同
JAVA的JVM的內存可分為3個區:堆(heap)、棧(stack)和方法區(method)也叫靜態存儲區。
堆區:
存儲的全部是對象,每個對象都包含一個與之對應的class的信息。(class的目的是得到操作指令)
2.jvm只有一個堆區(heap)被所有線程共享,堆中不存放基本類型和對象引用,只存放對象本身
棧區:
每個線程包含一個棧區,棧中只保存基礎數據類型的對象和自定義對象的引用(不是對象),對象都存放在堆區中
2.每個棧中的數據(原始類型和對象引用)都是私有的,其他棧不能訪問。
3.棧分為3個部分:基本類型變數區、執行環境上下文、操作指令區(存放操作指令)。
方法區:
又叫靜態區,跟堆一樣,被所有的線程共享。方法區包含所有的class和static變數。
2.方法區中包含的都是在整個程序中永遠唯一的元素,如class,static變數。
3. JAVA方法執行是棧結構嗎
棧幀(Stack Frame)是用於支持虛擬機進行方法調用和方法執行的數據結構,它回是虛擬機運行時數據區答中的虛擬機棧(Virtual Machine Stack)的錢元素。棧制存儲了方法的網部變最表、操作數棧、動態連接和方法返回地址等信息。每個方法從調用開始到執行完成的過程,就對應著一個棧幀在虛擬機裡面從入棧到出棧的過程。
每一個棧幀都包括了局部變數表、操作數棧、方法返回地址和一些額外的附加信息,在編譯程序代碼的時候,棧幀中需要多大的局部變數表、多深的操作數棧都已經完全確定了,並且寫入到方法表的Code屬性之中,因此一個棧幀需要分配多少內存, 不會受到程序運行期交最數據的影響,而僅僅取決於具體的虛擬機實現,
4. java中的,堆,棧,還有方法區都是用來放什麼的
堆區:
1.存儲的全部是對象,每個對象都包含一個與之對應的class的信息。(class的目的是得到操作指令)
2.jvm只有一個堆區(heap)被所有線程共享,堆中不存放基本類型和對象引用,只存放對象本身
棧區:
1.每個線程包含一個棧區,棧中只保存基礎數據類型的對象和自定義對象的引用(不是對象),對象都存放在堆區中
2.每個棧中的數據(原始類型和對象引用)都是私有的,其他棧不能訪問。
3.棧分為3個部分:基本類型變數區、執行環境上下文、操作指令區(存放操作指令)。
方法區:
1.又叫靜態區,跟堆一樣,被所有的線程共享。方法區包含所有的class和static變數。
2.方法區中包含的都是在整個程序中永遠唯一的元素,如class,static變數。
5. java 棧、方法棧的區別
棧與堆都是Java用來在Ram中存放數據的地方
String a="a";這樣的創建形式,在棧中主要存放一些基本類型的和對象的句柄,棧有一個很重要的特殊性,就是存在棧中的數據可以共享
String b = new String("b");堆中主要存放java對象,同時可以在堆棧中創建一個對String類的對象引用變數,也就是說:Java中所有對象的存儲空間都是在堆中分配的,但是這個對象的引用卻是在堆棧中分配,也 就是說在建立一個對象時從兩個地方都分配內存,在堆中分配的內存實際建立這個對象,而在堆棧中分配的內存只是一個指向這個堆對象的指針(引用)而已。
其中的區別包括:申請空間大小、效率、存儲內容上的差異
6. java做棧
沒有必要這樣做吧,因為在容器類中Stack已經很好實現的棧的所有功能了,
7. java方法怎麼在棧中是直接復制嗎
你得明白棧的定義。代碼執行的時候是執行一個方法,執行完,返回方法的上一個代碼塊繼續往下執行後面的內容。這樣的話是不是就是一個棧結構了?先進後出。方法一邊執行,一邊往棧裡面存數據,等執行完了就取出數據(取出的是返回值,是最後一個存進去的 棧結構是後進先出),然後執行外面的代碼。這么說你可能不明白,我給你舉個例子。
int sub(int a,int b){
return a+b;
}
int c = sub(2,3);//注意執行這條語句的時候是不是執行了一個方法?
//那麼語句執行的時候是要從左往右執行的對吧,但是事實的邏輯卻是先算出來sub(2,3)這個方
//法的返回值,然後再把返回值(5)賦值給 c ,那麼這個怎麼實現,肯定是一個棧的數據結構,編譯的時候先把」int c = 「入棧,然後再把 sub(2,3),入棧,執行的時候,從棧裡面取,取的第一個肯定是sub(2,3)吧?於是就計算出等於5,繼續取,取出了int c =,然後就和5對接上了,就把值賦給c了。這只是一個小例子。
道理是這樣,但是具體的存取可不是這樣的哦。具體的存取應該分的非常細膩,應該是按照java語法的最小單位來往棧里存取的。說白了一句話,程序運行的時候的先後順序是跟人大腦想問題的順序一樣的,但是代碼不是按照這樣的順序寫的(從左到右),於是就用棧結構來達到這樣的效果。
8. 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;
}
9. JAVA 方法的入棧出棧問題
你得明白棧的定義。代碼執行的時候是執行一個方法,執行完,返回方法的上一個代碼塊繼續往下執行後面的內容。這樣的話是不是就是一個棧結構了?先進後出。方法一邊執行,一邊往棧裡面存數據,等執行完了就取出數據(取出的是返回值,是最後一個存進去的 棧結構是後進先出),然後執行外面的代碼。這么說你可能不明白,我給你舉個例子。
int sub(int a,int b){
return a+b;
}
int c = sub(2,3);//注意執行這條語句的時候是不是執行了一個方法?
//那麼語句執行的時候是要從左往右執行的對吧,但是事實的邏輯卻是先算出來sub(2,3)這個方
//法的返回值,然後再把返回值(5)賦值給 c ,那麼這個怎麼實現,肯定是一個棧的數據結構,編譯的時候先把」int c = 「入棧,然後再把 sub(2,3),入棧,執行的時候,從棧裡面取,取的第一個肯定是sub(2,3)吧?於是就計算出等於5,繼續取,取出了int c =,然後就和5對接上了,就把值賦給c了。這只是一個小例子。
道理是這樣,但是具體的存取可不是這樣的哦。具體的存取應該分的非常細膩,應該是按照java語法的最小單位來往棧里存取的。說白了一句話,程序運行的時候的先後順序是跟人大腦想問題的順序一樣的,但是代碼不是按照這樣的順序寫的(從左到右),於是就用棧結構來達到這樣的效果。
這么說,明白了嗎?
10. java 中 棧的講解,請高人指教
不用的,這里只是讓你了解棧的含義與基本操作
一般情況用不大到,需要時知道什麼是棧以及它的特性就可以了