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 中 栈的讲解,请高人指教
不用的,这里只是让你了解栈的含义与基本操作
一般情况用不大到,需要时知道什么是栈以及它的特性就可以了