Java 返回存储在数组中的数组长度

Java 返回存储在数组中的数组长度,java,arrays,arraylist,Java,Arrays,Arraylist,我试图在代码中使用不同的方法调用和子字符串值等简单地返回存储的数组值。我的问题是,在终端输出中,我的数组长度是一个长度过大的值 7个字,存储在ArrayList中 apple bannana peach plum orange lime apple peach plum lime apple bannana peach plum orange lime apple peach plum lime 5个字,存储在ArrayList中 apple bannana peach plum ora

我试图在代码中使用不同的方法调用和子字符串值等简单地返回存储的数组值。我的问题是,在终端输出中,我的数组长度是一个长度过大的值

7个字,存储在ArrayList中

apple
bannana
peach
plum
orange
lime
apple
peach
plum
lime
apple
bannana
peach
plum
orange
lime
apple
peach
plum
lime
5个字,存储在ArrayList中

apple
bannana
peach
plum
orange
lime
apple
peach
plum
lime
apple
bannana
peach
plum
orange
lime
apple
peach
plum
lime
当它看起来像这样时:

6个字,存储在ArrayList中

apple
bannana
peach
plum
orange
lime
apple
peach
plum
lime
apple
bannana
peach
plum
orange
lime
apple
peach
plum
lime
4个单词,存储在ArrayList中

apple
bannana
peach
plum
orange
lime
apple
peach
plum
lime
apple
bannana
peach
plum
orange
lime
apple
peach
plum
lime
所以我在想我可能会错过什么。 我的代码如下所示:

/**
 * Create an ArrayList<String> instance, and
 * assign it to wordList.
 */
public Words( )
{
    this.wordList = new ArrayList<String>();
    populate( );
}

//     public ArrayList<String> getWordList( )
//     {
//         return this.wordList;
//     }

/**
 * returns the size of wordList
 * 
 * @return int
 */
public int count( )
{
    return wordList.size()-1;
}

/**
 * Done
 */
public void populate( )
{
    String[ ] spellings = 
        { 
            new String( "" ),
            new String( "1234" ),

            "a99le",

            "apple",               
            "bannana",
            "peach",

            new String( "plum" ),

            "orange",
            "lime"
        };

    for (String s: spellings)
    {
        this.addWord( s );

    }
}

/*
 * Creates and returns a String[ ] array of all String elements
 * that are included in the wordList field.
 * 
 * @return a String[ ] array
 */
public String[ ] copyToWordArray( )
{
    String[]wordArray = new String[wordList.size()];

    for (int n = 0; n < wordArray.length; n++ )
    {
        wordArray[n] = wordList.get(n);
    }
    return wordArray;
}

/*
 * Creates and returns an ArrayList of all String elements 
 * in wordList that contain the given substring.
 * 
 * @return ArrayList<String>
 * @param String substring
 */
public ArrayList<String> contains( String substring )
{
    ArrayList<String> list = new ArrayList<String>();
    for (String s: wordList)
    {
        if (s.contains(substring))
        {
            list.add(s);
        }
    }

    return list;
}

/*
 * Creates and returns an ArrayList of all String elements
 * in wordList that start with the given prefix.
 * 
 * @return ArrayList<String>
 * @param  String prefix
 */
public ArrayList<String> startsWith( String prefix )
{
    ArrayList<String> list = new ArrayList<String>();

    for (String s: wordList)
    {
        if (s.startsWith(prefix))
        {
            list.add(s);
        }
    }

    return list;
}

/**
 * Searches wordList with a for-each loop, and 
 * returns the word if it is found. Otherwise,
 * returns null.
 * 
 * @return String
 * @param String word
 */
public String find( String word )
{
    for (String s: wordList)
    { 
        if (s.equals(word))
        {
            return s;
        }
    }
    return null;
}

/**
 *  For a word to be valid:
 *    1) word.length() is postive,
 *    2) word contains alphabetic characters exclusively.
 *    
 *  This method uses a for loop to examine each character 
 *  in the given word.
 *  
 *  it returns:
 *    - false for the first non-alphabetic character detected. 
 *    - true if all characters in the given word are strictly
 *      alphabetic, and the word length is positive.
 *  
 *  @return true or false
 *  @param  String str
 */
