求求各位大神帮忙看看,说说,改改,到底哪里错误了。 谢谢啦
import java.util.*;
import java.util.Scanner;
import java.util.Stack;
import java.io.File;
public class test
{
private static int i;
private static final char LEFTPARENT = '(';
private static final char RIGHTPARENT = ')';
private static final char LEFTBRACE = '{';
private static final char RIGHTBRACE = '}';
private static final char LEFTBRACKET = '[';
private static final char RIGHTBRACKET = ']';
public static boolean isBalanced(String s)
{
Stack<E> stack = new Stack<E>();
for (i = 0; i < s.length(); i++)
{
if (s.charAt(i) == LEFTPARENT) stack.push(LEFTPARENT);
else if (s.charAt(i) == LEFTBRACE) stack.push(LEFTBRACE);
else if (s.charAt(i) == LEFTBRACKET) stack.push(LEFTBRACKET);
else if (s.charAt(i) == RIGHTPARENT)
{
if (stack.isEmpty())
return false;
if (stack.pop() != LEFTPARENT)
return false;
}
else if (s.charAt(i) == RIGHTBRACE)
{
if (stack.isEmpty())
return false;
if (stack.pop() != LEFTBRACE)
return false;
}
else if (s.charAt(i) == RIGHTBRACKET)
{
if (stack.isEmpty())
return false;
if (stack.pop() != LEFTBRACKET)
return false;
}
}
return stack.isEmpty();
}
public static void main(String args[])
{
Scanner reader = new Scanner(new File("data.txt"));
while (reader.hasNext()){
int s = reader.nextInt();
System.out.println(i);
}
{
if (isBalanced(s))
System.out.println("The grouping symbols in"+" \""+ s +"\" "+"match");
else
System.out.println("The grouping symbols in"+" \""+ s +"\" "+ "do not match");
System.out.println();
}
}
}
------解决思路----------------------
题目的意思,应该是让你自己写一个 Stack<E> 吧?
------解决思路----------------------
package collectiontest;
public class MyStack<E> {
private Object[] es = null;
private final int len = 2;
public MyStack() {
es = new Object[len];
}
public boolean isEmpty() {
for (Object e : es) {
if (e != null) {
return false;
}
}
return true;
}
private void fill(Object[] os, Object o) {
for (Object obj : os) {
obj = o;
}
}
private void beforePop() {
for (Object e : es) {
if (e == null) {
return;
}
}
Object[] back = es;
es = new Object[es.length + len];
fill(es, null);
for (int i = 0; i < back.length; i++) {
es[i] = back[i];
}
}
@SuppressWarnings("unchecked")
public synchronized E peek() {
if (isEmpty()) {
throw new RuntimeException("stack is emptyp");
}
return (E) es[0];
}
public synchronized void push(E e) {
beforePop();
for (int i = es.length - 1; i >= 0; i--) {
if (es[i] != null) {
es[i + 1] = es[i];
}
}
es[0] = e;
}
@SuppressWarnings("unchecked")
public synchronized E pop() {
if (isEmpty()) {
throw new RuntimeException("stack is emptyp");
}
E ret = (E) es[0];
remove();
return ret;
}
private void remove() {
for (int i = 0; i < es.length - 1; i++) {
es[i] = es[i + 1];
}
es[es.length - 1] = null;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyStack<String> t = new MyStack<String>();
// t.push("abc");
t.push("def");
t.push("xyz");
t.push("uvw");
// t.push("hij");
System.out.println(t.pop());
System.out.println(t.peek());
}
}
------解决思路----------------------
你多次提到Stack<E>这个是吧,你去看看java泛型就知道这个e是什么意思了,这个E是要你去设置的数据类型。。。