0

Could you tell me how to get size of reserved array ?

Look this:

public static String arr_list[][][][][]= new String[2][10][10][2][5];

I know it's big array.

I place data there, and want to get the data size of the third element.

arr_list[0][0].length - but it still returns 10.

But on [0][0] I have only 4 values:

arr_list[0][0][0]..
arr_list[0][0][1]..
arr_list[0][0][2]..
arr_list[0][0][3]..

How to return 4, not 10?

arshajii
  • 127,459
  • 24
  • 238
  • 287
user2652995
  • 57
  • 2
  • 15
  • 6
    Do you really need a 5d array. What are you storing here? – SoWhat Aug 11 '13 at 15:59
  • 1
    Also, it will return 10 because the elements are there regardless of whether you set it – SoWhat Aug 11 '13 at 16:00
  • But `10` is the correct answer. `arr_list[0][0][9]` is an array containing two arrays, each of five strings. – ruakh Aug 11 '13 at 16:00
  • A 5D array seems okay, but a 5D Arraylist is (In my opinion) where things get really ugly. You really can't break it down into Objects? Just imagine that type (ArrayList>>>>). Signatures will be fun! – tilpner Aug 11 '13 at 16:13
  • @StackOverflowException, "A 5D array seems okay..." Really? Nearly all the comments here say that there's probably a better solution, although we don't know what the OP is trying to do. Agreed about the declarations, though, I had the same thought. – neizan Aug 11 '13 at 16:23
  • @neizan: Yeah, I used 5D Arrays, when you need to access a lot of data fast (slow mobile phones). The reason for that was the [memory use of boxing and its speed](http://stackoverflow.com/questions/5199359/why-do-people-still-use-primitive-types-in-java) (I had an array of ints). But I also asked if breaking it down into objects is impossible... – tilpner Aug 11 '13 at 16:27

3 Answers3

2

Arrays are constant size containers, they always have a declared length, this is why you will always get 10.

Consider using Lists (for example ArrayList) instead.

neizan
  • 2,291
  • 2
  • 37
  • 52
lejlot
  • 64,777
  • 8
  • 131
  • 164
  • Is possible to create 5d arraylist? Could you tell me how? – user2652995 Aug 11 '13 at 16:07
  • 4
    @user2652995: despite being an experienced developer, I can barely wrap my head around arrays that have more than 2 dimensions. You should tell us what you want to achieve, because a 5-d array (or 5-d list) is probably not the appropriate solution to your problem. – JB Nizet Aug 11 '13 at 16:09
  • if you **really** need it, then 5d ArrayList would simple be an ArrayList of ArrayLists of ArrayLists of ArrayLists of ArrayLists, but I highly doubt that you actually need more then 2 dimensions. – lejlot Aug 11 '13 at 16:10
  • But please, don't do that, will you? **Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.** In this case, I'm sure he would not like type declarations like `ArrayList>>>>` and the comparably low level of abstraction, compared to an object-oriented structure... – tilpner Aug 11 '13 at 16:32
1

You have initialized a 5d array..... size of 1st dimension is 2,2d is 10 3d is 10 4d is 2 and size of 5th d is 5..

so the total size will be 2*10*10*10*2*5=20,000 if you leave them empty ..they will automatically be filled by null characters......if you want to calculate the size....you can find where the first null character is and count the elements before it

Ankit Srivastava
  • 280
  • 2
  • 7
  • 24
  • 2
    @user2652995: **No**, unless you present a valid reason why you need it to be 5dimensional, you should use something appropriate. Probably some custom classes. – jlordo Aug 11 '13 at 16:08
0

If you initialize an array with new String[2][10][10][2][5] you have explicitly told the system you want arr_list[0][0] to have 10 elements. I'm afraid it is going to believe you, and create a 10 element array.

If you want the size to vary depending on the number of actual element, you need to initialize it dynamically.

public static String arr_list[][][][][]= new String[2][][][][]; creates arr_list with two null references of type String[][][][]. You can then initialize each of them with arrays containing the number of elements you actually need at the next level, and so on.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75