符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
Stack.java
10年积累的网站设计、成都做网站经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站策划后付款的网站建设流程,更有郓城免费网站建设让你可以放心的选择与我们合作。
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Stack {
private int[] data;
private int index;
private Lock lock;
private Condition moreSpace;
private Condition moreEelment;
public Stack(int size){
this.data = new int[size];
this.index = 0;
this.lock = new ReentrantLock();
this.moreSpace = lock.newCondition();
this.moreEelment = lock.newCondition();
}
public void push(int value){
lock.lock();
try {
while(index == data.length){
moreSpace.await();
}
data[index++] = value;
moreEelment.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public int popup(){
lock.lock();
int value = 0;
try {
while(index == 0){
moreEelment.await();
}
value = data[--index];
moreSpace.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
return value;
}
}
写入线程 WriteStack.java
import java.util.Random;
public class WriteStack implements Runnable {
private Stack stack;
public WriteStack(Stack stack){
this.stack = stack;
}
@Override
public void run() {
Random r = new Random(System.currentTimeMillis());
for(int i = 0;i 10; i++){
int value = r.nextInt(500);
stack.push(value);
System.out.printf("Write: push %d in stack\n",value);
try {
Thread.sleep(r.nextInt(500));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
读取线程 ReadStack.java
import java.util.Random;
public class ReadStack implements Runnable {
private Stack stack;
public ReadStack(Stack stack){
this.stack = stack;
}
@Override
public void run() {
Random r = new Random(System.currentTimeMillis());
for(int i = 0;i 10; i++){
int value = stack.popup();
System.out.printf("Read: popup an element %d\n",value);
try {
Thread.sleep(r.nextInt(500));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
主测试线程 StackExample.java
public class StackExample {
public static void main(String[] args) throws InterruptedException {
Stack stack = new Stack(5);
WriteStack writeStack = new WriteStack(stack);
ReadStack readStack = new ReadStack(stack);
Thread writeThread = new Thread(writeStack);
Thread readThread = new Thread(readStack);
writeThread.start();
readThread.start();
}
}
public class Stack {
private Object[] stack;
//这个不需要;
//private int top = 0; //初始化栈顶
//这个也不需要;
//写一个栈出来,最好是可以动态的,可以自己改变大小的,即数组的长度;
//private int size = 0; // 初始化大小
//元素个数;
private int size;
//默认长度为10;
public Stack(){
this(10);
}
//也可以自己设置长度,即容量;
public Stack(int len){
stack = new Object[len];
}
//返回元素个数;
public int size(){
return size;
}
//返回数组长度,即容量;
public int capacity(){
return stack.length;
}
//实现动态的数组;
public void ensureCapacity(){
if(size() == capacity()){
Object[] newStack = new Object[size() * 3 / 2 + 1];
System.arraycopy(stack, 0, newStack, 0, size());
stack = newStack;
}
}
//入栈;
public void push(Object o){
size++;
ensureCapacity();
stack[size - 1] = o;
}
/*
public void push(Object object) {
if (isFull()) {
System.out.println("栈满! 入栈失败");
}
stack[top++] = object;
}
*/
//判空;
public boolean isEmpty(){
return size == 0;
}
//出栈;
public Object pop(){
//首先要判空;
if(isEmpty()){
throw new ArrayIndexOutOfBoundsException("不能为空");
}
Object o = stack[--size];
stack[size] = null;
return o;
}
/*
// 出栈
public Object pop() {
Object object = stack[--top];
stack[top] = null;
return object;
}
*/
/*
// 计算栈当前大小
public int size() {
return top;
}
// 判断是否是空栈
public boolean isEmpey() {
return top == 0;
}
// 判断是否栈满
public boolean isFull() {
return top = size;
}
public Stack(int size) {
this.size = size;
}
*/
public static void main(String[] args) {
Stack stack = new Stack(3);
String[] data = new String[] { "a", "b", "c" };
for (int i = 0; i data.length; i++) {
stack.push(data[i]);
System.out.println(data[i] + "");
}
System.out.println("***********");
while (!stack.isEmpty()) {
System.out.println(stack.pop() + "");
}
//}
}
}
你自己对比一下,我是在你的里面修改的
如何使用java代码实现栈和队列
import java.util.Scanner;
import java.util.Stack;
/**
* @author Owner
*
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n= sc.nextInt();//3条测试数据数据
StackCharacter stack = null;
while(n!=0){
//从控制台读入一个测试字符串[]() [(])
String str = sc.next();
//如果该输入字符串为奇数,说明不匹配
if(str.length() % 2 == 1){
System.out.println("No");
}else{
//说明字符是偶数
stack = new StackCharacter();
//遍历第一条测试字符串[]() [(])
for(int i=0;istr.length();i++){
if(stack.isEmpty()){
//如果栈是空的
stack.push(str.charAt(i));
}else if(stack.peek() == '[' str.charAt(i) == ']' || stack.peek() == '(' str.charAt(i) == ')'){
//说明此时栈中字符不是空的,并且符合,
stack.pop();
}else{
stack.push(str.charAt(i));
}
}
if(stack.isEmpty()){
//如果栈是空的,说明a href=";tn=44039180_cprfenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3mhDvP10Ln19hrANbP1fk0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3ErHnLP1nsPHDznHTkn1fYPHnz" target="_blank" class="baidu-highlight"括号/a匹配
System.out.println("Yes");
}else{
//说明栈不为空,a href=";tn=44039180_cprfenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3mhDvP10Ln19hrANbP1fk0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3ErHnLP1nsPHDznHTkn1fYPHnz" target="_blank" class="baidu-highlight"括号/a不匹配
System.out.println("No");
}
}
n--;
}
}
}
Java栈的实现
public
class
MyStack
{
//定义一个堆栈类
int[]
array;
//用int数组来保存数据,根据需要可以换类型
int
s_size;
//定义堆栈的宽度
public
MyStack(int
i){
//定义一个带参数构造器
array=new
int[i];
//动态定义数组的长度
s_size=0;
//堆栈的默认宽度为0
}
public
MyStack(){
//默认构造器
this(50);
//默认构造器可容纳50个元素
}
public
void
push(int
i){
//压栈
array[this.s_size]=i;
this.s_size++;
}
public
int
pop(){
//从堆栈中取元素,从栈顶开始取
if(this.s_size!=0){
int
t=array[s_size-1];
//用中间变量保存栈顶的元素
array[s_size-1]=0;
//取完元素该位置设为0
s_size--;
//栈的大小减1
return
t;
//返回栈顶元素
}else{
System.out.println("This
stack
is
empty");
//当栈为空时显示提示信息,返回0
return
0;
}
}
public
boolean
isEmpty(){
//判断栈是否为空
return
this.s_size==0;
}
public
int
top(){
//从栈顶取值,功能和
pop()
方法一样
if(!this.isEmpty()){
int
t=array[this.s_size-1];
array[this.s_size-1]=0;
this.s_size--;
return
t;
}else{
System.out.println("This
stack
is
empty!");
return
0;
}
}
public
void
printAll(){
//打印出堆栈中的所有元素的值,不是取出,元素依然在堆栈里
if(!this.isEmpty()){
for(int
i=this.s_size
-
1;i=0;i--){
System.out.println(array[i]);
}
}
}
//下面是测试代码
public
static
void
main(String[]
args){
MyStack
stack=new
MyStack();
stack.push(4);
stack.push(5);
stack.push(6);
stack.push(7);
//System.out.println(stack.isEmpty());
stack.printAll();
System.out.println("===========");
System.out.println(stack.top());
System.out.println(stack.top());
System.out.println(stack.top());
System.out.println(stack.top());
System.out.println(stack.top());
}
}
使用java.util包中的Stack类创建一个栈对象
public Object push(Object data);输入数据,实现压栈
public Object pop();输出数据,实现弹栈
public boolean empty()判空
public Object peek();查看栈顶元素
可以去查查API嘛
我也是学java的,大家一起进步。