Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我如何加上“;及;在最后一位作者之前';她姓什么?_Java - Fatal编程技术网

Java 我如何加上“;及;在最后一位作者之前';她姓什么?

Java 我如何加上“;及;在最后一位作者之前';她姓什么?,java,Java,如何添加单词“and”来分隔列表的最后两个名称?使用带索引的for循环 public String Cite() { String authorsList = ""; Collections.sort(authors); for(Author a: authors) { authorsList += a.firstName.toUpperCase().charAt(0) + ". " + a.lastName + ", "; }

如何添加单词“and”来分隔列表的最后两个名称?

使用带索引的
for
循环

    public String Cite()
{
    String authorsList = "";
    Collections.sort(authors);
    for(Author a: authors)
    {
        authorsList += a.firstName.toUpperCase().charAt(0) + ". " + a.lastName + ", ";
    }

    String cite = authorsList + "\"" + title + "\", " + venue + "(" + getAcronym() + ")" + " , " + 
    publisher;
    return cite;     
}
for(int i=0;i
您应该为循环使用正常的
,这样您就可以检测到您位于第一个和/或最后一个元素上

其他变化:

  • 删除最后一位作者之后的

  • 使用
    StringBuilder
    构建
    字符串

牛津逗号 当然,你是否想要
之前的
逗号和
(牛津逗号)完全取决于你自己

List<Author> authors = new ArrayList<>(List.of(
        new Author("Stephen", "King"),
        new Author("John", "Grisham"),
        new Author("William", "Shakespeare"),
        new Author("Charles", "Dickens") ));
Collections.sort(authors);

StringBuilder buf = new StringBuilder();
for (int i = 0; i < authors.size(); i++) {
    Author a = authors.get(i);
    if (i != 0)
        buf.append(i < authors.size() - 1 ? ", " : " and ");
    buf.append(a.firstName.toUpperCase().charAt(0) + ". " + a.lastName);
}
String authorsList = buf.toString();

System.out.println(authorsList);

更新

由于此时所有其他4个答案对单个作者的结果都不好,下面是对不同数量作者的测试结果

全面测试
公共静态字符串引用(ArrayList作者){
字符串authorsList=“”;
Collections.sort(authors,new CustomComperator());
int size=authors.size();
整数计数=0;
for(作者a:作者){
如果(大小==1){
authorsList=a.firstName.toUpperCase().charAt(0)+“+a.lastName;
}
否则如果(计数==大小-2){
authorsList+=a.firstName.toUpperCase().charAt(0)+“+a.lastName;
}
else if(计数==大小-1){
authorsList+=”和“+a.firstName.toUpperCase().charAt(0)+”+a.lastName;
}
否则{
authorsList+=a.firstName.toUpperCase().charAt(0)+“+a.lastName+”,“;
}
计数++;
}
返回作者列表;
}

如果要在没有循环的情况下执行相同的操作,可以:

Author
类中定义
toString
或类似方法:

public static String Cite(ArrayList<Author> authors){
        String authorsList = "";
        Collections.sort(authors, new CustomComperator());
        int size = authors.size();
        int count = 0;
        for(Author a: authors) {
            if (size ==1) {
                authorsList = a.firstName.toUpperCase().charAt(0) + ". " + a.lastName;
            }

            else if (count == size-2) {
               authorsList +=  a.firstName.toUpperCase().charAt(0) + ". " + a.lastName ;
            }
            else if (count == size - 1) {
                authorsList += " and " + a.firstName.toUpperCase().charAt(0) + ". " + a.lastName ;
            }

            else{
                authorsList += a.firstName.toUpperCase().charAt(0) + ". " + a.lastName + ", ";
            }
           count ++;
        }
        return authorsList;     
    }
并使用创建初始列表(逗号分隔)


输出
p.Neruda、J.Rowling和J.Verne

尝试对
使用
,而不是对每个
使用
。e、 g.
for(int i=0;i
。迭代到最后一个作者。然后,只需执行
”和“+authors.get(authors.size()-1)
。您需要验证作者列表中是否至少有两位作者。这个问题是它自己的答案:如果您有一个
n
作者列表,那么您可以用逗号将所有内容连接到n-1,然后添加“and”,后面是最终作者。现在,您正在加入所有这些程序:停止一个。按照建议使用for循环,并小心管理只有一个作者的情况。您可以使用类
StringJoiner
收集器。加入
以达到此目的:加入者只会在最后一个位置将其添加到所有需要的位置。错误:1)编译错误:什么是
a
2) 只有一位作者,结果是“S.King”
——有两位作者,结果是“S.King和J.Grisham”
——有三位作者,结果是“S.King,J.Grisham和W.Shakespeare”
有一位作者,结果是“S.King”和“S.King”,对不起,我错过了角案。我更新了代码。你应该删除
println(size)
语句。谢谢@AndreasOk,现在可以了,所以删除了down vote。虽然结果是正确的,但似乎重复太多,所以我没有投赞成票。很抱歉
        buf.append(i < authors.size() - 1 ? ", " : ", and ");
List<Author> allAuthors = List.of(
        new Author("Stephen", "King"),
        new Author("John", "Grisham"),
        new Author("William", "Shakespeare"),
        new Author("Charles", "Dickens") );
for (int aCount = 0; aCount <= allAuthors.size(); aCount++) {
    List<Author> authors = new ArrayList<>(allAuthors.subList(0, aCount));
    Collections.sort(authors);

    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < authors.size(); i++) {
        Author a = authors.get(i);
        if (i != 0)
            buf.append(i < authors.size() - 1 ? ", " : " and ");
        buf.append(a.firstName.toUpperCase().charAt(0) + ". " + a.lastName);
    }
    String authorsList = buf.toString();

    System.out.println(aCount + ": \"" + authorsList + "\"");
}
public static String Cite(ArrayList<Author> authors){
        String authorsList = "";
        Collections.sort(authors, new CustomComperator());
        int size = authors.size();
        int count = 0;
        for(Author a: authors) {
            if (size ==1) {
                authorsList = a.firstName.toUpperCase().charAt(0) + ". " + a.lastName;
            }

            else if (count == size-2) {
               authorsList +=  a.firstName.toUpperCase().charAt(0) + ". " + a.lastName ;
            }
            else if (count == size - 1) {
                authorsList += " and " + a.firstName.toUpperCase().charAt(0) + ". " + a.lastName ;
            }

            else{
                authorsList += a.firstName.toUpperCase().charAt(0) + ". " + a.lastName + ", ";
            }
           count ++;
        }
        return authorsList;     
    }
class Author{
  String firstName;
  String lastName;
  Author(String firstName,String lastName){
    this.firstName=firstName;
    this.lastName=lastName;
  }
  @Override
  public String toString(){
    return String.format("%s. %s",this.firstName.toUpperCase().charAt(0), this.lastName);
  }
}
List<Author> authors = Arrays.asList(new Author("Jules", "Verne"),
    new Author("Pablo", "Neruda"), new Author("JK", "Rowling"));
authors.sort(Comparator.comparing(a -> a.lastName));
StringBuffer result = new StringBuffer(
    authors.stream().limit(authors.size() - 1).map(a -> a.toString()).collect(
        Collectors.joining(" ,")));
if (authors.size() > 1) {
  result.append(String.format(" and %s", authors.get(authors.size() - 1).toString()));
} else {
  result.append(authors.get(authors.size() - 1).toString());
}
System.out.println(result);