Using Varargs

Nicholas Duchon: Oct 19, 2018.

Outline


Example 1

Declaration:

int lineCount

Reset lineCount value in calling method, and an example of code - naturally the references need to exist somewhere.

// setup
   lineCount = 0;
   String st;
// Stuff
   for (Tool t: r.tools)
      if (t.name.indexOf (sr) != -1)
        st += addSearch ("Tool", c.name, r.name, t.name);
// the rest of the program

The useful method, with a variable number of String arguments, referenced using array indices.

String addSearch (String... str) {
   String st = "";
   if (lineCount++ % 5 == 0) st += "\n";
   st += String.format ("\n%10s", str[0]);
   for (int i = 1; i < str.length; i++)
      st += String.format ("%20s", str[i]);
   return st;
} // end addSearch


ND.