import java.util.Stack;
創(chuàng)新互聯(lián)是一家專業(yè)提供泗水企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計、成都網(wǎng)站制作、成都h5網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為泗水眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。
public class Translate {
//程序入口
public static void main(String[]args){
int n = Translate.translate(3467,8);
System.out.println("結(jié)果是:"+n);
}
public static int translate(int number, int base_num) {
//使用棧
StackIntegerstack = new StackInteger();
while(number0){
//壓棧
stack.push(number % base_num);
number /= base_num;
}
int n = stack.size();
int val=0;
//依次出棧并合成結(jié)果(用我們熟悉的十進(jìn)制表示,所以乘以10)
for(int i=0;in;i++){
val=val*10+stack.pop();
}
return val;
}
}
public class Stack {
private Object[] stack;
//這個不需要;
//private int top = 0; //初始化棧頂
//這個也不需要;
//寫一個棧出來,最好是可以動態(tài)的,可以自己改變大小的,即數(shù)組的長度;
//private int size = 0; // 初始化大小
//元素個數(shù);
private int size;
//默認(rèn)長度為10;
public Stack(){
this(10);
}
//也可以自己設(shè)置長度,即容量;
public Stack(int len){
stack = new Object[len];
}
//返回元素個數(shù);
public int size(){
return size;
}
//返回數(shù)組長度,即容量;
public int capacity(){
return stack.length;
}
//實現(xiàn)動態(tài)的數(shù)組;
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;
}
*/
/*
// 計算棧當(dāng)前大小
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.util包中的Stack類創(chuàng)建一個棧對象
public Object push(Object data);輸入數(shù)據(jù),實現(xiàn)壓棧
public Object pop();輸出數(shù)據(jù),實現(xiàn)彈棧
public boolean empty()判空
public Object peek();查看棧頂元素
可以去查查API嘛
我也是學(xué)java的,大家一起進(jìn)步。
如何使用java代碼實現(xiàn)棧和隊列
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條測試數(shù)據(jù)數(shù)據(jù)
StackCharacter stack = null;
while(n!=0){
//從控制臺讀入一個測試字符串[]() [(])
String str = sc.next();
//如果該輸入字符串為奇數(shù),說明不匹配
if(str.length() % 2 == 1){
System.out.println("No");
}else{
//說明字符是偶數(shù)
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--;
}
}
}