private boolean isValidWord( String str )
{
    if (str.length() > 0 && str!= null)
    {
        for (int i = 0; i < str.length(); i++)
        {
            if (!Character.isLetter(str.charAt(i)))
            {
                return false;
            }
        }
    }
    return true;
}

/**
 * Calls addWord( s ) for each element s in the String[ ] array
 * to add them to wordList.
 * 
 * @param String[ ] spellings
 */
public void addWords( String[ ] spellings )
{
    for (String s: spellings)
    {
        wordList.add(s);
    }
}

/**
 *  This method calls the find method to determine
 *  whether the given word is already in wordList.
 *  There are two cases: find returns either 
 *  1) null -- word not found in wordList -- or 
 *  2) the word -- the one that it found.
 *  

 *     
 *
 *  
 *  @param  String word
 */
public void add( String word )
{
    String w = this.find( word );

    if (w == null)
    {
        wordList.add(word);

    }
    return;
}

/**
 *  If the given word is valid, this method calls the add( word ) method. 
 * 
 *  
 *  @param  String str
 */
public void addWord( String str )
{
    if (isValidWord( str ))
    {
        this.add(str); 
    }

}

/**
 *  This method calls the find method. Two cases: 
 *  find returns either null or the word that was found.
 *  
 *  If the given word is found, the method removes
 *  it from the wordList, 
 *     
 *  
 *  
 *  @param  String word
 */
public void remove( String word )
{
    String w = this.find( word );

    if (w == null )
    {
        message = "The word cannot be removed from list.";
    }
    else
    {
        wordList.remove(word);
    }
}

/**
 *  This method, on the condition that there is an nth element
 *  in wordList, removes the given word from the location.
 * 
 *  
 *  @param  n
 */
public void remove( int n )
{
    if (n < this.wordList.size( ))
    {

        wordList.remove(n);
    }
}

/**
 * Done
 */
public String toString( )
{
    String str = wordList.size( ) + " words, stored in an ArrayList<String>";
    for (String c: wordList)
    {
        str += "\n\t" + c.toString( );
    }
    return str + "\n";
}


/**
 * Done
 */
public static void main( String[ ] args )
{
    System.out.println( "" );
    Words words = new Words( );

    System.out.println( words.toString( ) );
    System.out.println( );

    words.add( "lemon" );

    words.add( "lemon" );

    words.remove( "lemon" );

    words.remove( "lemon" );
    words.remove( words.count( ) - 1 );

    words.remove( "bannana" );

    System.out.println( words.toString( ) );
    String[ ] wordArray = words.copyToWordArray( );
    System.out.println( wordArray.length + " words, stored in a String[ ] array" );
    for (String w: wordArray)
    {
        System.out.println( "\t" + w.toString( ) );
    }
    System.out.println( );

    ArrayList<String> list = words.contains( "p" );
    System.out.println( list.size( ) + " words that contain letter p" );
    for (String w: list)
    {
        System.out.println( "\t" + w.toString( ) );
    }
    System.out.println( );

    list = words.startsWith( "p" );
    System.out.println( list.size( ) + " words that start with letter p" );
    for (String w: list)
    {
        System.out.println( "\t" + w.toString( ) );
    }
    System.out.println( );
}
/**
*创建ArrayList实例,然后
*将其分配到单词列表。
*/
公开用语()
{
this.wordList=new ArrayList();
填充();
}
//公共ArrayList getWordList()
//     {
//返回此.wordList;
//     }
/**
*返回单词列表的大小
* 
*@return int
*/
公共整数计数()
{
返回wordList.size()-1;
}
/**
*完成
*/
公共空间填充()
{
字符串[]拼写=
{ 
新字符串(“”),
新字符串(“1234”),
“a99le”,
“苹果”,
“班纳纳”,
“桃子”,
新弦(“梅花”),
“橙色”,
“石灰”
};
for(字符串s:拼写)
{
本.增补字(s);
}
}
/*
*创建并返回所有字符串元素的String[]数组
*包含在单词列表字段中的。
* 
*@返回字符串[]数组
*/
公共字符串[]CopyToOrdArray()
{
String[]wordArray=新字符串[wordList.size()];
for(int n=0;n0&&str!=null)
{
对于(int i=0;i