Passing a string array as a parameter to a function java

后端 未结 8 994
清酒与你
清酒与你 2020-12-16 20:09

I would like to pass a string array as a parameter to a function. Please look at the code below

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


        
相关标签:
8条回答
  • 2020-12-16 20:34

    please check the below code for more details

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

    just copy it and past... your code.. it will work.. and then you understand how to pass string array as a parameter ...

    Thank you

    0 讨论(0)
  • 2020-12-16 20:40

    look at familiar main method which takes string array as param

    0 讨论(0)
提交回复
热议问题