12

我想将字符串数组作为参数传递给函数。请看下面的代码

String[] stringArray = {'a', 'b', 'c', 'd', 'e'};

functionFoo(stringArray);

代替:

functionFoo('a', 'b', 'c', 'd', 'e');

但如果我这样做,我会收到一条错误消息,指出转换String[]String. 我想知道是否可以传递这样的值或者正确的方法是什么。

4

8 回答 8

27

怎么样:

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);
    }
}
于 2012-07-28T04:29:40.697 回答
6

以上所有答案都是正确的。但请注意,当您像这样传递时,您将传递对字符串数组的引用。如果您在被调用函数中对数组进行任何修改,它也会反映在调用函数中。

在 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) 之类的函数或您想要的任意数量的参数。

更多信息:http ://today.java.net/pub/a/today/2004/04/19/varargs.html

于 2012-07-28T07:40:22.663 回答
3

我相信这应该是这样做的方式......

    public static void function(String [] array){
    ...
    }

呼叫将像...

    public void test(){
        String[] stringArray = {"a","b","c","d","e","f","g","h","t","k","k","k","l","k"};
        function(stringArray);
    }
于 2012-07-28T04:31:06.303 回答
1

看看熟悉的以字符串数组为参数的 main 方法

于 2012-07-28T05:52:31.207 回答
0

您的方法声明很可能不正确。确保方法参数的类型是字符串数组 (String[]) 而不仅仅是字符串,并且在数组声明中使用双引号将字符串括起来。

private String[] stringArray = {"a","b","c","d","e","f","g","h","t","k","k","k"};
public void myMethod(String[] myArray) {}
于 2012-07-28T04:30:16.380 回答
0

我认为您忘记将参数注册为 String[]

于 2012-07-28T07:00:59.380 回答
0

随意使用它。

/*
 * 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()

基本上我会这样使用它:

String[] students = {"Tom", "Jeff", "Ashley", "Mary"};
// 4 new students enter the class so we need to extend the string array
students = extendStrArray(4, students); //this will effectively add 4 new empty positions to the "students" array.
于 2013-11-01T21:55:08.410 回答
0

请检查以下代码以获取更多详细信息


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

只需复制它并过去...您的代码..它将起作用..然后您了解如何将字符串数组作为参数传递...

谢谢

于 2020-06-24T23:51:45.747 回答