black box {array} in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

black box {array} in java

int[ ][ ][ ]... someArray = {--you don't know--}; If you don't know the size of "Array", do you see the elements each other? Can't we see the elements of Array, if we don't know array structure? In other programming language, can you?

15th Mar 2017, 11:22 PM
slowlearner
slowlearner - avatar
3 Answers
+ 3
If you CREATE the array object, all elements by default (for ints) are 0. For Strings they'd be null, and booleans false. For example, int[] array = new int[SOME_SIZE]; all elements up to that size are initially 0 however, you can't declare an array without giving it a size. So int[] array = new int[]; // error, what's its size? If all the elements have been set yet you don't know them, you can see them by looping through the array and printing out each element. I'm fairly certain this works the same way for most languages. If you want to make an array without declaring the size, you may be looking for a list.
15th Mar 2017, 11:55 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
For example, There is the array object SOMEONE created. We don't know how size this array object has. But can we try to see all the elements? by looping.... First, using .length method, we can print each first elements. Next, for each first elements, second elements(someword, 0, null, false) etc. will be searched. Next.... Like this, with trial and error, could we find all the elements? By the way, is this test a wrong manner? because Someone's data is hiding.
16th Mar 2017, 12:12 AM
slowlearner
slowlearner - avatar
+ 2
Yes if you don't know the size, you can find it with .length. If you don't know the elements you can find them all via a loop, or a specific one with array[index]. It's not reaaally trial and error since with length you know the length for certain. I don't think this would be the wrong manner of dealing with an array without knowing the elements/sizes since I'm not sure how else you can find the elements. You need to go through each index somehow. note* you can find the length of an array in the array with array[0].length, array[1].length. Pretty much need to know 2 things in order to get things in an array: The starting index, and the ending index. Starting is 0, ending is length. I believe Java has a built in method of finding all elements, try: Arrays.toString(SOMEONE); where SOMEONE is an / the array. If it gives you a memory location, it's another array/dimension. So you'd need to do: SOMEONE[0] to see the elements of THAT array, or use a for loop for each array in the array. (length) :P
16th Mar 2017, 1:28 AM
Rrestoring faith
Rrestoring faith - avatar