Java 中 String 的可变参数如何转换成 byte[]的可变参数,如下面例子中 obj.functionB 的入参是 byte[]的可变参数,请问如何转换
public void functionA(String...args){
Object obj = new Object();
//args 如何转换成 functionB 的入参
obj.functionB(?);
}
Class Object{
public void functionB(byte[]...args){
......
}
}
1
hpeng 2016-07-07 20:08:43 +08:00 1
public class test {
public static void main(String[] args) { try { ByteArray(StringArrayToBytesArray("utf8", "abc", "def", "fff", "ddd")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } public static byte[][] StringArrayToBytesArray(String encoding, String ...strings) throws UnsupportedEncodingException { byte[][] bytes = new byte[strings.length][]; for (int i=0; i<strings.length; i++) { bytes[i] = strings[i].getBytes(encoding); } return bytes; } public static void ByteArray(byte[] ...args) { System.out.println(args.length); } } |
2
shyling 2016-07-07 20:59:12 +08:00 1
class M{
void customer(byte[] ...args){}; void convert(String ...args){ List<byte[]> list = Stream.of(args).parallel().flatMap(s->Stream.of(s.getBytes())).collect(Collectors.toList()); byte[][] bs = new byte[list.size()][]; list.toArray(bs); customer(bs); } } |