1

我对使用省略号的想法很陌生。我几乎可以肯定我的错误是由不正确地声明或初始化“ String[] authors”引起的,但我不知道如何做到这一点,我的setAuthors方法仍然有效。

import java.util.*;
public class Book {
    private String[] authors; //I'm guessing this line should end with "= new String..."
                              //but not sure how to w/o specifying an array dimension
    private int authorsSize;

    //Receives variable # of String parameters and indices them into String[] "authors"
    public void setAuthors(String... authors) {
        authorsSize = authors.length;
        for(int i=0;i<authorsSize;i++)
            this.authors[i] = authors[i];
    }

//getAuthors 方法:

    public String getAuthors(){
         String s = "";
         authorsSize = authors.length;
              for(int i=0;i<authorsSize;i++)
              s = s+authors[i] + ", ";
         printAuthors = s;
         return s;
    }
4

5 回答 5

7

简单的方法就是克隆数组:

public void setAuthors(String... authors){
    this.authors = (String[]) authors.clone();
}

毕竟你反正是在覆盖之前的数据,在方法调用之前你是无法知道大小的。此时您不需要该authorsSize变量 - 您已经获得了authors知道其自身长度的数组。

(当然,如果您能够使用不可变集合,您甚至不需要克隆。)

编辑:如评论中所述,如果您传入空引用,此方法将引发异常。你不应该自动决定这是你应该“处理”的情况——记录参数不能为空是完全合法的。我建议以任何一种方式记录围绕无效性的行为。

事实上,如果你这样做,你可能想像这样初始化你的实例字段:

private String[] authors = new String[0];

这样,您始终知道您将在该字段中有一个非空引用,因此您可以不受惩罚地使用它。我宁愿这样,每次使用时都必须检查 null。

于 2012-11-16T08:19:09.073 回答
3

你从来没有初始化authors你的数组。您需要在使用它之前对其进行初始化。

String[] authors = new String[size];

//但不确定如何不指定数组维度

最好的方法是使用 List 实现类,因为它们是动态数组。即,您不需要指定大小。

List<String> authors = new ArrayList<String>();
于 2012-11-16T08:19:10.017 回答
1

因为“private String[] authors”的声明在“public void setAuthors(String... authors)”之前,所以不能使用“String[] authors = new String[authorsSize]”这样的格式。这将使作者的大小始终为 0。

更好的方法是使用动态初始化: List authors = new ArrayList(); 然后使用 this.authors.add(authors[i]) 传递参数。

于 2012-11-16T09:17:10.017 回答
1

您必须按setAuthors如下所述更正您的方法

public void setAuthors(String... authors) {
    if (authors != null && authors.length > 0) {
        authorsSize = authors.length;
        authors = new String[authorsSize];
        for (int i = 0; i < authorsSize; i++)
            this.authors[i] = authors[i];
    }else{
        this.authors = null;
    }
}
于 2012-11-16T08:22:04.603 回答
0

你也可以像这样调整你的代码:

import java.util.*;
public class Book{

    private String[] authors; 
    private int authorsSize;

    public void setAuthors(String... authors){

        //check for null, you could also set this.authors = new String[] if you prefer.
        if(authors == null){
            this.authors = null;
        }else{
            authorsSize = authors.length;

            //also add this line...
            this.authors = new String[authorsSize];

            for(int i=0;i<authorsSize;i++)
                this.authors[i] = authors[i];
        }
    }

    public String getAuthors(){
         if(authors == null){
             return ""; //could also return null here if you would prefer
         }

         StringBuilder s = new StringBuilder();
         authorsSize = authors.length;
         for(int i=0;i<authorsSize;i++){
             if(i > 0)
                 s.append(",");

             s.append(authors[i]);
         }

         //printAuthors = s;
         return s.toString();
    }
}
于 2012-11-16T08:27:26.033 回答