100分请教一个问题:是关于异常处理的 while(!q1.isFull())while(!q1.isEmpty())你的这两句已经决定了肯定不会抛出异常的啦呵呵 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 感觉楼住的程序boolean isEmpty() { if(c.length==0) return true ; else return false ; }用不到呀!!!不可能为空. 我把程序的功能和大家说一下,用数组实现一个char队列类,该类的构造函数传递一个int类型的参数,指出队列的大小,操作有put和get,队列为满时再put或队列为空时再get,要用异常处理(定义自己的异常类)。 public static void main(String[] args) { CharQueue q1 = new CharQueue(100) ; //CharQueue q2 = new CharQueue(4) ; try { for(char i='a'; i<='z'; i++) q1.put(i) ; }catch(FullQueueException e) { System.out.println(e.toString()) ; } try { for(char i='a'; i<='z'; i++) System.out.print(q1.get()) ; }catch(EmptyQueueException ee) { System.out.println(ee.toString()) ; } } 把while()判断语句去掉~~没有什么用。尽管在执行put方法时可能出现异常情况,但是后面有catch语句进行捕获。如果有while的话,那么put方法中就不可能出现异常,所以就不会执行catch语句; if(c.length>=capacity)你数组一但定义了.他的c.length就是capacity.所以他无论什么情况都相等的. 楼上的有道理这样就OK了boolean isEmpty() { if(back==0) return true ; else return false ; } boolean isFull() { if(back==capacity+1) return true ; else return false ; } import java.util.* ;import java.io.* ;class TestCharQueue{ public static void main(String[] args) { CharQueue q1 = new CharQueue(100) ; //CharQueue q2 = new CharQueue(4) ; try { //while(!q1.isFull()) //{ for(char i='a'; i<='z'; i++) q1.put(i) ; //} }catch(FullQueueException e) { System.out.println(e.toString()) ; } try { for(char i='a'; i<=200; i++) System.out.print(q1.get()) ; }catch(EmptyQueueException ee) { System.out.println(ee.toString()) ; } }}class CharQueue{ int top=0; int capacity ; char[] c ; public CharQueue(int capacity) { this.capacity = capacity ; c = new char[capacity] ; } boolean isEmpty() { if(top==0) return true ; else return false ; } boolean isFull() { if(top>=capacity) return true ; else return false ; } public void put(char c1) throws FullQueueException//入队列 { if(isFull()) throw new FullQueueException() ; else c[top++] = c1 ; } public char get() throws EmptyQueueException//出队列 { char c1 ; if(isEmpty()) throw new EmptyQueueException() ; else c1 = c[top--] ; return c1 ; }}class EmptyQueueException extends Exception{ CharQueue sourceQueue ; public EmptyQueueException() { } public EmptyQueueException(CharQueue q) { super("队列已空。") ; sourceQueue = q ; } public String toString() { return("队列" + "已经为空,执行出队操作引发异常") ; }}class FullQueueException extends Exception{ CharQueue sourceQueue ; public FullQueueException() { } public FullQueueException(CharQueue q) { super("队列已满") ; sourceQueue = q ; } public String toString() { return("队列" + "队列已满,执行入队操作引发异常") ; }}你调式一下看是否符合你的要求??? vgvg,你是pku的吗?如果是的话,偶们一个系的了,呵呵 import java.util.* ;import java.io.* ;class TestCharQueue{ public static void main(String[] args) { CharQueue q1 = new CharQueue(100) ; CharQueue q2 = new CharQueue(4) ; try { for(char i='a'; i<='z'; i++) q1.put(i) ; } catch(FullQueueException e1) { System.out.println(e1.toString()) ; } try { for(char i='a'; i<='z'; i++) System.out.print(q1.get()) ; } catch(EmptyQueueException e2) { System.out.println(e2.toString()) ; } System.out.println("\n") ; System.out.println("下面输出q2队列的字符") ; System.out.println("\n") ; try { q2.put('z') ; q2.put('y') ; q2.put('x') ; q2.put('w') ; q2.put('v') ; } catch(FullQueueException e3) { System.out.println(e3.toString()) ; } try { System.out.print(q2.get()) ; System.out.print(q2.get()) ; System.out.print(q2.get()) ; System.out.print(q2.get()) ; //System.out.print(q2.get()) ; } catch(EmptyQueueException e4) { System.out.println(e4.toString()) ; } }}class CharQueue{ int front = 0 ; int back = 0 ; int capacity ; char[] c ; public CharQueue(int capacity) { this.capacity = capacity ; c = new char[capacity] ; } boolean isEmpty() { if(back==0) return true ; else return false ; } boolean isFull() { if(back==capacity+1) return true ; else return false ; } public void put(char c1) throws FullQueueException//入队列 { if(isFull()) throw new FullQueueException(this) ; else c[back++] = c1 ; } public char get() throws EmptyQueueException//出队列 { char c1 ; if(isEmpty()) throw new EmptyQueueException(this) ; else c1 = c[front++] ; if(2*front>=capacity) { for(int i=0; i<back-front; i++) c[i] = c[i+front] ; back -=front ; front = 0 ; } return c1 ; }}class EmptyQueueException extends Exception{ CharQueue sourceQueue ; public EmptyQueueException() { } public EmptyQueueException(CharQueue q) { super("队列已空。") ; sourceQueue = q ; } public String toString() { return("队列" + sourceQueue.toString() + "已经为空,执行出队操作引发异常") ; }}class FullQueueException extends Exception{ CharQueue sourceQueue ; public FullQueueException() { } public FullQueueException(CharQueue q) { super("队列已满") ; sourceQueue = q ; } public String toString() { return("队列" + sourceQueue.toString() + "队列已满,执行入队操作引发异常") ; }}为什么捉不到我定义的异常信息啊? 问题解决了,呵呵boolean isFull() { if(back>=capacity) return true ; else return false ; } 另外100分我在下面这个帖子和你接,你们到那边只要up就好了http://community.csdn.net/Expert/topic/3636/3636721.xml?temp=.2500879 java虚拟机异常 谁能帮我看看这是什么问题 问一个关于protected修饰符的问题。 java 动态数据库连接 有问题请教 一个简单的输入对话框的问题 急!菜鸟新手MM求救!画出正弦,余弦,正切,余切的图形 关于DOM的基本概念,谢谢! 100分 请教一个关于JCheckBOX的问题!!! 处理CLOB字段遇到的问题,test.txt大于700多字节就不行,600多字节可以。好郁闷 java怎么入门啊?各位用的是什么操作系统? 帮帮忙 高手来帮忙~~谢谢
boolean isEmpty()
{
if(c.length==0)
return true ;
else
return false ;
}
用不到呀!!!不可能为空.
{
CharQueue q1 = new CharQueue(100) ;
//CharQueue q2 = new CharQueue(4) ;
try
{
for(char i='a'; i<='z'; i++)
q1.put(i) ;
}catch(FullQueueException e)
{
System.out.println(e.toString()) ;
}
try
{
for(char i='a'; i<='z'; i++)
System.out.print(q1.get()) ;
}catch(EmptyQueueException ee)
{
System.out.println(ee.toString()) ;
}
}
没有什么用。
尽管在执行put方法时可能出现异常情况,但是后面有catch语句进行捕获。
如果有while的话,那么put方法中就不可能出现异常,所以就不会执行catch语句;
你数组一但定义了.他的c.length就是capacity.所以他无论什么情况都相等的.
这样就OK了
boolean isEmpty()
{
if(back==0)
return true ;
else
return false ;
}
boolean isFull()
{
if(back==capacity+1)
return true ;
else
return false ;
}
import java.io.* ;
class TestCharQueue
{
public static void main(String[] args)
{
CharQueue q1 = new CharQueue(100) ;
//CharQueue q2 = new CharQueue(4) ;
try
{ //while(!q1.isFull())
//{
for(char i='a'; i<='z'; i++)
q1.put(i) ;
//}
}catch(FullQueueException e)
{
System.out.println(e.toString()) ;
}
try
{
for(char i='a'; i<=200; i++)
System.out.print(q1.get()) ;
}catch(EmptyQueueException ee)
{
System.out.println(ee.toString()) ;
}
}
}
class CharQueue
{ int top=0;
int capacity ;
char[] c ;
public CharQueue(int capacity)
{
this.capacity = capacity ;
c = new char[capacity] ;
}
boolean isEmpty()
{
if(top==0)
return true ;
else
return false ;
}
boolean isFull()
{
if(top>=capacity)
return true ;
else
return false ;
}
public void put(char c1) throws FullQueueException//入队列
{
if(isFull())
throw new FullQueueException() ;
else
c[top++] = c1 ;
}
public char get() throws EmptyQueueException//出队列
{ char c1 ;
if(isEmpty())
throw new EmptyQueueException() ;
else
c1 = c[top--] ;
return c1 ;
}
}class EmptyQueueException extends Exception
{
CharQueue sourceQueue ;
public EmptyQueueException()
{
}
public EmptyQueueException(CharQueue q)
{
super("队列已空。") ;
sourceQueue = q ;
}
public String toString()
{
return("队列" + "已经为空,执行出队操作引发异常") ;
}
}class FullQueueException extends Exception
{
CharQueue sourceQueue ;
public FullQueueException()
{
}
public FullQueueException(CharQueue q)
{
super("队列已满") ;
sourceQueue = q ;
}
public String toString()
{
return("队列" + "队列已满,执行入队操作引发异常") ;
}
}
你调式一下看是否符合你的要求???
如果是的话,偶们一个系的了,呵呵
import java.io.* ;
class TestCharQueue
{
public static void main(String[] args)
{
CharQueue q1 = new CharQueue(100) ;
CharQueue q2 = new CharQueue(4) ;
try
{
for(char i='a'; i<='z'; i++)
q1.put(i) ;
}
catch(FullQueueException e1)
{
System.out.println(e1.toString()) ;
}
try
{
for(char i='a'; i<='z'; i++)
System.out.print(q1.get()) ;
}
catch(EmptyQueueException e2)
{
System.out.println(e2.toString()) ;
}
System.out.println("\n") ;
System.out.println("下面输出q2队列的字符") ;
System.out.println("\n") ;
try
{
q2.put('z') ;
q2.put('y') ;
q2.put('x') ;
q2.put('w') ;
q2.put('v') ;
}
catch(FullQueueException e3)
{
System.out.println(e3.toString()) ;
}
try
{
System.out.print(q2.get()) ;
System.out.print(q2.get()) ;
System.out.print(q2.get()) ;
System.out.print(q2.get()) ;
//System.out.print(q2.get()) ;
}
catch(EmptyQueueException e4)
{
System.out.println(e4.toString()) ;
}
}
}
class CharQueue
{
int front = 0 ;
int back = 0 ;
int capacity ;
char[] c ;
public CharQueue(int capacity)
{
this.capacity = capacity ;
c = new char[capacity] ;
}
boolean isEmpty()
{
if(back==0)
return true ;
else
return false ;
}
boolean isFull()
{
if(back==capacity+1)
return true ;
else
return false ;
}
public void put(char c1) throws FullQueueException//入队列
{
if(isFull())
throw new FullQueueException(this) ;
else
c[back++] = c1 ;
}
public char get() throws EmptyQueueException//出队列
{ char c1 ;
if(isEmpty())
throw new EmptyQueueException(this) ;
else
c1 = c[front++] ;
if(2*front>=capacity)
{
for(int i=0; i<back-front; i++)
c[i] = c[i+front] ;
back -=front ;
front = 0 ;
}
return c1 ;
}
}class EmptyQueueException extends Exception
{
CharQueue sourceQueue ;
public EmptyQueueException()
{
}
public EmptyQueueException(CharQueue q)
{
super("队列已空。") ;
sourceQueue = q ;
}
public String toString()
{
return("队列" + sourceQueue.toString() + "已经为空,执行出队操作引发异常") ;
}
}class FullQueueException extends Exception
{
CharQueue sourceQueue ;
public FullQueueException()
{
}
public FullQueueException(CharQueue q)
{
super("队列已满") ;
sourceQueue = q ;
}
public String toString()
{
return("队列" + sourceQueue.toString() + "队列已满,执行入队操作引发异常") ;
}
}为什么捉不到我定义的异常信息啊?
{
if(back>=capacity)
return true ;
else
return false ;
}