tryclass PlayList 

        int numberOfSong=0;
        int capacity;
        String songArtistTitle;
        PlayList[] list;
        
        public PlayList(int capacityIn)
        {
                capacity=capacityIn;
list=new PlayList[capacity];
        }
        
        public PlayList(String songArtistTitleIn)
        {
                
                if (songArtistTitleIn.length() < 1)
                {
                        System.err.println("bad song artist in constructor") ;
                System.exit(1) ;
        }
                songArtistTitle=songArtistTitleIn;
        }
            
        public boolean addSong(String songArtistTitleIn)
        {
                songArtistTitle=songArtistTitleIn;
                if(numberOfSong>=0 && numberOfSong<=(capacity-1))
                {
                        list[numberOfSong]=new PlayList(songArtistTitle);
                        numberOfSong++;
                        return true;
                }
                else
                    
                        return false;
                    
        }
      
        public String pickSong()
        {
                int ran;
                String str;
                if(numberOfSong==0)
                        return null;
                else
                {
                        ran=(int)(Math.random()*numberOfSong);
                        str=list[ran].toString();  
                        for(int j=ran;j<(numberOfSong-1);j++)
                        {
                                list[j]=list[j+1];
                        }
numberOfSong--;
                        return str; 
                }
        }
        public String toString()
        {
                return songArtistTitle;
        }
        public boolean isFull()
        {
                if(numberOfSong==capacity)
                    
                        return true;
                    
                else
                    
                        return false;
                    
        }
        public boolean isEmpty()
        {
                if(numberOfSong==0)
                    
                        return true;
                  
                else
                    
                        return false;
                                  
        }
}
public class PlayListTester
{
  public static void main( String[] args )
  {
    PlayList hitList = new PlayList(10);    if( hitList.isEmpty() )
      System.out.println( "The play list is empty" );    hitList.addSong( "The Rolling Stones: Jumpin' Jack Flash" );
    hitList.addSong( "Manic Street Preachers: Motorcycle Emptiness" );
    hitList.addSong( "Manic Street Preachers: If You Tolerate This" );
    hitList.addSong( "The Kinks: Waterloo Sunset" );
    hitList.addSong( "The Pretenders: Don't Get Me Wrong" );
    hitList.addSong( "Radiohead: Airbag" );
    hitList.addSong( "Johny Cash: San Quentin" );
    hitList.addSong( "Blur: Girls and Boys" ); 
    hitList.addSong( "Pulp: Do You Remember The First Time" );
    hitList.addSong( "The Verve: This is Music" );    System.out.println( "The play list contains..." );
    System.out.println( hitList.toString() );    if( hitList.isFull() )
      System.out.println( "The play list is full" );    if( ! hitList.addSong( "The Smiths: This Charming Man" ) )
      System.out.println( "Couldn't add The Smiths track - full play list" );      while( ! hitList.isEmpty() )
      System.out.println( "Next song to play is: " + hitList.pickSong() );
    
    if( hitList.isEmpty() )
      System.out.println( "The play list is empty" ); 
  }
}    

解决方案 »

  1.   

    按你说的修改之后一部分正  确了,但是还有下面的错误,是程序编的太烂了,不好意思,下面的  怎么回事
    The play list is empty
    The paly list contains...
    The Verve:This is Music
    Couldn't add The Smiths Track-full play list
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
                  at PlayList.pickSong(PlsyList.java:54)
                  at PlayListTester.main(PlayListTester.java:36)
    谢谢
      

  2.   

    try my code, it works, here are the changes:1. 
    PlayList[] list;
            
            public PlayList(int capacityIn)
            {
                    capacity=capacityIn;
    list=new PlayList[capacity];
            }
    2. 
    for(int j=ran;j<(numberOfSong-1);j++)
                            {
                                    list[j]=list[j+1];
                            }
    numberOfSong--;
    3. public boolean isFull()
            {
                    if(numberOfSong==capacity)
                        
                            return true;
                        
                    else
                        
                            return false;
                        
            }
      

  3.   

    太好拉,真的很感谢,谢谢你这么晚还帮我调程序,以后还能跟你联系么,我好象很笨,不  过真的希望以后还能请教您,我的Email:[email protected],能告诉我你的信箱么