Hi there,
What I am doing is filling a 2-D array with numbers in a random order.
i.e. I pick a position in the 2-D array and input a number there
eg: array[2][1] = 3;(this input is received from a text file)
In this way, I fill the array but some spots remain empty.
What I want to do is fill these empty position in the 2-D array with the number 99, but I don't know how to go through the array and test if each position is empty.
I tried going through the array and comparing each position to null but that didnt help, it gave me a "incomparible type" error.
Can someone help me?

Recommended Answers

All 7 Replies

When you initiate the array (i.e. int[][] array = new int[20][20]; ) every position is already filled with 0 (since a primitive cannot be null). So, if zero is not one of the numbers you are using, simply cycle through the arrays with a nested loop replacing 0 with 99.

Yes, as masijade mentioned, initialize the array elements to the value you want to represent "empty" before you start filling it. You didn't say what the valid range for your number entries was, but filling with -1 or Integer.MIN_VALUE can be useful values to represent "unset".

Agree that a premitive cannot be null. But what if one of the position is actually filled with number zero [0] from a text file (zero also being a number) and should not be replaced with 99?


When you initiate the array (i.e. int[][] array = new int[20][20]; ) every position is already filled with 0 (since a primitive cannot be null). So, if zero is not one of the numbers you are using, simply cycle through the arrays with a nested loop replacing 0 with 99.

Agree that a premitive cannot be null. But what if one of the position is actually filled with number zero [0] from a text file (zero also being a number) and should not be replaced with 99?

Then don't replace it with 99? Think, you're going from 2 states: nothing or number, to three states: nothing, number, or protected.

Easy way to do that is to make a nested loop to set all values to some other value than 0 such as -1 as soon as you make your 2D array. That way you will know that all -1 spots are fair game, all number > 0 spots are already set, and all 0 spots are untouchable.


That what you were talking about?

Are you using Java 1.5 or later? If so, make it an Integer array, rather than an int array. Then "empty" spots will be null and you just (after having filled it with the random numbers) cycle through with an == null check. And, you can still do array[j] = 1; or some other int, because autoboxing will automatically convert it to an Integer. You can also, later, use the Integer as if it were a normal int. That seems to be easiest solution.

Why doesn't he just fill the made arrays with 99 from the start then if you have a number of 0 it replaces a 99 thus you wont have to go back and check if the numbers are 0's. Although you would have to loop through first so you are adding more process time.

Just thought I would comment.

Tell me if I'm wrong...

Turendur

This appears to be the close fit solution. Attempting to fill the slots with any number (be it zero, 99 or any negative number) on array initialization is a processing burden and doesn't address the original problem of filling the array with the same number as that used for initialization. Having an Object array will allow null value and subsequent comparison during traversing the array and fill it with ANY number (zero, positive or negative intiger) as the need be.
Thanks for this solution.

Are you using Java 1.5 or later? If so, make it an Integer array, rather than an int array. Then "empty" spots will be null and you just (after having filled it with the random numbers) cycle through with an == null check. And, you can still do array[j] = 1; or some other int, because autoboxing will automatically convert it to an Integer. You can also, later, use the Integer as if it were a normal int. That seems to be easiest solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.