Java 在数组中除最后一个以外的项目之间插入分号

Java 在数组中除最后一个以外的项目之间插入分号,java,loops,Java,Loops,基于此,我有以下代码: if (authors.length >= 1) { System.out.print(authors[0]); } for (int i = 1; i < authors.length; i++) { System.out.print("; " + authors[i]); } 所以这个的输出是author1;作者2;作者3 如何将其更改为author1;author2和author3?如果只有两个作者,则输出应为author1和auth

基于此,我有以下代码:

if (authors.length >= 1) {
    System.out.print(authors[0]);
}

for (int i = 1; i < authors.length; i++) {
    System.out.print("; " + authors[i]);
}
所以这个的输出是author1;作者2;作者3
如何将其更改为author1;author2和author3?如果只有两个作者,则输出应为author1和author2。提前感谢。

您只需在循环中添加一个条件即可处理最后一种情况:

for (int i = 1; i < authors.length; i++) {
    if(i == authors.length - 1)
        System.out.print("& " + authors[i]);
    else
        System.out.print("; " + authors[i]);
 }

您只需在循环中添加一个条件,即可处理最后一种情况:

for (int i = 1; i < authors.length; i++) {
    if(i == authors.length - 1)
        System.out.print("& " + authors[i]);
    else
        System.out.print("; " + authors[i]);
 }

一种方法是更改代码结构,使用循环和布尔标志代替条件,如下所示:

boolean isFirst = true;
for (int i = 0 ; i != authors.length ; i++) {
    if (!isFirst) {
        System.out.print(i == authors.length-1 ? "& " : "; ");
    } else {
         isFirst = false;
    }
    System.out.print(authors[i]);
}

一种方法是更改代码结构,使用循环和布尔标志代替条件,如下所示:

boolean isFirst = true;
for (int i = 0 ; i != authors.length ; i++) {
    if (!isFirst) {
        System.out.print(i == authors.length-1 ? "& " : "; ");
    } else {
         isFirst = false;
    }
    System.out.print(authors[i]);
}

您可以递归地将案例清楚地分开。似乎其他答案都缺少这一点

这是代理函数:

public static String doIt(String[] authors){

    if (authors == null || authors.length == 0){
        return "";
    }

    if (authors.length == 1){
        return authors[0];
    }

    return authors[0] + doHelper(authors, 1);

}
public static String doItHelper(String[] authors, int index){
    if (index == authors.length - 1){
        return " & " + authors[index];
    }
    return "; " + authors[index] + doItHelper(authors, index + 1);

}
和助手函数:

public static String doIt(String[] authors){

    if (authors == null || authors.length == 0){
        return "";
    }

    if (authors.length == 1){
        return authors[0];
    }

    return authors[0] + doHelper(authors, 1);

}
public static String doItHelper(String[] authors, int index){
    if (index == authors.length - 1){
        return " & " + authors[index];
    }
    return "; " + authors[index] + doItHelper(authors, index + 1);

}
如评论中所述,感谢@JNYRanger,当性能成为一个问题时,这不是最优的


现在无法测试它,所以我希望想法是明确的。

您可以递归地进行测试,以清晰地分离案例。似乎其他答案都缺少这一点

这是代理函数:

public static String doIt(String[] authors){

    if (authors == null || authors.length == 0){
        return "";
    }

    if (authors.length == 1){
        return authors[0];
    }

    return authors[0] + doHelper(authors, 1);

}
public static String doItHelper(String[] authors, int index){
    if (index == authors.length - 1){
        return " & " + authors[index];
    }
    return "; " + authors[index] + doItHelper(authors, index + 1);

}
和助手函数:

public static String doIt(String[] authors){

    if (authors == null || authors.length == 0){
        return "";
    }

    if (authors.length == 1){
        return authors[0];
    }

    return authors[0] + doHelper(authors, 1);

}
public static String doItHelper(String[] authors, int index){
    if (index == authors.length - 1){
        return " & " + authors[index];
    }
    return "; " + authors[index] + doItHelper(authors, index + 1);

}
如评论中所述,感谢@JNYRanger,当性能成为一个问题时,这不是最优的

现在无法测试,因此我希望想法是明确的。

请尝试以下方法:

    String[] authors = { "1", "2", "3", "4", "5" };

    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < authors.length; i++) {

        sb.append(authors[i]);
        if (i + 2 < authors.length) {
            sb.append(";");
        } else if (i + 2 == authors.length) {
            sb.append("&");
        }
    }
    System.out.print(sb.toString());
试着这样做:

    String[] authors = { "1", "2", "3", "4", "5" };

    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < authors.length; i++) {

        sb.append(authors[i]);
        if (i + 2 < authors.length) {
            sb.append(";");
        } else if (i + 2 == authors.length) {
            sb.append("&");
        }
    }
    System.out.print(sb.toString());


那么这不是一个无限循环,在i>authors.length之后继续吗?或者,它是否会在不满足条件的第一个实例中中断循环?我认为我和作家是平等的。length@CalebB这将在第一次不满足条件时中断,它将不是无限的。一旦从循环表达式计算出false,所有循环都将停止迭代。@caleb无法使数组作者将此循环变成无限循环。打破我的唯一方法!=authors.length将使authors.length为负值,这当然是不可能的。它是如何实现的?:又打电话来了?我知道这与布尔运算有关。@WonderWorld这是一个条件运算符,也称为三元运算符。那么这不是一个无限循环,在I>authors.length之后继续吗?或者,它是否会在不满足条件的第一个实例中中断循环?我认为我和作家是平等的。length@CalebB这将在第一次不满足条件时中断,它将不是无限的。一旦从循环表达式计算出false,所有循环都将停止迭代。@caleb无法使数组作者将此循环变成无限循环。打破我的唯一方法!=authors.length将使authors.length为负值,这当然是不可能的。它是如何实现的?:又打电话来了?我知道它与布尔运算有关。@WonderWorld这是一个条件运算符,也称为三元运算符。在Java中,对一个极其简单的算法使用递归是非常不必要和浪费的。如果这是Haskell,那么这将是一个好主意……但是对于Java来说,不。@JNYRanger您正在优化不应该优化的内容。这是浪费。@ReutSharabani占用资源和编写智能代码是如何浪费的?您可以编写一个字符串,每次添加一个字符,但为什么要这样做?@CalebB如果这样做有效且易于理解,为什么要使用其他方法?你要把数百万作者串联起来吗?您是否遇到性能问题?如果你这样做了,你应该为优化而烦恼。可能不是这样,你认为循环更容易,而我认为是这样。这很公平。对于这个微不足道的例子来说,这两种方式都无关紧要。干杯在Java中,对一个极其简单的算法使用递归是非常不必要和浪费的。如果这是Haskell,那么这将是一个好主意……但是对于Java来说,不。@JNYRanger您正在优化不应该优化的内容。这是浪费。@ReutSharabani占用资源和编写智能代码是如何浪费的?您可以编写一个字符串,每次添加一个字符,但为什么要这样做?@CalebB如果这样做有效且易于理解,为什么要使用其他方法?你要把数百万作者串联起来吗?您是否遇到性能问题?如果你这样做了,你应该为优化而烦恼。可能不是这样,你认为循环更容易,而我认为是这样。这很公平。对于这个微不足道的例子来说,这两种方式都无关紧要。干杯