0

I am trying to create a method that should dynamically accept the string values and store it in an array list.Am trying to achieve method overloading here. Please see the below example and help me resolve in this:

public static void main(String[] args){
  verifyFilesInFolder("folder1", "file1", "file2", "file3");
  verifyFilesInFolder("folder2", "file1", "file2");
}

verifyFilesInFolder(String folder, ArrayList ???)
{

  List<String> list = new ArrayList<String>();
  int size=list.size();
  for(int i=0; i<size;i++){
    list.add(i); // is this correct??
  }
}

After storing it in the array list, i want to compare this expected list with Actual list captured from the application by sorting.

Hope you got the point am looking for. If not ArrayList, please suggest me a way to achieve this by having only one method but the number of files may change while calling that method.

Rahul
  • 759
  • 3
  • 21
  • 43
  • 2
    Possible duplicate of [Java variable number or arguments for a method](https://stackoverflow.com/questions/2330942/java-variable-number-or-arguments-for-a-method) –  Aug 14 '19 at 09:04

1 Answers1

1

You have 2 options:

Overloading

Define mulitple methods with the same names but different numbers of arguments:

public void func(String s1) { ... }

public void func(String s1, String s2) { ... }

public void func(String s1, String s2, String s3) { ... }

Varargs

Define a single method that takes any number of args:

public void func(String ...strings) {
    // strings is of type String[]
    String s1 = strings[0];
    String s2 = strings[1];  // note, be careful with ArrayIndexOutOfBoundsException
    // generally use a for-each loop
}

This can be called as:

  • func("1");
  • func("1", "2");
  • func("1", "2", "3");
  • func(new String[] {"1", "2", "3"});

EDIT:

If you want to add these values to a List, you can do this:

verifyFilesInFolder(String folder, String ...strings) {
    List<String> list = Arrays.asList(strings);

    for (String s : list) System.out.println(s);
}

Some inputs/outputs:

  • verifyFilesInFolder("folder", "item1"); -> prints "item1"
  • verifyFilesInFolder("folder", "item1", "item2"); -> prints "item1" and "item2"
  • verifyFilesInFolder("folder"); -> prints nothing
  • verifyFilesInFolder(); -> won't compile
cameron1024
  • 9,083
  • 2
  • 16
  • 36
  • This will not solve my problem actually. My scenario is to add the dynamic string values to a list and compare that list with another list captured from application. Please suggest if you have any other solution. – Rahul Aug 14 '19 at 09:29
  • Yes,Partially ! – Rahul Aug 14 '19 at 09:36
  • You can compare the size of two list and using the methods containsAll() – Dinesh Aug 14 '19 at 09:38
  • @Dinesh: I have an idea on comparing the list.But how to store the dynamic string values to an array list. – Rahul Aug 14 '19 at 09:39
  • 1
    @cameron1024 : Thanks Mate ! I think that will work.Will check and come back. – Rahul Aug 14 '19 at 09:45
  • @cameron1024 : For some other scenario , i tried this approach and yes, it is throwing ArrayOutOfBoundsException .. public void verifyErrorMsg(String... strings) { String abc = strings[0]; String def = strings[1]; String ghi = strings[2]; when i pass only two string parameters, it is throwing AIBOE. Please let me know how to assign variables dynamically. I need to validate based on those assigned variables. – Rahul Aug 21 '19 at 14:35
  • If you call `someVarargsFunction("hello", "world")` (i.e. with 2 parameters), the varargs parameter has 2 items in it: `param[0]` = `"hello"`, `param[1]` = `"world"`. Calling `param[2]` throws an `ArrayIndexOutOfBoundsException`, since the maximum value of an index for an `Object[] array` is `array.length - 1`. – cameron1024 Aug 21 '19 at 14:41
  • BTW, you can use `strings.length` to get the number of vararg parameters, if that helps you write your function depending on parameter count. – cameron1024 Aug 21 '19 at 14:42
  • @cameron1024 :Yeah, I thought of doing it. But am looking to implement with Map or list.Am a beginner in java and want to learn these.Please suggest. – Rahul Aug 22 '19 at 04:44
  • If you want to turn your `String ...strings` into a `List` you can use `Arrays.asList(strings)`. If you want to turn it into a `Map` you can use the following: `Map map = new HashMap<>(); Arrays.asList(strings).forEach(str -> map.put(str, someValue));` You should use a `List` if you care about the order of the `String`s, but don't need any extra data. You should use a `Map` if you don't care about the order of the `String`s, but you want to associate each `String` with some additional data. – cameron1024 Aug 22 '19 at 07:42