导航:首页 > 编程语言 > 栈java

栈java

发布时间:2022-01-25 16:05:25

java中什么叫堆栈

和别的一样啊,就是后进去的先出来!

⑵ java中什么是栈啊

存放基本类型的变量数据和对象的引用,但对象本身不存放在栈中,而是存放在堆(new 出来的对象)或者常量池中(字符串常量对象存放在常量池中。)。

栈和常量池中的对象可以共享,对于堆中的对象不可以共享。栈中的数据大小和生命周期是可以确定的,当没有引用指向数据时,这个数据就会消失。堆中的对象的由垃圾回收器负责回收,因此大小和生命周期不需要确定。

局部变量的数据存在于栈内存中。

栈的优势是,存取速度比堆要快,仅次于寄存器,栈数据可以共享。但缺点是,存在栈中的数据大小与生存期必须是确定的,缺乏灵活性。栈中主要存放一些基本类型的变量数据(int, short, long, byte, float, double, boolean, char)和对象句柄(引用)。

⑶ 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);
}
}

⑷ java 栈 什么意思

栈是一种常用的数据结构,栈只允许访问栈顶的元素,栈就像一个杯子,每次都只能取杯子顶上的东西,而对于栈就只能每次访问它的栈顶元素,从而可以达到保护栈顶元素以下的其他元素.”先进后出”或”后进先出”就是栈的一大特点,先进栈的元素总是要等到后进栈的元素出栈以后才能出栈.递归就是利用到了系统栈,暂时保存临时结果,对临时结果进行保护.
对于栈的学习,建议你看一看<数据结构与算法>这本书.

⑸ java 栈、方法栈的区别

栈与堆都是Java用来在Ram中存放数据的地方
String
a="a";这样的创建形式,在栈中主要存放一些基本类型的和对象的句柄,栈有一个很重要的特殊性,就是存在栈中的数据可以共享
String
b
=
new
String("b");堆中主要存放java对象,同时可以在堆栈中创建一个对String类的对象引用变量,也就是说:Java中所有对象的存储空间都是在堆中分配的,但是这个对象的引用却是在堆栈中分配,也
就是说在建立一个对象时从两个地方都分配内存,在堆中分配的内存实际建立这个对象,而在堆栈中分配的内存只是一个指向这个堆对象的指针(引用)而已。
其中的区别包括:申请空间大小、效率、存储内容上的差异

⑹ java中栈的应用

栈中存储的是方法参数变量和方法体中的局部变量还有整形变量。。具体的应用你可以去看thinking in java。。。

⑺ 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;
}

⑻ Java中为什么栈运行

ava Virtual Machine Stacks,线程私有,生命周期与线程相同,描述的是Java方法执行的内存模型:每一个方法执行的同时都会创建一个栈帧(Stack Frame),由于存储局部变量表、操作数栈、动态链接、方法出口等信息。每一个方法的执行就对应着栈帧在虚拟机栈中的入栈,出栈过程。

⑼ 栈与队列的实现(java)

import java.util.Stack;

public class Translate {
//程序入口
public static void main(String[]args){
int n = Translate.translate(3467,8);
System.out.println("结果是:"+n);
}

public static int translate(int number, int base_num) {
//使用栈
Stack<Integer>stack = new Stack<Integer>();
while(number>0){
//压栈
stack.push(number % base_num);
number /= base_num;
}
int n = stack.size();
int val=0;
//依次出栈并合成结果(用我们熟悉的十进制表示,所以乘以10)
for(int i=0;i<n;i++){
val=val*10+stack.pop();
}
return val;
}
}

⑽ java中栈与堆的区别

Java栈是与每一个线程关联的,JVM在创建每一个线程的时候,会分配一定的栈空间给线程。它主要用来存储线程执行过程中的局部变量,方法的返回值,以及方法调用上下文。栈空间随着线程的终止而释放
Java中堆是由所有的线程共享的一块内存区域,堆用来保存各种JAVA对象,比如数组,线程对象等

阅读全文

与栈java相关的资料

热点内容
通达信成本均线源码 浏览:614
可以下载的解压音频 浏览:564
海贼王怎么换服务器 浏览:318
计算机上的共享文件夹映射 浏览:940
荣耀安装包在文件夹哪里 浏览:195
机票php源码 浏览:231
linux共享mac 浏览:922
中国没有国外的服务器地址 浏览:758
为什么退款服务器连接错误 浏览:555
android短信存储位置 浏览:970
unix网络编程卷4 浏览:808
找靓机app下单什么时候发货 浏览:413
android一个应用两个进程 浏览:803
linux硬盘复制 浏览:808
php图片服务器搭建 浏览:801
下载压缩文件怎么打开 浏览:194
新建文件夹叫什么名字 浏览:567
windows20的开机命令 浏览:335
微信一般在电脑的那个文件夹 浏览:511
go在win7下编译特别慢 浏览:256