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

时间:2015-12-02 22:26:00

标签: java arrays arraylist

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

7个字,存储在ArrayList

apple
bannana
peach
plum
orange
lime

5个字,存储在ArrayList

apple
peach
plum
lime

什么时候应该是这样的:

6个字,存储在ArrayList

apple
bannana
peach
plum
orange
lime

4个字,存储在ArrayList

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( );
}

}

这可能是一件非常简单的事情,我想念但是我没有抓住它 - 提前谢谢你。

3 个答案:

答案 0 :(得分:1)

我认为问题可能出在addWords方法中。在所有其他add方法中,您已检查传入的字符串是否有效,但是这个方法只是直接从传入的数组中添加字符串。这是在填充ArrayList时使用的方法。从构造函数调用的populate方法。

populate方法将空字符串添加到添加到ArrayList的spellings数组中。如果检查传递给addWords的数组中字符串的有效性,则不会将此空字符串添加到ArrayList中。

答案 1 :(得分:1)

这比我想象的要快。正如我在评论中所假设的那样,你的数组中有一个空字符串。在populate()方法中观察您添加到内部列表的第一个String

    new String( "" ),

即空字符串。据我所知,你永远不会删除它。当你打印出内容时,你会计算并打印那个元素,但你显然会错过它,因为相应的输出采用空行的形式。

如果您将空字符串放在列表中间的某个位置,那么您将更容易看到它。或者,您可以在单词周围输出引号,或者执行其他一些允许您将输出单词与普通空格区分开来的内容。

答案 2 :(得分:0)

您将计数定义为worList.size()-1,其时间应为wordlist.size();

public int count( )
{
    return wordList.size()-1;
}