如何在方法和类之间传递ArrayList?

How to pass the ArrayList between methods and classes?

我有这个方法 returns 一个 arrayList 标记 :

public static String[] Tokenize(String input) throws InvalidFormatException, IOException {
    InputStream is = new FileInputStream("en-token.bin");    
    TokenizerModel model = new TokenizerModel(is);   
    Tokenizer tokenizer = new TokenizerME(model);    
    String tokens[] = tokenizer.tokenize(input);     
    for (String a : tokens)
        System.out.println(a);   
    is.close();
    return tokens;
}

我希望这些标记在另一种方法中使用:

public static void findName(String[] input) throws IOException {
    InputStream is = new FileInputStream("en-ner-person.bin");   
    TokenNameFinderModel model = new TokenNameFinderModel(is);
    is.close();  
    NameFinderME nameFinder = new NameFinderME(model);   
    Span nameSpans[] = nameFinder.find(input);   
    for(Span s: nameSpans)
        System.out.println(s.toString());           
}

此外,我如何在另一个 class 中使用此标记 arrayList,例如

public class Main{
    public static void main( String[] args) throws Exception
    {
        Anotherclass.Tokenize(input);
        Anotherclass.findName(tokens);

    }
}

我想不通!请帮助我。

非常感谢!

就用这个:

Anotherclass.findName(Anotherclass.Tokenize(input));

您创建了静态方法,因此您可以通过 Class 名称调用这些方法,例如

String[] SAMPLEARRAY = YOURCLASSNAME.Tokenize(String_ARRAY);

您必须将第一种方法的结果重新用于第二种方法。

public class Main{
     public static void main( String[] args) throws Exception
     {
         String[] tokens = Anotherclass.Tokenize(input);
         Anotherclass.findName(tokens);

     }
}

你还得弄清楚你的input变量是从哪里来的。