将字符串数组作为参数传递给java函数
我想将字符串数组作为参数传递给函数。请看下面的代码将字符串数组作为参数传递给java函数,java,function,arrays,Java,Function,Arrays,我想将字符串数组作为参数传递给函数。请看下面的代码 String[] stringArray = {'a', 'b', 'c', 'd', 'e'}; functionFoo(stringArray); 而不是: functionFoo('a', 'b', 'c', 'd', 'e'); 但如果我这样做,我会得到一个错误,说明将String[]转换为String。我想知道是否可以这样传递值,或者正确的方法是什么。怎么样: public class test { public sta
String[] stringArray = {'a', 'b', 'c', 'd', 'e'};
functionFoo(stringArray);
而不是:
functionFoo('a', 'b', 'c', 'd', 'e');
但如果我这样做,我会得到一个错误,说明将String[]
转换为String
。我想知道是否可以这样传递值,或者正确的方法是什么。怎么样:
public class test {
public static void someFunction(String[] strArray) {
// do something
}
public static void main(String[] args) {
String[] strArray = new String[]{"Foo","Bar","Baz"};
someFunction(strArray);
}
}
您的方法声明很可能不正确。确保methods参数的类型为stringarray(String[]),而不仅仅是String,并且在数组声明中的字符串周围使用双引号
private String[] stringArray = {"a","b","c","d","e","f","g","h","t","k","k","k"};
public void myMethod(String[] myArray) {}
我相信这应该是这样做的
公共静态无效函数(字符串[]数组){
...
}
而且通话会像这样进行
公共无效测试(){
String[]stringArray={“a”、“b”、“c”、“d”、“e”、“f”、“g”、“h”、“t”、“k”、“k”、“k”、“l”、“k”};
函数(字符串数组);
}
看看熟悉的main方法,它将字符串数组作为参数我想您忘记了将参数注册为string[]
以上所有答案都是正确的。但是请注意,当您这样传递时,您将传递对字符串数组的引用。如果对被调用函数中的数组进行任何修改,它也将反映在调用函数中
Java中还有另一个称为变量参数的概念,您可以研究它。它基本上是这样工作的。例如:-
String concat (String ... strings)
{
StringBuilder sb = new StringBuilder ();
for (int i = 0; i < strings.length; i++)
sb.append (strings [i]);
return sb.toString ();
}
在这里,我们可以调用concat(a、b、c、d)之类的函数或任何数量的参数
更多信息:您可以随意使用它
/*
* The extendStrArray() method will takes a number "n" and
* a String Array "strArray" and will return a new array
* containing 'n' new positions. This new returned array
* can then be assigned to a new array, or the existing
* one to "extend" it, it contain the old value in the
* new array with the addition n empty positions.
*/
private String[] extendStrArray(int n, String[] strArray){
String[] old_str_array = strArray;
String[] new_str_array = new String[(old_str_array.length + n)];
for(int i = 0; i < old_str_array.length; i++ ){
new_str_array[i] = old_str_array[i];
}//end for loop
return new_str_array;
}//end extendStrArray()
请检查以下代码以了解更多详细信息
package FirstTestNgPackage;
import java.util.ArrayList;
import java.util.Arrays;
public class testingclass {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.out.println("Hello");
int size = 7;
String myArray[] = new String[size];
System.out.println("Enter elements of the array (Strings) :: ");
for(int i=0; i<size; i++)
{
myArray[i] = "testing"+i;
}
System.out.println(Arrays.toString(myArray));
ArrayList<String> myList = new ArrayList<String>(Arrays.asList(myArray));
System.out.println("Enter the element that is to be added:");
myArray = myList.toArray(myArray);
someFunction(myArray);
}
public static void someFunction(String[] strArray)
{
System.out.println("in function");
System.out.println("in function length"+strArray.length );
System.out.println(Arrays.toString(strArray));
}
}
包第一个测试包;
导入java.util.ArrayList;
导入java.util.array;
公共类测试类{
公共静态void main(字符串[]args)引发InterruptedException{
//TODO自动生成的方法存根
System.out.println(“你好”);
int size=7;
字符串myArray[]=新字符串[大小];
System.out.println(“输入数组的元素(字符串):”;
对于(int i=0;i终端论方面,Java没有函数,只有方法。您应该认为函数是一个分离的功能(例如c=a+b
),而方法是它对给定数据执行的一个或多个函数的包装上下文(继续这个例子,int sum(a,b)
)。“我想将一个字符串数组作为参数传递给一个方法”,然后使用“string[]”作为该参数的类型。I<
的意思是什么
package FirstTestNgPackage;
import java.util.ArrayList;
import java.util.Arrays;
public class testingclass {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.out.println("Hello");
int size = 7;
String myArray[] = new String[size];
System.out.println("Enter elements of the array (Strings) :: ");
for(int i=0; i<size; i++)
{
myArray[i] = "testing"+i;
}
System.out.println(Arrays.toString(myArray));
ArrayList<String> myList = new ArrayList<String>(Arrays.asList(myArray));
System.out.println("Enter the element that is to be added:");
myArray = myList.toArray(myArray);
someFunction(myArray);
}
public static void someFunction(String[] strArray)
{
System.out.println("in function");
System.out.println("in function length"+strArray.length );
System.out.println(Arrays.toString(strArray));
}
}