简体   繁体   中英

Set size of inner arrays in Java

I have an ArrayList of Arrays:

ArrayList<byte[]> arrayOfBytes = new ArrayList <>();

I want (and I don't know how) to set a predefined size of the Inner Arrays (the bytes arrays).

Is this possible? If it is, how can I do it?

Thanks in advance!

The one and only way is to set the size when you're adding those arrays into the list.

list.add(new byte[size]);

There are no arrays in the List without you explicitly adding them. Just create the arrays at the right size.

Create a function the returns an array of constant size and use it

private byte[] getArray() {
    return new byte[10]();
}

The init the inner arrays by calling this function

Your list starts empty, so it has no arrays in it. Therefore you need to explicitly add them - and as part of doing so, you'll be dimensioning them:

final int arraySize = 1024;

for (int i = 0; i < 10; i++)
{
  arrayOfBytes.add(new byte[arraySize]);
}

当您声明列表是不可能的(该信息与声明列表本身无关)时,但是您可以在向列表中添加新元素时定义大小

arrayOfBytes.add(new byte[size]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM