将数组传递给方法(Java)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11027645/
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-31 03:29:54  来源:igfitidea点击:

Passing array to a method (Java)

java

提问by jhedm

I have a program that accepts a number that represents an array index and a String to replace the array element corresponding to the index.

我有一个程序,它接受一个表示数组索引的数字和一个字符串来替换与索引对应的数组元素。

Here is my code:

这是我的代码:

public static void main(String args[]){

    String names[]={"Alvin", "Einstein", "Ace", "Vino", "Vince"};

          for(int i=0; i<names.length;i++)
                 System.out.println(i + " - " + names[i]);

    replaceArrayElement(names);
}

   public static void replaceArrayElement(String name[]){

       int index = 0;
       String value = "";

       Scanner scan = new Scanner(System.in);

       System.out.print("\nEnter the index to be replaced:");
       index = scan.nextInt();

       if(index<name.length){

           System.out.print("Enter the new value:");
           value = scan.nextLine();

           name[index] = scan.nextLine(); 

           System.out.println("\nThe new elements of the array are");

           for(int j=0; j<name.length;j++)
               System.out.println(name[j]);
       } 
       else{     
            System.out.println("Error!");    
       }  
   }

What I need to do is to put the int index variable and String value variable inside the method replaceArrayElement as a parameters. But I don't know how to call a method with different data type parameters. Can somebody show me how?? Thank you.

我需要做的是将int index变量和String value变量放在方法replaceArrayElement中作为参数。但我不知道如何调用具有不同数据类型参数的方法。有人可以告诉我怎么做吗??谢谢你。

回答by Jon Skeet

Well it's not clear where you'd get the values to pass in from, but here's how you would declare the method:

好吧,目前尚不清楚从何处获取要传入的值,但您可以通过以下方式声明该方法:

public static void replaceArrayElement(String[] name, int index, String value)

You'd call it with:

你可以这样称呼它:

// Get the values from elsewhere, obviously
replaceArrayElement(array, 5, "fred");

Note that I've used String[] nameinstead of String name[]- while the latter syntax is permitted, it's strongly discouraged as a matter of style.

请注意,我使用了String[] name而不是String name[]- 虽然允许使用后一种语法,但强烈建议不要使用它作为风格问题。

回答by Pramod Kumar

Declare replaceArrayElement as follows -

声明 replaceArrayElement 如下 -

public static void replaceArrayElement(int index, String value, String[] name)

and call it as -

并将其称为 -

replaceArrayElement(2, "Einstein2", names);

回答by Alpesh Prajapati

  public static void main(String args[]){

String names[]={"Alvin", "Einstein", "Ace", "Vino", "Vince"};

          for(int i=0; i<names.length;i++)
                 System.out.println(i + " - " + names[i]);

    replaceArrayElement(3,"alpesh",names);
}

   public static void replaceArrayElement(int index, String replacename, String names[]){


       if(index<names.length){

           names[index] = replacename;

           System.out.println("\nThe new elements of the array are");

           for(int j=0; j<names.length;j++)
               System.out.println(names[j]);
       } 
       else{     
            System.out.println("Error!");    
       }  
   }