【问题标题】:Concatenation with arraylist与数组列表连接
【发布时间】:2014-02-15 16:57:40
【问题描述】:

这是我的代码。我知道如何使用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

【问题讨论】:

  • System.out.println(projectStrings.get(1).concat(projectStrings.get(0)));?
  • 对不起,我一直在关注错误消息,我不知道该怎么做。
  • @BackSlash AFAIK String 没有append 方法,即StringBuilderStringBuffer
  • @LuiggiMendoza 是的,我的错

标签: java arraylist concat


【解决方案1】:

您当前代码中的主要问题:

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

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

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

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

【讨论】:

  • @YoungDeezie 请edit your question 并添加您当前的代码以查看可能出现的错误。
  • @YoungDeezie 我已经更新了我的答案。下次请多加小心,祝编码愉快:)。
【解决方案2】:

你所需要的只是

ArrayList<String> projectStrings = new ArrayList<>();
//populate as needed
System.out.println(projectStrings.get(0) + projectStrings.get(1));

如果你想使用ArrayList

注意我对泛型的使用。您定义的ArrayListObject 的集合。我的是String 的集合。以你的方式做这件事会花费你的编译时间保证,并且还会使其他一些答案不可行。

【讨论】:

  • 好点。我没有注意到他没有将 ArrayList 声明为 ArrayList。这应该可以解决他的问题。
【解决方案3】:

我认为您只需要了解 ArrayList 的工作原理。您可能正在尝试这样做:

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

...

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

【讨论】:

  • 返回错误 The method concat(java.lang.Object, java.lang.Object) is undefined for type stringProject
  • concat 是来自String API 的方法,应该是projectStrings.get(1).concat(projectStrings.get(0))
  • @BackSlash String#concat 是,但 concat 单独不是。
猜你喜欢
  • 2016-02-19
  • 2012-11-30
  • 1970-01-01
  • 1970-01-01
  • 2020-11-17
  • 2017-07-17
  • 2023-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多