- 浏览: 565087 次
- 性别:
- 来自: 南京
文章分类
- 全部博客 (350)
- Sybase (30)
- SQL SERVER2005 (14)
- 数据库 (27)
- SSH框架 (27)
- WebService (21)
- 下载-软件收藏 (15)
- 随笔-日常使用 (9)
- Flex 相关 (13)
- Linux (11)
- Web (64)
- XML相关 (9)
- Socket相关 (1)
- Elipse (3)
- 统计报表 (11)
- 线程相关 (3)
- Java相关 (37)
- JAVASCRIPT (19)
- JAVA反射 (3)
- JSP标签 (3)
- 随笔-其他 (2)
- 随笔-设计模式 (3)
- 随笔-架构师相关 (1)
- 下载-源码 (7)
- 下载-帮助文档 (1)
- 下载-插件 (6)
- 技术-.NET (2)
- 技术-Excel VBA (8)
- 应用-地图相关 (2)
- 应用-GSM短信猫 (5)
- 应用-单点登录 (3)
- Android相关 (3)
最新评论
-
sucheng2016:
发现jconn4.jar 里面有getBlob(String) ...
Sybase15驱动包的问题com.sybase.jdbc3.jdbc.SybDriver -
sucheng2016:
java.lang.UnsupportedOperationE ...
Sybase15驱动包的问题com.sybase.jdbc3.jdbc.SybDriver -
ok123zxx:
没下文了吗
通过 Tomcat Advanced I/O 获得高性能的 Ajax tocmat6+CometProcessor -
q1345111:
大家这个问题 尚未完成方法 com.sybase.jdbc3. ...
Sybase15驱动包的问题com.sybase.jdbc3.jdbc.SybDriver -
bdk82924:
heshujing217187 写道问题同1楼一样,求解!换j ...
Sybase15驱动包的问题com.sybase.jdbc3.jdbc.SybDriver
Java实现 栈(stack)与堆(heap)
上次写过一个的,下次记得把代码贴上来
待续...
HeapUtil
StackUtil
Test
MyVec 是我写的另外一个例子
特殊的缓存列表 首先有500的缓存大小用来入数据, 10缓存的大小用来读取数据, 读完后指针及指向后面了
上次写过一个的,下次记得把代码贴上来
待续...
HeapUtil
import java.util.EmptyStackException; import java.util.Vector; /** * * 模拟 堆,先进先出 * * @author * @date 2012-6-27 上午02:19:06 * * @version 1.0 */ public class HeapUtil<E> extends Vector<E> { /** * Creates an empty Stack. */ public HeapUtil() { } /** * 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() { if (empty()) { return null; } E obj; int len = size(); obj = peek(); removeElementAt(0); 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(0); } /** * 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; }
StackUtil
/** * * 栈 先进后出 * * @author * @date 2012-6-27 上午08:36:52 * * @param <E> * @version 1.0 */ public class StackUtil<E> extends Vector<E> { /** * Creates an empty Stack. */ public StackUtil() { } /** * 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() { if (empty()) { return null; } 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; }
Test
import java.util.Arrays; public class Test { public static void main(String[] args) { MyVec<Integer> m = new MyVec<Integer>(5); m.push(1); m.push(2); m.push(3); m.push(4); m.push(5); System.out.println(Arrays.asList(m.get())); System.out.println(Arrays.asList(m.get())); System.out.println(Arrays.asList(m.get())); System.out.println(Arrays.asList(m.get())); System.out.println(Arrays.asList(m.get())); m.push(6); m.push(7); m.push(8); System.out.println(Arrays.asList(m.get())); m.push(9); m.push(10); System.out.println(Arrays.asList(m.get())); System.out.println(Arrays.asList(m.get())); System.out.println(Arrays.asList(m.get())); System.out.println(Arrays.asList(m.get())); // while (!m.empty()) // { // System.out.println(m.pop()); // } } public static void main1(String[] args) { System.out.println("StackUtil......"); StackUtil<String> s = new StackUtil<String>(); s.push("1"); s.push("2"); s.push("3"); while (!s.empty()) { System.out.println(s.pop()); } System.out.println("HeapUtil......"); HeapUtil<String> h = new HeapUtil<String>(); h.push("1"); h.push("2"); h.push("3"); while (!h.empty()) { System.out.println(h.pop()); } System.out.println("QueueUtil......"); QueueUtil<String> q = new QueueUtil<String>(); q.add("1"); q.add("2"); q.add("3"); for (int i = 0; i < 10; i++) { System.out.println(q.next()); } } }
MyVec 是我写的另外一个例子
特殊的缓存列表 首先有500的缓存大小用来入数据, 10缓存的大小用来读取数据, 读完后指针及指向后面了
import java.util.ArrayList; import java.util.EmptyStackException; import java.util.List; import java.util.Vector; /** * * 特殊的缓存列表 首先有500的缓存大小用来入数据, 10缓存的大小用来读取数据, 读完后指针及指向后面了, * * @author bdk197431 * @date 2013-1-14 上午12:41:00 * * @version 1.0 */ public class MyVec<E> extends Vector<E> { /** * 总大小,缓存的申请大小,如500 */ private int maxSize; /** * 读取缓存的大小,如10,这个值要小于maxSize */ private int readBuffMaxSize = 2; /** * 指针的指向的id */ private int startIndex; /** * 指针的指向的endId */ private int endIndex; public MyVec() { } public MyVec(int maxSize) { this.maxSize = maxSize; } public void debug(String s) { // System.out.println(s); } public String toString() { StringBuilder sb = new StringBuilder(); sb.append("["); for (int i = 0; i < size(); i++) { sb.append(elementAt(i)); if (i != size() - 1) { sb.append(","); } } sb.append("]"); return sb.toString(); } public List<E> get() { debug("get"); List<E> list = new ArrayList<E>(); for (int i = startIndex; i < endIndex; i++) { list.add(get(i)); } if (startIndex + readBuffMaxSize > endIndex) { startIndex = endIndex; } else { startIndex += readBuffMaxSize; endIndex += readBuffMaxSize; } if (endIndex >= maxSize) { endIndex = maxSize; } // readBuffSize = 0; debug("start:" + startIndex + " end:" + endIndex); debug(this.toString()); return list; } public E get(int index) { if (index >= this.maxSize && index <= endIndex) { index = index % this.maxSize; } return elementAt(index); } /** * * 增加 读取缓存的大小 * * @author * @date 2013-1-14 上午03:22:11 */ private void addReadBuffSize() { endIndex++; if (endIndex - startIndex > readBuffMaxSize) { endIndex = startIndex + readBuffMaxSize; } if (startIndex > 0) { moveIndex(-1); } } private void moveIndex(int num) { startIndex += num; endIndex += num; if (endIndex > maxSize) { moveIndex(-1); } } /** * 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) { debug("push"); addElement(item); if (this.size() > maxSize) { removeElementAt(0); // 指针整体移动一位 moveIndex(1); } addReadBuffSize(); debug("start:" + startIndex + " end:" + endIndex); 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() { if (empty()) { return null; } 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; }
发表评论
-
Jav解析soap的xml
2015-05-17 21:13 799解析xml import org.dom4j.Docume ... -
Json转换利器Gson
2013-12-13 08:59 524转: http://blog.csdn.net/lk_blog ... -
asm jar包冲突的问题和解决方法(转)
2013-11-03 01:48 1155asm jar包冲突的问题和解决方法 在用Spring+Hi ... -
Spring ehCache 示例
2013-04-28 15:46 780http://blog.chinaunix.net/uid-2 ... -
将json字符串转换为bean (json-lib)
2013-04-26 15:26 1014Json-lib可以将Java对象转成json格式的字符串,也 ... -
QPID学习
2013-03-09 14:13 4447最近在看QPID 首先看下QPID是什么,他是个消息队列,用 ... -
短网址的原理和实现
2013-01-22 15:36 15376微博上经常会看到类似 http://t.cn/Afafhe 这 ... -
Tomcat7.0下实现的websocket 代码
2013-01-07 19:37 989测试环境: JDK1.6 Tomcat7.0.30 ... -
java IO写入文件效率——几种方法比较
2012-11-02 11:17 1081总结: 如果按字符和字节来分类,除方法1和2,其余都是按字符 ... -
Apache自带压力测试工具AB的使用方法
2012-09-18 11:26 1311使用例子: 1、打开dos界面,开始-》运行-》输入“cmd ... -
MyEclipse6.01注册码,Java源码
2012-07-02 16:12 1102不用为注册码犯愁了.. 下面是在网上搜索到的一段代码 ,分享给 ... -
ant解决OutOfMemoryError 或者Error starting modern compiler
2012-05-11 16:36 1390起因:在执行ant脚本的时候 ,报的错误是 Error st ... -
Java中使用Json 用到的jar包
2012-02-22 08:25 1381操作json开源的Jar包很多 ,那么多的jar选择哪个好呢 ... -
JsonUtils 类,将任意数据格式转换为Json格式
2012-02-21 09:12 1815package json; ... -
Java生成二维码或一维条形码(待续 未完)
2012-02-13 08:22 1765Java生成二维码或一维条形码(待续) 前段时间用了“ ... -
Java操作SVN(待续)
2012-02-02 08:31 1023一般大家都会直接使用snv客户端或者在eclipse中svn插 ... -
使用BeanUtils类简化request和resultset转换
2012-02-01 15:41 1089转:http://xdwangiflytek.iteye.co ... -
HttpClient 网络抓取
2012-01-20 10:08 1132利用 HttpClient 进行抓取 ... -
用Java实现按字节长度截取中英文数字字符串的方法总结 .
2011-12-20 16:59 1663转:http://blog.csdn.net/yanwudin ... -
JAVA读写INI文件,亲测
2011-12-01 16:01 4066代码如下: import java.io.Buf ...
相关推荐
"Java 中堆(heap)和堆栈(stack)的区别" Java 中堆(heap)和堆栈(stack)是两个不同的内存区域,分别用于存储不同的数据类型和对象。堆栈(stack)是 Java 中的一种内存区域,用于存储基本类型的变量和对象的...
栈(stack)与堆(heap)都是Java用来在Ram中存放数据的地方。与C++不同,Java自动管理栈和堆,程序员不能直接地设置栈或堆。
堆(heap)与栈(stack)是计算机内存管理中的两种基本数据结构,用于存储程序运行时产生的临时变量。在C语言中,这两种内存区域有非常明确的区分,对于理解程序的内存分配和回收具有重要意义。 首先,栈是一种特殊...
栈(stack)与堆(heap)都是Java用来在Ram中存放数据的地方。与C++不同,Java自动管理栈和堆,程序员不能直接地设置栈或堆。
关于Java栈与堆的深入解析 Java作为一种广泛使用的编程语言,其内存管理机制是学习者必须掌握的核心概念之一。在Java中,栈(Stack)与堆(Heap)是用于存储数据的主要区域,它们各自承担着不同的职责,对于理解...
Java内存主要分为三个区域:堆(Heap)、栈(Stack)和方法区(Method Area),其中堆是用于存储对象实例的主要区域,当堆空间不足时,就会抛出`OutOfMemoryError: Java heap space`。 1. **调整JVM堆大小**:可以...
程序运行时所使用的内存主要分为两类:堆内存(Heap Memory)和栈内存(Stack Memory)。理解这两种内存类型的工作原理及其区别对于优化程序性能、避免内存泄漏等问题至关重要。本文将深入探讨Java中堆内存与栈内存...
在Java编程中,理解堆(Heap)和栈(Stack)的概念及其区别对于程序员来说至关重要。本文将深入剖析这两个概念,并探讨它们之间的差异以及如何影响程序的运行。 #### 二、Java中的栈(Stack) Java中的栈主要用于...
在Java程序中,`java.lang.OutOfMemoryError: Java heap space` 是一个常见的错误,意味着程序在运行过程中耗尽了JVM分配的堆内存。这个错误通常发生在创建大量对象或者单个对象占用过多内存时。 一、问题描述与...
Java内存主要分为堆(Heap)、栈(Stack)、方法区(Method Area)、程序计数器(PC Register)和本地方法栈(Native Method Stack)五大部分: 1. **堆**:Java对象主要存储在堆中,它是所有线程共享的一块区域。...
本文将深入探讨Java中的两种主要内存区域:堆内存(Heap Memory)和栈内存(Stack Memory)。这两种内存分别承担着不同的角色,对于程序员理解和优化Java程序至关重要。 #### 二、栈内存 栈内存主要用于存储方法的...
在Java编程语言中,理解栈(stack)和堆(heap)的概念及其工作原理对于深入掌握Java虚拟机(JVM)如何管理内存至关重要。栈和堆都是在RAM中用于存储数据的关键区域,但在功能、使用场景和管理机制上存在显著差异。 #### ...
与堆不同,Java的栈用于存储基本类型变量和对象的引用。栈是一种线性的数据结构,遵循先进后出(LIFO)的原则。当函数调用发生时,相应的局部变量和参数会被压入栈中;当函数返回时,这些数据将从栈中弹出,释放所占...
##### 示例2:字符串常量池与堆的区别 ```java String s0 = "kvill"; String s1 = "kvill"; String s2 = "kv" + "ill"; System.out.println(s0 == s1); System.out.println(s0 == s2); ``` 这里涉及到字符串常量池...
Java程序运行时,内存分为堆内存(Heap)和栈内存(Stack)。堆内存主要用来存储对象实例和数组,而栈内存主要存储基本类型变量和对象引用。 2. **堆内存分配** 堆内存是Java中的全局共享区域,用于存储所有的...
在Java虚拟机(JVM)中,内存分为两个部分:Stack(栈)和Heap(堆)。Stack是JVM的内存指令区,管理很简单,push一定长度字节的数据或者指令,Stack指针压栈相应的字节位移;pop一定字节长度数据或者指令,Stack...
在Java中,内存被分为两部分:堆(Heap)和栈(Stack)。堆是用来存放对象的,它是由Java虚拟机(JVM)管理的。栈是用来存放基本类型的变量和对象的引用变量的,它是由Java编译器管理的。 2. 堆的内存分配 在Java...