java - 与数组列表串联

标签 java arraylist concatenation

这是我的代码。我知道如何使用 concat 运算符,但不知道如何使用数组或 arraylist

import java.util.*;
class stringProject {
    static String concat(String str1, String str2) {
        return str1.concat(str2);
    }

    public static void main(String[] args) 
    {
      ArrayList<String> projectStrings = new ArrayList<String>();
      projectStrings.add("The ");
      projectStrings.add("quick ");
      projectStrings.add("brown ");
      projectStrings.add("fox ");
      projectStrings.add("jumped ");
      projectStrings.add("over ");
      projectStrings.add("the ");
      projectStrings.add("lazy ");
      projectStrings.add("dog.");
    }
    System.out.println(projectStrings.get(1).concat(projectStrings.get(0)));
    }
}

我的错误是:

File: C:\Users\Daniel\stringProject.java  [line: 23]
Error: Syntax error on token "println", Identifier expected after this token
File: C:\Users\Daniel\stringProject.java  [line: 23]
Error: Syntax error on tokens, AnnotationName expected instead
File: C:\Users\Daniel\stringProject.java  [line: 23]
Error: Syntax error on token ")", delete this token
File: C:\Users\Daniel\stringProject.java  [line: 23]
Error: Syntax error, insert "Type VariableDeclaratorId" to complete FormalParameter
File: C:\Users\Daniel\stringProject.java  [line: 23]
Error: Syntax error, insert ")" to complete MethodDeclaration
File: C:\Users\Daniel\stringProject.java  [line: 25]
Error: Syntax error on token "}", delete this token

最佳答案

当前代码中的主要问题:

  1. 您必须定义您正在使用String列表。您可以通过在 ArrayList 声明中添加 String 泛型来实现此目的,否则您将声明一个原始 ArrayList,因此所有元素都将是被视为对象:

    ArrayList<String> projectStrings = new ArrayList<String>();
    

    或 Java 7 方式,使用菱形运算符:

    ArrayList<String> projectStrings = new ArrayList<>();
    
  2. 您尚未在代码中定义 concat 方法。也许您想使用 String#concat :

    System.out.println(projectStrings.get(1).concat(projectStrings(0)));
    

连接String的另一种方法是使用+符号:

System.out.println(projectStrings.get(1) + projectStrings(0));

以防万一您希望当前代码正常工作,请在 main 方法外部定义一个 concat 方法:

class stringProject {
    //this method should be static in order to be used in other static methods like main
    static String concat(String str1, String str2) {
        //naive implementation
        return str1.concat(str2);
    }

    public static void main(String[] args) {
        //your current code here...
        //since you have defined a concat method, you can use it with no problems
        System.out.println(concat(projectStrings.get(1), projectStrings.get(0)));
    }
}

另一个建议:你不需要添加import java.lang.*,这是编译器默认添加的,所以你可以删除这一行。

<小时/>

代码编辑后,您在打印输出之前关闭了 main 方法。这就是您的代码现在的样子:

public static void main(String[] args) 
{ //this is where your main method starts
  ArrayList<String> projectStrings = new ArrayList<String>();
  projectStrings.add("The ");
  projectStrings.add("quick ");
  projectStrings.add("brown ");
  projectStrings.add("fox ");
  projectStrings.add("jumped ");
  projectStrings.add("over ");
  projectStrings.add("the ");
  projectStrings.add("lazy ");
  projectStrings.add("dog.");
} //this is where your main method ends
//this line is outside any method, thus you get those errors
System.out.println(projectStrings.get(1).concat(projectStrings.get(0)));
} //this is where your stringProject class definition ends

解决方案:只需将 System.out.println(...) 行移到 main 方法中即可:

public static void main(String[] args) 
{ //this is where your main method starts
  ArrayList<String> projectStrings = new ArrayList<String>();
  projectStrings.add("The ");
  projectStrings.add("quick ");
  projectStrings.add("brown ");
  projectStrings.add("fox ");
  projectStrings.add("jumped ");
  projectStrings.add("over ");
  projectStrings.add("the ");
  projectStrings.add("lazy ");
  projectStrings.add("dog.");
  //easy fix
  System.out.println(projectStrings.get(1).concat(projectStrings.get(0)));
} //this is where your main method ends
} //this is where your stringProject class definition ends

关于java - 与数组列表串联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21337069/

相关文章:

java - Tosca Angular table 转向

java - 如何在 Android java 代码中同时使用 CordovaActivity 和 AppCompatActivity?

java - 连接表并将结果保存在 ArrayList-Attribute 中

java - 我如何知道是使用数组还是数组列表?

audio - 如何无损连接 ogg vorbis 文件?

c - c : "..."". ..”中一个接一个的字符串,就好像它们是串联的一样

java - Hibernate CriteriaBuilder 将多行连接成一行

java - 将 jacoco.exec 文件加载到 Sonar 中以进行多模块项目

java - Maven 指定编译时插件

java - 如何从两个数组列表中删除不常见的元素