导航:首页 > 编程语言 > 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的栈相关的资料

热点内容
程序员真的累吗 浏览:323
学信网app为什么刷脸不了 浏览:871
天蝎vs程序员 浏览:991
单片机下载口叫什么 浏览:186
程序员的道 浏览:924
云服务器不实名违法吗 浏览:556
怎样查看文件夹图片是否重复 浏览:993
文件怎么导成pdf文件 浏览:806
打开sql表的命令 浏览:101
安卓手机如何面部支付 浏览:37
天元数学app为什么登录不上去 浏览:822
明日之后为什么有些服务器是四个字 浏览:102
安卓系统l1是什么意思 浏览:24
服务器一直崩应该用什么指令 浏览:923
cm202贴片机编程 浏览:729
php构造函数带参数 浏览:179
解压电波歌曲大全 浏览:345
为啥文件夹移到桌面成word了 浏览:860
命令符的安全模式是哪个键 浏览:760
编程中学 浏览:957