将字符串数组作为参数传递给Java函数

14

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

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

functionFoo(stringArray);

改为:

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

但是如果我这样做,就会出现错误,提示将String[]转换为String。我想知道是否可能以这种方式传递值,或者正确的方法是什么。


从术语上讲,Java 没有“函数”,而是“方法”。你应该这样思考:函数是一个独立的功能块(例如 c = a + b),而方法是对给定数据执行一个或多个函数的包装上下文(继续上面的例子,int sum(a,b))。 - Esko
1
我想将字符串数组作为参数传递给一个方法。然后可以使用“String []”作为该参数的类型。 - Fattie
8个回答

31

这样如何:

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

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


i&lt 是什么意思或作用?你是不是指的是 i<strings.length - Thufir

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

1

看一下熟悉的主方法,它以字符串数组作为参数


0
很可能您的方法声明不正确。请确保方法的参数是字符串数组(String []),而不仅仅是字符串(String),并在数组声明中使用双引号引用您的字符串。
private String[] stringArray = {"a","b","c","d","e","f","g","h","t","k","k","k"};
public void myMethod(String[] myArray) {}

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.

0

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


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

只需复制并粘贴...您的代码...它将正常工作...然后您就会了解如何将字符串数组作为参数传递...

谢谢


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接