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?
baltazar
0
Light Poster
Recommended Answers
Jump to PostWhen 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.
Jump to PostYes, 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".
All 7 Replies
masijade
1,351
Industrious Poster
Team Colleague
Featured Poster
Ezzaral
2,714
Posting Sage
Team Colleague
Featured Poster
anamika_nagpur
0
Newbie Poster
TheGathering
12
Junior Poster
masijade
1,351
Industrious Poster
Team Colleague
Featured Poster
Turendur
0
Newbie Poster
anamika_nagpur
0
Newbie Poster
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.