我正在尝试使用以下代码将包含Integer对象的ArrayList转换为原始int [],但它会引发编译时错误.是否可以用Java进行转换?
List<Integer> x = new ArrayList<Integer>();
int[] n = (int[])x.toArray(int[x.size()]);
Run Code Online (Sandbox Code Playgroud) ChannelBufferInputStream responseStream = (ChannelBufferInputStream) response.getBodyAsStream();
ArrayList<Byte> arrayList = new ArrayList<Byte>();
try {
while (responseStream.available() > 0) {
arrayList.add(responseStream.readByte());
}
} catch (IOException e) {
e.printStackTrace();
return internalServerError();
}
Iterator<Byte> iterator = arrayList.iterator();
byte[] bytes = new byte[arrayList.size()];
int i = 0;
while (iterator.hasNext()) {
bytes[i++] = iterator.next();
}
Run Code Online (Sandbox Code Playgroud)
在我的Web应用程序的每个页面加载时调用此代码.它似乎运行得非常快,但是有什么能让它运行得更快吗?
编辑 - 使用字节数组输出流更新
ChannelBufferInputStream responseStream = (ChannelBufferInputStream) response.getBodyAsStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
int read = responseStream.read();
while (read != -1) {
byteArrayOutputStream.write(read);
read = responseStream.read();
}
} catch …Run Code Online (Sandbox Code Playgroud) 我有这个签名的功能:
void insert(T[] thearray);
Run Code Online (Sandbox Code Playgroud)
我有一个像这样的字节数组:
byte[] value = new byte[4096];
Run Code Online (Sandbox Code Playgroud)
但是,如果我这样调用函数:
cb.insert(值);
我收到一个错误:
The method insert(Byte) in the type CircularBuffer<Byte> is not applicable for the arguments (byte[])
Run Code Online (Sandbox Code Playgroud)
CircularBuffer是带有insert方法的类.
我像这样实例化CircularBuffer:
CircularBuffer<Byte> cb = new CircularBuffer<Byte>(4096);
Run Code Online (Sandbox Code Playgroud)
如何将值传递给此插入函数?
为了清楚起见,CircularBuffer类声明如下:
public class CircularBuffer<T>
public CircularBuffer(int size) //ctor
Run Code Online (Sandbox Code Playgroud)
如果您需要更多细节:
EDIT到底为了效率原因我决定创建一个专门的ByteCircularBuffer. - 使用字节.这种原始类型到对象类型的区域,例如byte - > Byte令人困惑.