admin管理员组文章数量:1516870
I'm trying to convert the following JS to Java:
serializeSig:function(r,s){
var rBa=r.toByteArraySigned();
var sBa=s.toByteArraySigned();
var sequence=[];
sequence.push(2);
sequence.push(rBa.length);
sequence=sequence.concat(rBa);
sequence.push(2);
sequence.push(sBa.length);
sequence=sequence.concat(sBa);
sequence.unshift(sequence.length);
sequence.unshift(48);
return sequence
}
I think push would translate to add, concat to some kind of addAll, but what is unshift? And of what type would my variables in java be?
I'm trying to convert the following JS to Java:
serializeSig:function(r,s){
var rBa=r.toByteArraySigned();
var sBa=s.toByteArraySigned();
var sequence=[];
sequence.push(2);
sequence.push(rBa.length);
sequence=sequence.concat(rBa);
sequence.push(2);
sequence.push(sBa.length);
sequence=sequence.concat(sBa);
sequence.unshift(sequence.length);
sequence.unshift(48);
return sequence
}
I think push would translate to add, concat to some kind of addAll, but what is unshift? And of what type would my variables in java be?
3 Answers
Reset to default 5myArray.unshift(obj) corresponds to myList.add(0, obj).
A byte array in Java is byte[]. A list of byte arrays would be a List<byte[]>. (That's java.util.List, not java.awt.List, just in case you get the wrong import.)
EDIT: Looks like you are trying to create a byte array, not a list of byte arrays. In that case, you should use a java.nio.ByteBuffer, or possibly a java.io.ByteArrayOutputStream. The latter has to be written to sequentially -- you cannot do the equivalent of an unshift.
This looks like you are trying to use a ByteBuffer to serialize some data.
public static void serialize(ByteBuffer bb, String r, String s) {
bb.put(48);
int start = bb.position();
bb.put(0); // padding.
bb.put(2);
byte[] rBa = r.getBytes();
bb.put((byte) rBa.length);
bb.put(rBa);
byte[] sBa = s.getBytes();
bb.put((byte) sBa.length);
bb.put(sBa);
bb.put(start, (byte) (bb.position() - start - 1));
}
Using an List<Byte> is very inefficient and not supported by the packages which do IO.
You could use an arraylist of Bytes.
List<Byte> sequence = new ArrayList<>();
To add to the end of an arraylist use:
sequence.add(item);
To add to the front use:
sequence.add(0, item);
本文标签: javascriptWhat are java equivalents to JS pushconcatunshiftStack Overflow
版权声明:本文标题:javascript - What are java equivalents to JS push, concat, unshift? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.betaflare.com/web/1744453636a2606879.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论