我如何preFIX一个字符串的每个元素在一个字符串数组? [英] How do I prefix a String to each element in an array of Strings?
问题描述
我想知道是否有在Java中,可以preFIX定义字符串字符串数组中的每个字符串开头的函数。
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.
例如,
my_function({"apple", "orange", "ant"}, "eat an ") would return {"eat an apple", "eat an orange", "eat an ant"}
目前,我codeD这个功能,但我不知道,如果它已经存在。
Currently, I coded this function, but I wonder if it already exists.
推荐答案
像这样的事情存在于Java库。这不是Lisp的,所以数组不是列表,而不是已经为您提供了一堆名单导向功能。这是部分地由于Java的类型系统,这将使它不切实际的所有可在面向列表的方式使用不同类型的提供这么多的类似的功能。
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.
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;
}
会做阵列的伎俩,但也有列表
接口,其中包括可调整大小的的ArrayList
S,矢量
S,迭代
S,的LinkedList
s,而上而上,并在
Will do the trick for arrays, but there are also List
interfaces, which include resizable ArrayList
s, Vector
s, Iteration
s, LinkedList
s, and on, and on, and on.
由于面向对象编程的资料,这些不同的实现每个人将不得不实行prePEND(......),这将造成沉重打击了任何人关心执行任何类型的列表。在Lisp中,情况并非如此,因为该函数可以独立地为对象的存储
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.
这篇关于我如何preFIX一个字符串的每个元素在一个字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!