Conversion of arrays in java -
assume have array of integers, int[] x={0,1,2,3};
can convert x array of type string? can upcast x array of doubles? above conversions, mean converting entries of array collectively, not individually.
how conversions work arrays in java? have convert each entry of original array , assign corresponding entry of target array?
in java8 can do:
int[] x = {0,1,2,3}; // int double double[] doublesarray = arrays.stream(x).asdoublestream().toarray(); //int string string[] stringarray = arrays.stream(x).maptoobj(string::valueof).toarray(string[]::new); // string double double[] doublesarrayfromstring = arrays.stream(stringarray).maptodouble(double::valueof).toarray(); arrays.stream(doublesarray).foreach(system.out::println); arrays.stream(stringarray).foreach(system.out::println); arrays.stream(doublesarrayfromstring).foreach(system.out::println);
here's running code.
Comments
Post a Comment