How do I convert varargs to an array?
Category: java.lang, viewed: 805 time(s).
Varargs can be seen as a simplification of array when we need to pass a multiple value as a method parameter. Varargs it self is an array that automatically created, for these reason you will be enable to do things you can do with array to varargs.
In the example below you can see the messages parameter can be assigned to the String array variables, we can call the length method to the messages parameter as we do with the array. So actually you don't have to convert varargs to array because the varargs is an array.
package org.kodejava.example.lang; public class VarargsToArray { public static void main(String[] args) { printMessage("Hello ", "there", ", ", "how ", "are ", "you", "?"); } public static void printMessage(String... messages) { String[] copiedMessage = messages; for (int i = 0; i < messages.length; i++) { System.out.print(copiedMessage[i]); } } } |
Related Examples
- How do I read system property as an integer?
- How do I decode string to integer?
- How do I insert a string in the StringBuilder?
- How do I remove substring from StringBuilder?
- How do I reverse a string by word?
- How do I remove trailing white space from a string?
- How do I remove leading white space from a string?
- How do I create a method that accept varargs in Java?
- How do I know a class of an object?
- How do I replace characters in string?
|
|