java 如何为字符串数组中的每个元素添加字符串前缀?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6308393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:19:04  来源:igfitidea点击:

How do I prefix a String to each element in an array of Strings?

javaarraysstringappend

提问by darksider

I would like to know if there is, in Java, a function that can prefix a defined String to the beginning of every String of an array of Strings.

我想知道在 Java 中是否有一个函数可以将定义的字符串前缀到字符串数组的每个字符串的开头。

For example,

例如,

my_function({"apple", "orange", "ant"}, "eat an ")  would return {"eat an apple", "eat an orange", "eat an ant"}

Currently, I coded this function, but I wonder if it already exists.

目前,我编写了这个函数,但我想知道它是否已经存在。

回答by StriplingWarrior

Nope. Since it should be about a three-line function, you're probably better of just sticking with the one you coded.

不。由于它应该是一个三行函数,因此您最好坚持使用您编码的函数。

Update

更新

With Java 8, the syntax is simple enough I'm not even sure if it's worth creating a function for:

使用 Java 8,语法足够简单,我什至不确定是否值得为以下对象创建函数:

List<String> eatFoods = foodNames.stream()
    .map(s -> "eat an " + s)
    .collect(Collectors.toList());

回答by Edwin Buck

Nothing like this exists in the java libraries. This isn't Lisp, so Arrays are not Lists, and a bunch of List oriented functions aren't already provided for you. That's partially due to Java's typing system, which would make it impractical to provide so many similar functions for all of the different types that can be used in a list-oriented manner.

java库中不存在这样的东西。这不是 Lisp,因此数组不是列表,并且还没有为您提供一堆面向列表的函数。这部分是由于 Java 的类型系统,这使得为可以以面向列表的方式使用的所有不同类型提供如此多的相似函数变得不切实际。

public String[] prepend(String[] input, String prepend) {
   String[] output = new String[input.length];
   for (int index = 0; index < input.length; index++) {
      output[index] = "" + prepend + input[index];
   }
   return output;
}

Will do the trick for arrays, but there are also Listinterfaces, which include resizable ArrayLists, Vectors, Iterations, LinkedLists, and on, and on, and on.

可以处理数组,但也有List接口,其中包括可调整大小的ArrayLists、Vectors、Iterations、LinkedLists 和 on、on 和 on。

Due to the particulars of object oriented programming, each one of these different implementations would have to implement "prepend(...)" which would put a heavy toll on anyone caring to implement a list of any kind. In Lisp, this isn't so because the function can be stored independently of an Object.

由于面向对象编程的特殊性,这些不同的实现中的每一个都必须实现“prepend(...)”,这会给任何关心实现任何类型列表的人带来沉重的代价。在 Lisp 中,情况并非如此,因为函数可以独立于对象存储。

回答by Peter Lawrey

How about something like ...

诸如此类的东西怎么样...

public static String[] appendTo(String toAppend, String... appendees) {
    for(int i=0;i<appendees.length;i++)
        appendees[i] = toAppend + appendees[i];
    return appendees;
}

String[] eating = appendTo("eat an ", "apple", "orange", "ant")